enum vs constexpr for actual static constants inside classes

For the record, the static constexpr version will work like you’d expected in C++17. From N4618 Annex D.1 [depr.static_constexpr]: D.1 Redeclaration of static constexpr data members [depr.static_constexpr] For compatibility with prior C++ International Standards, a constexpr static data member may be redundantly redeclared outside the class with no initializer. This usage is deprecated. [Example: struct … Read more

C++ check if statement can be evaluated constexpr

Here’s another solution, which is more generic (applicable to any expression, without defining a separate template each time). This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20. The idea is, the overload that returns true is selected when … Read more

Is constexpr really needed?

int vec[s_maxSize]; is actually illegal in the second example, so that is not possible to do in C++. But your first example is perfectly legal C++0x. So there’s your answer. You can’t actually do what you propose in C++. It can only be done in C++0x with constexpr. I would also like to point out, … Read more

When and why would you use static with constexpr?

constexpr variables are not compile-time values A value is immutable and does not occupy storage (it has no address), however objects declared as constexpr can be mutable and do occupy storage (under the as-if rule). Mutability Most objects declared as constexpr are immutable, but it is possible to define a constexpr object that is (partially) … Read more