r/cpp • • Jul 29 '19

Is auto-conversion of C++ code to a simpler, modern, and not backwards-compatible version possible?

I know that this kind of speculation doesn't go well here but could an automatic conversion of C/C++ code to a new language that's pretty close to modern C++ but with fixes (e.g. initialization syntax) and the bad parts removed (e.g. implicit conversions) ever be possible? A conversion to Rust or D would be harder. If it's possible, we could have a language with lesser cognitive load, able to use most legacy libraries and with the good and familiar features of C++ left intact. The performance might be somewhat worse - e.g. because memory initialization after allocations is desired. However, such a language wouldn't require as much work as completely new languages because it could just copy new features from C++.

53 Upvotes

122 comments sorted by

View all comments

Show parent comments

2

u/neuroblaster Jul 30 '19

I was wondering about that myself. May i ask why would you write `int a{10};` instead of `int a = 10;` as any human being would do in any other programming language for human beings?

I'm watching C++ cons from time to time and this reptiloid style of initialization seems to be plaguing source code of presenters. What's up with that? Fashion?

1

u/atimholt Jul 30 '19 edited Jul 30 '19

First, it should be mentioned that uniform initialization is for expressing a particular kind of idea about initialization: the compiler should be able to default to a sensible initialization that doesn’t care what’s being initialized, and all with a unified syntax. It’s also great for avoiding the most vexing parse, and let’s you use initialization in nameless contexts (unlike =). This all reduces the kind of mental load non-C++ devs complain about C++ having.

But notice I say it’s a sensible default, rather than always correct. Brace initialization was implemented under the principle of least astonishment. The idea is that what’s in the braces should represent what it looks like. Does it look like the fields you’d pass to the object’s constructor? Then the brace statement is a nameless instance in a context analogous to using auto. Does it look like an initializer list because all its elements are the same type, correct for initializing that class? Then it’s the initial state for that variable-size class object.

But what if it looks like both? What if you have a constructor that takes n T’s, but also have a constructor taking an initializer list of T’s? Some people find this a sticking point, but I find that the compiler’s behavior is beautifully intuitive.

Consider that you can use brace initialization outside of declaration statements (e.g. as an unnamed argument to a function). It would be a staggering mental load to expect the end programmer to have to search out whether a same-typed brace initialization is an initializer list or not. Therefore, they always are (if it can parse*). An alternative syntax is provided that is more specific, so you can leave the bounds of “sensible defaults”, but still be as clear and terse (in declarations) in what you mean, while being even more precise.

In an identifier declaration, you replace the braces with parentheses. When constructing namelessly as an argument to a function, you have to use the name of the class, else they’re considered evaluatable-expression parentheses. This is less needed, though, considering the most frequent use of in-place brace initialization of same types is STL containers—you rarely need to pass a default-y container, so it’s usually initializer lists.


* It can easily be deduced that using a same-typed brace initialization that doesn’t parse to an initializer list, in contexts where a fresh reader has to guess or look this up, is an extremely bad coding practice. It’s possible, but don’t do it. I’m guessing linters like clang-tidy can check for this.

1

u/neuroblaster Jul 31 '19
int x = 10;
std::vector<int> v = { 1, 2, 3 };
auto a = A(10);

This is what a human being even with minimal mental load is likely to understand intuitively.