Understanding Constant Expression

I am trying to understand the concept of constant expression (from C ++ link ):

struct S {
    static const int c;
};
const int d = 10 * S::c; // not a constant expression: S::c has no preceding
                         // initializer, this initialization happens after const
const int S::c = 5;      // constant initialization, guaranteed to happen first

Why there is no S::cconstant expression until we define it. It was declared as a static member of const , though ...

+4
source share
3 answers

Quoting the corresponding part of the C ++ 11 standard (draft N3337), section 5.19, paragraph 2:

, (3.2), AND (5.14), (5.15) (5.16), , [. . - ]:

  • lvalue-to-rvalue (4.1),

    • , const ,

d S::c.

: :

  • 5.1.1/8: S::c lvalue.
  • 3.10/1: glvalue lvalue xvalue.
  • 5/8: , lvalue-rvalue , , .
  • , , . , , .
+4

& hellip;

constexpr int d = 10 * S::c;
const int S::c = 5;

& hellip; S::c d. :

const int S::c = 5;
constexpr int d = 10 * S::c;
+2

++. d S::c. . d , S::c, , , .

+1

All Articles