Static const vs constexpr-member fields in C ++ 11/14?

What is the difference between these member variables:

struct my_class { static const int i = 0; static constexpr int j = 0; }; 

If my understanding is correct, I can use both i and j as compile-time constants. That is, both std::array<int, my_class::i> and std::array<int,my_class::j> should work.

+8
c ++ c ++ 11 templates c ++ 14 constexpr
source share
1 answer

There are no differences for members of an integral type or an enumeration type (as in your example). For all other types, constant expressions require constexpr :

lvalue-to-rvalue conversion (4.1) if it does not apply to

  • non-volatile glvalue of an integral or enumerated type that refers to a complete non-volatile const object with previous initialization, initialized with a constant expression, or [...]
  • unstable value of glvalue, which refers to a non-volatile object defined using constexpr , or refers to an constexpr sub-object of such an object or [...]
+7
source share

All Articles