Skip to content

[expr.const]/2.11 CWG 2186: "Preceding" is unclear #2307

@xskxzr

Description

@xskxzr

[expr.const]/2.11:

An expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine, would evaluate one of the following expressions:

  • ...
  • an id-expression that refers to a variable or data member of reference type unless the reference has a preceding initialization and either
    • it is initialized with a constant expression or
    • its lifetime began within the evaluation of e;

It's unclear what "preceding" means. It may either mean the initialization happens before the evaluation of the id-expression, or mean the initialization occurs before the id-expression in lexical order.

According to this example in cppreference:

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

It seems "preceding" means the latter, but consider:

constexpr int f(const int &i)
{
    return i;
}

constexpr int j = f(0);

Here f(0) is obviously a constant expression, but the initialization of i does not occur before return i; in lexical order.

I think it makes more sense to say:

  • an id-expression that refers to a variable or data member of reference type unless either
    • the reference has a preceding initialization, initialized with a constant expression or
    • its lifetime began within the evaluation of e;

Metadata

Metadata

Assignees

No one assigned

    Labels

    cwgIssue must be reviewed by CWG.not-editorialIssue is not deemed editorial; the editorial issue is kept open for tracking.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions