{"by":"compiler-devel","descendants":87,"id":32500572,"kids":[32523079,32523950,32525002,32523088,32525858,32524054,32524886,32523432,32523503,32523487,32524186,32525532,32523992,32526289,32526399,32522993,32526125,32538373,32523952,32537180,32524453,32527520,32524250,32529818],"score":76,"text":"Modified C++<p>Inspired by the paper &quot;Some Were Meant for C&quot; by Stephen Kell, I decided to show that it&#x27;s possible to iterate C++ to be safer, more explicit, and less error-prone.<p>Here&#x27;s a possible starting point: I didn&#x27;t invent a new language or compiler, but took the world&#x27;s best compiler, clang, and modified it to begin iterating towards a new furture of C++. Naming things is hard, so I call this &#x27;Modified C++&#x27;. Some of the following could be implemented as tooling in a linter or checker, but the idea is to update the compiler directly. I also wanted to learn more about clang. This compiler needs a flag to enable&#x2F;disable this functionality so that existing library code can be used with a &#x27;diagnostic ignored&#x27; pragma.<p>You can build clang using the normal non-bootstrap process and you&#x27;ll be left with a clang that compiles C++ but with the following modifications:<p><pre><code>     - All basic types (excluding pointers and references) are const by\n     default and may be marked &#x27;mutable&#x27; to allow them to be changed after\n     declaration\n     - Lambda capture lists must be explicit (no [&amp;] or [=], by themselves)\n     - Braces are required for conditional statements, case and default\n     statements within switches, and loops\n     - Implicit conversions to bool are prohibited (e.g., pointers must be\n     compared against nullptr&#x2F;NULL)\n     - No goto support\n     - Explicit &#x27;rule of six&#x27; for classes must be programmer-implemented\n     (default, copy, and move c&#x27;tors, copy and move assignment, d&#x27;tor)\n     - No C style casts\n\n</code></pre>\nHere&#x27;s an example program that&#x27;s valid in Modified C++:<p><pre><code>    mutable int main(int, char**)\n    {\n      mutable int x = 0;\n      return x;\n    }\n\n\n    Here&#x27;s another that will fail to compile:\n\n\n    mutable int main(int, char**)\n    {\n      int x = 1;\n      x = 0;  &#x2F;&#x2F; x is constant\n      return x;\n    }\n\n</code></pre>\nI&#x27;d like your feedback. Future changes I&#x27;m thinking about are:<p><pre><code>     - feature flag for modified c++ to enable&#x2F;disable with &#x27;diagnostic ignored&#x27;\n     pragma, to support existing headers and libraries\n     - support enum classes only\n     - constructor declarations are explicit by default\n     - namespaces within classes\n     - normalize lambda and free function syntax\n     - your ideas here</code></pre>","time":1660764093,"title":"Show HN: Modifying Clang for a Safer, More Explicit C++","type":"story","url":"https://github.com/compiler-devel/llvm-project/commit/cfd497fadb8bae4c5428f40ea50cfc760649afa4"}