Show HN: Modifying Clang for a Safer, More Explicit C++
IMO it would help adoption if you supply a clang-powered rewriter into and out of your language variant. It allays the fear of losing your codebase if the compiler project dies.<p>Reverse the default for typename. Currently some_class&lt;T&gt;::thing is assumed to be an expression where &#x27;thing&#x27; is a variable, when we don&#x27;t know which template pattern to use because there may be an explicit specialization on the T that the user chooses. Hence, we have to say &quot;typename std::vector&lt;T&gt;::iterator it;&quot; instead of just saying &quot;std::vector&lt;T&gt;::iterator it;&quot;. Instead, reverse that and assume it&#x27;s a type by default unless shown that it&#x27;s an expression. You&#x27;ll need a new keyword for that, replacing &quot;typename&quot;.<p>Remove the promotion-to-int rules. Currently in C (and in C++)<p><pre><code>  unsigned short test(unsigned short a, unsigned short b, unsigned short c) {
    unsigned short x = a * b * c;
    return x;
  }
</code></pre>
can have UB as signed integer overflow because any math done on an object smaller than int gets promoted to int. (No, you can&#x27;t fix this with &quot;(((unsigned short)x) * ((unsigned short)y))&quot; the promotion happens on <i>&#x27;s LHS and RHS, if those have types smaller than int.) Beyond this, people seem to expect that the type of the variable declaration will appertains to the calculation on the right, but it doesn&#x27;t. For instance people seem to think &quot;float f = a + b;&quot; can&#x27;t overflow where &#x27;a&#x27; and &#x27;b&#x27; are ints, because the assignment is going into a float.<p>I haven&#x27;t thought this idea through completely yet. Extend pointer types to include a static allocation identity as part of the type. Address-of local variable or global variable should produce one of these pointers. A &quot;static allocation identity&quot; is a special-typed zero-size variable, so you can stick it in code or as a class member. You could have pointers that were guaranteed to be allocated by THIS allocation point, instead of pointing to every possible T</i> in the program. I&#x27;ll fake up a syntax, &quot;tree_node ^ tree::node_alloc <i>&quot;. It&#x27;s known not to alias any other TreeNode</i> the program might have, it has to be attached to the allocation point owned by that specific &quot;node_alloc&quot; in that object. (Let me phrase it differently. A tree in C or C++ has pointers which can point anywhere as long as it&#x27;s another tree node type. That could be pointing to a different tree, it could be a self-pointer, it could be pointing up the tree, and so on. If your tree_node class has an allocation root, you can say that the pointers are things allocated through this allocation root. They can not outlive the allocation root. They are distinct from the things allocated by other allocation roots, which are the same tree_node types, but different tree_node objects. The node&#x27;s list of children is std::vector&lt;std::unique_ptr&lt;tree_node ^ node_alloc <i>&gt;&gt; so it clearly only holds pointers it allocated itself.)<p>There&#x27;s another problem with pointer related to the above. Some code I saw used a &quot;T &amp;get_or_default&lt;K, V&gt;(Container &amp;c, K key, V &amp;default);&quot; and the problem was that people would call it with a temporary for the default, like &quot;Value &amp;x = get_or_default(mymap, key, Value());&quot;  and they&#x27;d be holding a dangling reference. If you could make that an error, that&#x27;d be great. Maybe we use a trick like the &quot;allocation root&quot; above and treat pointers or references to temporaries have different type from the local variable. Then get_or_default takes and returns a reference-to-temporary and attempting to assign that to a reference in a variable declaration fails. Unlike the previous &quot;allocation root&quot; idea where you indicate the only thing you accept, this would be a case where you accept all allocation roots except one, the &quot;temporaries&quot; allocation root.<p>As far as I know, no compiler takes advantage of the freedom of the order of operations except in the most trivial ways. Everyone knows that in &quot;f() </i> g() + h()&quot; that * must happen before +, but people think this means that f() and g() must happen before h(). No, they may happen in any order at all. I had to fix a lot of code that did &quot;Print(stream.read(), stream.size())&quot; where &quot;read&quot; updates the pointer and leaves size == 0: gcc ran stream.size() first and clang ran stream.read() first, setting the subsequent size to zero. Similar issue with &quot;expr1() = expr2();&quot; expressions.<p>Extend switch() and case to work on objects with any operator== defined. Add a statement for fallthrough and default to &#x27;break;&#x27; before the start of the next case-label. Give each case label its own scope so I can declare variables in there without adding my own curly-braces. (Bonus 1 can you design a way to ensure that case labels are not overlapping? May require something other than operator==. Bonus 2 can you allow cases to be structured binding matches, similar to Rust?)<p>Speaking of structured binding, it&#x27;s great but doesn&#x27;t allow nesting. This<p><pre><code>  std::vector&lt;std::unordered_map&lt;std::string, std::pair&lt;int, int&gt;&gt;&gt; v;
  for (auto [name, [lhsid, rhsid]] : v) {
</code></pre>
is code I actually wanted to write in the past week yet that&#x27;s a syntax error.<p>Add the ability to declare object inheritance (&quot;class Derived : Base;&quot;) so that I can cast between them before writing out the body of the derived class. Also allow me to write out the entire class tree with no possibility for extension in another translation unit. The &quot;final&quot; keyword states that a class may not be derived from, but I usually have a Base class which does have subclasses, but a known list of subclasses that will never grow without recompiling the whole project. Currently the compiler has to assume I could write a new subclass and compile it into a shared object that the existing program dlopen&#x27;s and the existing program will work. It&#x27;s crazy. No, I have the final tree not just some leaf classes, please devirtualize the whole thing for me.<p>Are ABI changes on the table? Explicit template instantiations and explicit specializations should mangle differently. See my comment elsewhere: <a href="https:&#x2F;&#x2F;github.com&#x2F;dealii&#x2F;dealii&#x2F;issues&#x2F;3705#issuecomment-1175596077" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dealii&#x2F;dealii&#x2F;issues&#x2F;3705#issuecomment-11...</a><p>If I think of some more, I&#x27;ll reply to myself.