blog/assets/js/45c9e308.e6839f8a.js

1 line
11 KiB
JavaScript
Raw Normal View History

"use strict";(self.webpackChunkfi=self.webpackChunkfi||[]).push([[7084],{53181:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>r,default:()=>h,frontMatter:()=>o,metadata:()=>a,toc:()=>l});var i=t(85893),s=t(11151);const o={slug:"placeholders",title:"Placeholders",description:"Placeholders that are quite convenient to use when working on the code.\n",last_update:{date:new Date("2023-11-24T00:00:00.000Z")}},r=void 0,a={id:"exceptions-and-raii/2023-11-24-placeholders",title:"Placeholders",description:"Placeholders that are quite convenient to use when working on the code.\n",source:"@site/cpp/07-exceptions-and-raii/2023-11-24-placeholders.md",sourceDirName:"07-exceptions-and-raii",slug:"/exceptions-and-raii/placeholders",permalink:"/cpp/exceptions-and-raii/placeholders",draft:!1,unlisted:!1,editUrl:"https://github.com/mfocko/blog/tree/main/cpp/07-exceptions-and-raii/2023-11-24-placeholders.md",tags:[],version:"current",lastUpdatedAt:1700784e3,formattedLastUpdatedAt:"Nov 24, 2023",frontMatter:{slug:"placeholders",title:"Placeholders",description:"Placeholders that are quite convenient to use when working on the code.\n",last_update:{date:"2023-11-24T00:00:00.000Z"}},sidebar:"autogeneratedBar",previous:{title:"Exceptions and RAII",permalink:"/cpp/category/exceptions-and-raii"},next:{title:"Environment",permalink:"/cpp/environment"}},c={},l=[{value:"Design",id:"design",level:2},{value:"Implementation",id:"implementation",level:2},{value:"Wrapping in a function",id:"wrapping-in-a-function",level:2},{value:"Magic trick",id:"magic-trick",level:2},{value:"Finishing off with 2 more exceptions",id:"finishing-off-with-2-more-exceptions",level:2},{value:"Post-mortem",id:"post-mortem",level:2}];function d(e){const n={a:"a",admonition:"admonition",code:"code",em:"em",h2:"h2",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,s.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsxs)(n.p,{children:["Here we will try to implement some placeholders that you can find in other\nlanguages, but I miss them in the C++. I'm taking the inspiration from languages\nlike Rust (all that we will implement) or Kotlin (",(0,i.jsx)(n.code,{children:"TODO"}),") that have them\nimplemented."]}),"\n",(0,i.jsx)(n.p,{children:"You may ask what placeholders do we need in the code, in our case we will be\ntalking about TODOs and unexpected situations, such as not implemented branches."}),"\n",(0,i.jsx)(n.p,{children:"Namely we will implement"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"todo"}),","]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"unimplemented"}),", and"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"unreachable"}),"."]}),"\n"]}),"\n",(0,i.jsx)(n.h2,{id:"design",children:"Design"}),"\n",(0,i.jsx)(n.p,{children:"If we take the two languages mentioned above as examples, there are at least two\nways how to implement them:"}),"\n",(0,i.jsxs)(n.ol,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.em,{children:"panic"})," when they are reached (as they do in Rust), or"]}),"\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.em,{children:"raise"})," an exception when they are reached (as they do in Kotlin)."]}),"\n"]}),"\n",(0,i.jsxs)(n.p,{children:["I will choose raising an exception, since the closest equivalent of ",(0,i.jsx)(n.em,{children:"panic"})," in\nC++ would be ",(0,i.jsx)(n.code,{children:"assert"}),"s that are (by default) disabled in the ",(0,i.jsx)(n.em,{children:"release builds"}),"."]}),"\n",(0,i.jsx)(n.p,{children:"However I am too lazy to do:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cpp",children:'throw todo();\n// or\nthrow todo("optional note");\n'})}),"\n",(0,i.jsx)(n.p,{children:"Therefore we will implement exceptions and also wrap them in functions, so that\nwe can do:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cpp",children:'todo();\n// or\ntodo("optional note");\n'})}),"\n",(0,i.jsx)(n.admonition,{type:"tip",children:(0,i.jsx)(n.p,{children:"Wrapping them in a