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

Suggestions on syntax to express mathematical formula concisely

If you’re going to be writing this for the ab-initio world (which I’m guessing from your MP2 equation) you want to make it very easy and clear to express things as close to the mathematical definition that you can. For one, I wouldn’t have the complicated range function. Have it define a loop, but if … Read more

Scala’s .type and Java’s .class literal

Here is my rationalization: classOf[T] classOf is defined in Predef as a function with this signature: def classOf[T]: Class[T] Although it’s implemented by the compiler, using the function syntax is possible without having to create any special treatment in terms of syntax. So that’s one reason here to consider this option. The alternative like String.class … Read more

What is the rationale for not having static constructor in C++?

Using the static initialization order problem as an excuse to not introducing this feature to the language is and always has been a matter of status quo – it wasn’t introduced because it wasn’t introduced and people keep thinking that initialization order was a reason not to introduce it, even if the order problem has … Read more

Why is std::make_unique not implemented using list initialization?

In C++20, this will compile: std::make_unique<point>(1, 2); due to the new rule allowing initializing aggregates from a parenthesized list of values. In C++17, you can just do: std::unique_ptr<point>(new point{1, 2}); That won’t work with make_shared though. So you can also just create a factory (forwarding left as an exercise): template <typename… Args> struct braced_init { … Read more