`const int a = 1;` is `a` a constant expression, if `a` has automatic storage duration

前端 未结 1 1752
名媛妹妹
名媛妹妹 2021-01-07 01:18

N4527 5.20[expr.const]p2

A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of

相关标签:
1条回答
  • 2021-01-07 01:21

    a can be a prvalue core constant expression, but not a glvalue core constant expression, nor should that be possible. You already found the wording in the standard, so perhaps it's better to explain why the rules are what they are.

    void foo(){
        const int a = 1;//a has automatic storage duration
        static constexpr const int &ra = a;// cannot possibly be valid
    }
    

    This cannot be valid because it would require the address of a to be known before foo gets called, before there is any a.

    Your int b[a]{}; is fine, because it's using a as a prvalue core constant expression: it doesn't care where a is stored, it merely cares what value it has.

    0 讨论(0)
提交回复
热议问题