Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding constant expression

I'm trying to understand the constant expression concept (from c++reference):

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 isn't the S::c a constant expression untill we define it. It was declared as a static const data member though...

like image 699
stella Avatar asked Mar 15 '23 14:03

stella


1 Answers

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

A conditional-expression is a core constant expression unless it involves one of the following as a potentially evaluated subexpression (3.2), but subexpressions of logical AND (5.14), logical OR (5.15), and conditional (5.16) operations that are not evaluated are not considered [ Note: An overloaded operator invokes a function. — end note ]:

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

    • a glvalue of integral or enumeration type that refers to a non-volatile const object with a preceding initialization, initialized with a constant expression

There is no preceding initialization of S::c in your definition of d.

Edit: why this applies:

  • 5.1.1/8: S::c is an lvalue.
  • 3.10/1: a glvalue is an lvalue or xvalue.
  • 5/8: specifies that lvalue-to-rvalue conversion happens whenever an operator that expects a prvalue is used.
  • Proving that multiplication expects a prvalue is hurting my head. It is implied in many places, but I haven't found anywhere it is said explicitly.
like image 86
o11c Avatar answered Mar 23 '23 07:03

o11c