80 points · by todsacerdoti · 1y ago · 87 comments
I say unfortunately because this doesn’t scale. A junior programmer doesn’t have the time to process 30 years of C++’s historical development.
Mathematics (which has a much longer history and the same pedagogical problem) gets around this by consolidating foundations (Bourbaki-style enriched set theory -> category theory -> homotopy type theory, perhaps?) and by compartmentalization (a commutative algebraist usually doesn’t care about PDEs and vice versa).
I don’t see C++ taking either route, realistically.
I also wonder if most junior C++ programmers can shortcut a bit by just using common patterns. Articles like these I’ve always thought were geared more toward experienced programmers who are intellectually curious about the inner workings of the language.
But it has been taking the "compartmentalization" route: Once a new, nicer/safer/terser idiom to express something in code emerges, people being taught the language are directed to use that, without looking into the "compartment". Some of this compartmentalization is in the language itself, some in the standard library, and some is more in the 'wild' and programming customs.
It's true, though, that if you want to write your own library, flexibly enough for public use - or otherwise cater to whatever any other programmer might throw at you - you do have to dive in rather deep into the arcane specifics.
In particular you can most of the time define morphisms between concepts.
There are better choices if everything is built from scratch, but changing wheels from a running car isn't easy.
See also:
* https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n13... move designs
The real value of std::move is cases where you to HAVE to (effectively) make a deep copy, but still want to avoid the inefficiency. std::move supports this because moving means "stealing" the value from one variable and giving it to another. A common use case is move constructors for objects where you need to initialize the object's member variables, and can just move values passed by the caller rather than copying them.
Another important use case for std::move is returning values from functions, where the compiler will automatically use move rather than copy if available, allowing you to define functions returning large/complex return types without having to worry about the efficiency.
Given the limits of C, "adopting" a pointer variable would just mean that the foster-parent now had a copy of the pointer, and was considered owner of it, now responsible for freeing it too presumably. It's basically a move, except the source can now mess things up by still using the adopted value after the adoption.
It defaulted to pass-by-value, with shallow copy semantics, as opposed to pass by reference.
You COULD define you own type where the copy constructor did something other than deep copy (i.e. something other than copy!), just as you could choose to take a hand gun and shoot yourself in the foot.
> For non-union class types, the constructor performs full member-wise copy of the object's direct base subobjects and member subobjects, in their initialization order, using direct initialization. For each non-static data member of a reference type, the copy constructor binds the reference to the same object or function to which the source reference is bound.
Would you call this deep or shallow? I think most would call it a shallow copy.
In C++ the compiler generated copy constructors are shallow, not deep, meaning that if you want a copy to reconstruct the object being pointed to, you can not use the default copy constructor but need to supply your own explicit copy constructor to do so.
I couldn’t fathom starting a new project with whatever the current C++ is now.
As someone who grew up with modern C++, I can't even imagine going back to C++98 because it feels so incredibly verbose. Just compare how you iterate over a std::map and print its items in C++98 vs C++23:
// C++98:
for (std::map<std::string, int>::const_iterator it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << ": " << it->second << "\n";
}
// C++23:
for (const auto& [key, value] : m) {
std::print("{}: {}\n", key, value);
}
Then there are all the features I would miss, for example: - auto
- lambda functions and std::function
- move semantics
- std::unique_ptr and, to a lesser extent, std::shared_ptr
- variadic templates
- std::filesystem
- std::chrono
- std::thread, std::mutex, std::atomic, etc.
- a well-defined memory model for multi-threaded programs
- unordered containers
- structured bindings
- class template argument deducation
- std::format
- std::optional
- std::variant
- etc.But when you try to use all these funny features you're enumerating there for something serious, it will invariably end up in an overcomplicated slow compiling morass. Even just trying to make the types click for inserting something into a non-trivial templatized hashmap becomes a tedious act, and the IDE cannot help anymore either.
(Last issue I had was with catching some exception just to ignore it. Turned out catch(std::bad_alloc) doesn't work, you need write catch (std::bad_alloc&).)
I prefer writing simple C-style C++ where I write whole subsystems from scratch, and I can be very clear about the semantics from the start, design in what matters, and leave out what doesn't. Adding all the built-in object semantics baggage is too much overhead.
Modern C++ is easier and safer than it has ever been.
The biggest slight is simply that there are high quality alternatives.
You can still write things the old way, if you like.
everything has been going downhill since then. coincidence? i think not!
C++14:
- generalized lambda capture
- generic lambdas
C++17: - structured bindings
- init statement for if
- class template argument deduction (CTAD)
- std::string_view
- std::filesystem
- std::variant
- std::optional
- std::to_chars() and std::from_chars()
C++20: - std::format
- coroutines (makes ASIO code so much cleaner!)
- concepts
- std::span
- bit manipulation (<bit>)
- std::bind_front
- std::numbers (math constants)That's one of my biggest issues with C++ today. Objects that can be moved must support a "my value was moved out" state. So every access to the object usually starts with "if (have-a-value())". It also means that the destructor is called for an object that won't be used anymore.
MSVC and the Clang static analyzer have a analysis checks for this too. Not sure about GCC.
It's worth remembering though that values can be reinitialized in C++, after move.
... because I gave up on C++ in 2011, after reading Scott Meyers excellent Effective C++. It made me realize I had no desire to use a language that made it so difficult to use it correctly.
As for this article, it really did de-mystify those strange foo&& things for me. I had no idea that they're functionally identical to references and that what C++ does with them is left up to convention. But I still felt like I had to fight against sanity loss from a horrid realization.
Does it lead to problems? Surely. Should all metods be virtual by default? Probably. Should there be some keyword that indicates in derived class that a method intentionally shadows a non virtual method from the base class? Yes.
It's not a great human oriented design but it's consistent.
https://en.cppreference.com/w/cpp/language/rule_of_three.htm...
The confusion kind of speaks for itself. The language is a construction set where the primary building block is razor blades.
> int&& rvalueRef = (int&&)x;
Why are they casting x here?
I have steadfastly avoided dealing with C++ for almost 30 years, and I am grateful that I did not have to. It seems like such a messy language with overloaded operators and symbols ( don't even get me started on Lambdas!)
Anyway C++ isn't as complicated as people say, most of the so called complexity exists for a reason, so if you understand the reasoning it tends to make logical sense.
You can also mostly just stick to the core subset of the language, and only use the more obscure stuff when it is actually needed(which isn't that often, but I'm glad it exists when I need it). And move semantics is not hard to understand IMO.
I think there was a comment on HN by Walter Bright, saying that at some point, C++ became too complex to be fully understood by a single person.
> You can also mostly just stick to the core subset of the language
This works well for tightly controlled codebases (e.g. Quake by Carmack), but I'm not sure how this work in general, especially when project owners change over time.
OK, let me ask this: what is "&&" ? Is it a boolean AND ? Where in that article is it explained what "&&" is, other than just handwaving, saying "it's an rvalue".
For someone who's used to seeing "&" as an "address of" operator (or, a pointer), why wouldn't "&&" mean "address of pointer" ?
> For someone who's used to seeing "&" as an "address of" operator (or, a pointer)
You must be talking about "&something" which takes the "address of something" but the OP does not talk about this at all. You know this because you wrote in your other comment ...
> And if this indeed the case, why is int&& x compatible with int& y ?
So you clearly understand the OP is discussing "int&&" and "int&". Those are totally different from "&something". Even a cursory reading of the OP should tell you these are references, not the "address of something" that you're probably more familiar with.
One is rvalue reference and the other is lvalue reference and I agree that the article could have explained it better what they mean. But the OP doesn't seem to be an introductory piece. It's clearly aimed at intermediate to advanced C++ developers. What I find confusing is that you're mixing up something specific like "int&&" with "&something", which are entirely different concepts.
I mean when have you ever seen "int&" to be "address of" or "pointer"? You have only seen "&something" and "int*" and "int**" be "address of" or "pointer", haven't you?
So, basically, you're just trolling us about a language you avoid using. Thanks, that's very helpful.
Problem: Whenever I'm dealing with X, I get tripped by Y.
Solution A: Don't deal with X.
Solution B: Understand Y, when dealing with X.
For && meaning, this article [1] is still very useful.
[1]: https://isocpp.org/blog/2012/11/universal-references-in-c11-...