Title
Where must a variable be initialized to be used in a constant expression?
Status
cd2
Section
7.7 [expr.const]
Submitter
James Kanze

Created on 2008-09-22.00:00:00 last changed 143 months ago

Messages

Date: 2009-10-15.00:00:00

[Voted into WP at October, 2009 meeting.]

Date: 2009-07-15.00:00:00

Proposed resolution (July, 2009):

Change 7.7 [expr.const] paragraph 2, bullet 4, sub-bullet 1 as follows:

  • an lvalue of effective integral type that refers to a non-volatile const variable or static data member with a preceding initialization, initialized with a constant expressions, or
Date: 2009-06-19.00:00:00

Additional note, June, 2009:

It has been suggested that the requirement that a static data member be initialized in the class definition is not actually needed but that static data members should be treated like other variable declarations -- a preceding definition with initialization should be sufficient. That is, given

    extern const int i;
    const int i = 5;
    struct S {
      static const int j;
    };
    const int S::j = 5;
    int a1[i];
    int a2[S::j];

there doesn't appear to be a good rationale for making a1 well-formed and a2 ill-formed. Some major implementations accept the declaration of a2 without error.

Date: 2009-03-15.00:00:00

Proposed resolution (March, 2009):

Change 7.7 [expr.const] paragraph 2, bullet 4, sub-bullet 1 as follows:

  • an lvalue of effective integral type that refers to a non-volatile const variable with a preceding initialization or to a non-volatile const static data member with an initialization in the class definition (11.4.9.3 [class.static.data]), in either case initialized with constant expressions, or

Date: 2009-03-23.00:00:00

7.7 [expr.const] paragraph 2 allows an lvalue-to-rvalue conversion in a constant expression if it is applied to “an lvalue of effective integral type that refers to a non-volatile const variable or static data member initialized with constant expressions.” However, this does not require, as it presumably should, that the initialization occur in the same translation unit and precede the constant expression, nor that the static data member be initialized within the member-specification of its class.

History
Date User Action Args
2010-03-29 00:00:00adminsetstatus: dr -> cd2
2009-11-08 00:00:00adminsetmessages: + msg2445
2009-11-08 00:00:00adminsetstatus: ready -> dr
2009-08-03 00:00:00adminsetmessages: + msg2155
2009-08-03 00:00:00adminsetstatus: review -> ready
2009-06-19 00:00:00adminsetmessages: + msg2039
2009-03-23 00:00:00adminsetmessages: + msg1930
2009-03-23 00:00:00adminsetstatus: open -> review
2008-09-22 00:00:00admincreate