9

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'm able to 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.

Columbo
  • 57,033
  • 7
  • 145
  • 194
amin
  • 2,844
  • 3
  • 25
  • 51
  • 5
    Possible duplicate of [Difference between \`constexpr\` and \`const\`](http://stackoverflow.com/questions/14116003/difference-between-constexpr-and-const) – Humam Helfawi Mar 29 '16 at 10:50
  • After reading that question, i can't still figure out the answer to my question :-) – amin Mar 29 '16 at 10:52
  • @HumamHelfawi i want know what difference does it make when the constexpr is static. that is a little bit different. – amin Mar 29 '16 at 10:54

1 Answers1

8

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

an lvalue-to-rvalue conversion (4.1) unless it is applied to

  • a non-volatile glvalue of integral or enumeration type that refers to a complete non-volatile const object with a preceding initialization, initialized with a constant expression, or […]
  • a non-volatile glvalue that refers to a non-volatile object defined with constexpr, or that refers to a non-mutable sub-object of such an object, or […]
Columbo
  • 57,033
  • 7
  • 145
  • 194