{"id":3010,"date":"2021-10-29T23:46:32","date_gmt":"2021-10-30T07:46:32","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=3010"},"modified":"2021-10-30T21:23:22","modified_gmt":"2021-10-31T05:23:22","slug":"rationale-behind-c-commandments-2-philosophy-of-c","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/29\/rationale-behind-c-commandments-2-philosophy-of-c\/","title":{"rendered":"Rationale Behind C++ Commandments (2) &#8211; Philosophy of C"},"content":{"rendered":"\n<p>Everything is seen as a bitstream<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>pointers are just integers to memory locations<br>&#8211; [25] integer and pointers might be indistinguishable in signature resolution<br><\/li><li>code (CPU instructions) and addresses are treated the same way as a stream of data<br>&#8211; concept of function pointers leads to lambda (functors)<br>&#8211; classes came from structs containing data and function pointers (combined with namespaces)!<br><\/li><li>unchecked type declarations: the compiler trusts your interpretations of data<br>&#8211; leads to run-time features such as overriding (virtual methods)<br><\/li><li>handles (pointers and references) has unrestricted power<br>&#8211; [29] can <code>const_cast<\/code> it away if the handle is exposed (bad idea)<\/li><\/ul>\n\n\n\n<p>Performance-first design choice<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>do not pay performance penalty for features not used<br>&#8211; static compilation and binding by default<br>&#8211; unchecked type declarations (see above)<br><\/li><li>static compilation: the compiler tries to know everything at compile time<br><\/li><li>static binding by default (cheapest)<br>&#8211; pay extra to use virtual methods (overriding)<br>&#8211; [38] default parameter values are statically bound and not stored in vtable (i.e. overridden child method&#8217;s default values are ignored and parent&#8217;s default values are used ONLY WHEN called through up-casts)<br><\/li><li>inline is at the mercy of the optimizer (which can choose to emit an object if decided inlining is counter-productive). Mechanism that forces a function pointer to exist (pointing the function, virtual functions creates the pointer in vtable)<\/li><\/ul>\n\n\n\n<p>Toolchain<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>preprocessor (parser &amp; macros)<\/li><li>compiler (create object files per translation unit, which is .c file in C)<br>&#8211; access control (encapsulation) extends the old trick of emulating <code>private<\/code> in C++ through macros by marking functions as <code>static<\/code> (local within translation unit) in C.<\/li><li>linker (combine object files and adjust the addresses)<\/li><\/ol>\n\n\n\n<p>Templates behaves like a combination of macros (copy-and-paste with parameters) except it&#8217;s spread <a rel=\"noreferrer noopener\" href=\"https:\/\/www.drdobbs.com\/inline-redux\/184403879\" target=\"_blank\">across the toolchain like inline<\/a> optimizations:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Code bloat (one copy per type combination)<\/li><li>Can only live in the header files (it&#8217;s a template, not realized code, so no object is emitted like a .cpp file)<\/li><\/ul>\n\n\n\n<p>Parsing (language design)<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>most vexing parse<\/strong> [Effective STL Item 6]: if something can be interpreted as a function declaration, it will be interpreted as a function declaration<\/li><\/ul>\n\n\n\n<p>Plain Old Data Types (C++ classes tried to emulate in their operator overloading behavior)<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>[15] allow <code>(a=b)=c<\/code> chaining by returning <code>*this<\/code> for <code>operator=<\/code><\/li><li>[21] disallow rvalue assignment <code>(a+b)=c<\/code> by returning <code>const <\/code>object<\/li><\/ul>\n\n\n\n<p><\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_3010\" class=\"pvc_stats all  \" data-element-id=\"3010\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Everything is seen as a bitstream pointers are just integers to memory locations&#8211; [25] integer and pointers might be indistinguishable in signature resolution code (CPU instructions) and addresses are treated the same way as a stream of data&#8211; concept of &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/29\/rationale-behind-c-commandments-2-philosophy-of-c\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_3010\" class=\"pvc_stats all  \" data-element-id=\"3010\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[30,25,64],"tags":[],"class_list":["post-3010","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-cpp","category-rbcc-series"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=3010"}],"version-history":[{"count":11,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3010\/revisions"}],"predecessor-version":[{"id":3071,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3010\/revisions\/3071"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=3010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=3010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=3010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}