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

自古美人都是妖i 提交于 2019-12-22 11:29:54

问题


N4527 5.20[expr.const]p2

A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions:

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

(2.7.1) — a non-volatile glvalue of integral or enumeration type that refers to a complete non-volatile const object with a preceding initialization, initialized with a constant expression, or

(2.7.2) — a non-volatile glvalue that refers to a subobject of a string literal (2.13.5), or

(2.7.3) — a non-volatile glvalue that refers to a non-volatile object defined with constexpr, or that refers to a non-mutable sub-object of such an object, or

(2.7.4) — a non-volatile glvalue of literal type that refers to a non-volatile object whose lifetime began within the evaluation of e;

5.20[expr.const]p5

A constant expression is either a glvalue core constant expression whose value refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value is an object where, for that object and its subobjects:

— each non-static data member of reference type refers to an entity that is a permitted result of a constant expression, and

— if the object or subobject is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object (5.7), the address of a function, or a null pointer value.

An entity is a permitted result of a constant expression if it is an object with static storage duration that is either not a temporary object or is a temporary object whose value satisfies the above constraints, or it is a function.

void foo(){
    const int a = 1;//a has automatic storage duration
    int b[a]{};
}

In int b[a]{};, a is an id-expression, a is a lvalue core constant expression. Is a a constant expression?


This is a clarification of Is a glvalue integral constant expression a constant expression?


回答1:


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.



来源:https://stackoverflow.com/questions/31537359/const-int-a-1-is-a-a-constant-expression-if-a-has-automatic-storage-du

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!