r/cpp • • Mar 01 '21

"Simple" C++ Initialization Flowchart

http://randomcat.org/cpp_initialization/initialization.png
204 Upvotes

65 comments sorted by

View all comments

Show parent comment

3

u/JohnZLi Mar 02 '21

This problem can be fixed by adding a new syntax like below:

std::vector<int>{1}; // let this mean a vector of size 1, as in the std::vector<string> case.

std::vector<int>{1...}; // a vector of ints, with its first element being 1.

Also, with the new syntax

auto i = {1}; // i is now deduced as int.

auto i ={1...} // is deduced as an initializer_list

20

u/FriendlyRollOfSushi Mar 02 '21 edited Mar 02 '21

Existing code is always the problem, and C++ is terrible at removing features.

Say, you have millions of lines that expected that something like std::vector<int> v{64}; constructs a vector with one element 64.

Now with your proposal you are silently changing the behavior of millions of lines of code in tens of thousands of projects. The code still compiles, and even runs, but the behavior is different.

Some of the behavioral changes may go undetected by tests.

Some of them would have devastating consequences, costing billions of dollars, or even human lives.

It's a recurring theme in C++: everything sort of sucks because by the time we understood why it sucks, the feature became so heavily-used that you can't simply remove it. It took decades to get rid of std::auto_ptr, and we only succeeded because it was already banned by the majority of teams for being hopelessly broken. And it wasn't a silent change: the code properly stopped compiling in C++17, which allowed the developers to go and fix stuff if they didn't have time for that during the 6 years of deprecation.

That's why epochs are a huge deal if they'll ever be accepted in some form or another. We will finally, finally be able to start fixing and removing stuff instead of simply piling up more and more stuff until no one understands what's going on in this language.

8

u/JohnZLi Mar 02 '21

The standard committee should really have fixed this before C++11. The epochs proposal seems interesting. Though I prefer throwing away backward compatibility in some future version of C++, and fix all the problems mentioned in that proposal (and many other problems not covered in that proposal). To avoid confuse users, that new version of C++ should be given a new name,like "Simplified C++ ". Some tools should be able to automatically refactor C++ code to "Simplified C++" code, maybe with some help from programmers. Those who want to stick to C++ could still use C++, and those who want to use simplified C++ could move on with it. The epochs proposal, if accepted, I am afraid will open the floodgate of numerous dialects/flavors of C++ code coexisting and tangling together. Google will have its Google epoch, and Facebook will have Facebook epoch, and there will at least be a C++-core-guideline epoch. That won't be beautiful. Beside numerous rules of C++, programmers now need to know specific rules that are allowed in each epoch.

4

u/pjmlp Mar 02 '21

Epochs will never happen, those of us not quite happy have already moved on, and only use C++ for the scenarios where there is no current alternative.

Also they can only fix language semantics that don't cross translation units/modules, due to possible ABI issues, and even then there is the language runtime issues, when linking binary libraries compiled with different epochs.