Why are the conditions for an implicitly-defined move constructor/assignment operator different than for copy operations?

I believe backwards compatibility plays a big part here. If the user defines any of the “Rule of three” functions (copy ctor, copy assignment op, dtor), it can be assumed the class does some internal resource management. Implicitly defining a move constructor could suddenly make the class invalid when compiled under C++11. Consider this example: … Read more

Why do type alias templates use ‘using’ instead of ‘typedef’ in their syntax?

Here is what Bjarne Stroustrup says about why they introduced using instead of extending typedef: The keyword using is used to get a linear notation “name followed by what it refers to.” We tried with the conventional and convoluted typedef solution, but never managed to get a complete and coherent solution until we settled on … Read more

Why does std::optional not have a specialization for reference types?

When n3406 (revision #2 of the proposal) was discussed, some committee members were uncomfortable with optional references. In n3527 (revision #3), the authors decided to make optional references an auxiliary proposal, to increase the chances of getting optional values approved and put into what became C++14. While optional didn’t quite make it into C++14 for … Read more

Why was std::bit_cast added, if reinterpret_cast could do the same?

Well, there is one obvious reason: because it wouldn’t do everything that bit_cast does. Even in the C++20 world where we can allocate memory at compile time, reinterpret_cast is forbidden in constexpr functions. One of the explicit goals of bit_cast is to be able to do these sorts of things at compile-time: Furthermore, it is … Read more

What is the difference in const-correctness between C and C++?

In addition to the differences you cite, and the library differences that Steve Jessop mentions, char* p1; char const* const* p2 = &p1; is legal in C++, but not in C. Historically, this is because C originally allowed: char* p1; char const** p2 = &p1; Shortly before the standard was adopted, someone realized that this … Read more

Why is there no universal base class in C++?

The definitive ruling is found in Stroustrup’s FAQs. In short, it doesn’t convey any semantic meaning. It will have a cost. Templates are more useful for containers. Why doesn’t C++ have a universal class Object? We don’t need one: generic programming provides statically type safe alternatives in most cases. Other cases are handled using multiple … Read more