Is a glvalue integral constant expression a constant expression?

我怕爱的太早我们不能终老 提交于 2019-12-20 02:38:29

问题


N4527 5.20 [expr.const]p3

An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression.

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:

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

(5.2) — 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
    // all ok in gcc 5.1.0 and clang 3.8.0
    int b[a]{};
    static_assert(a,"");
    switch(1){
      case a:
        ;
    }
}

Question1: Is a an integral constant expression?

Question2: Is a a constant expression?

Question3: Is a glvalue integral constant expression a constant expression?

Question4:

If the answer of question 3 is yes, does this conflict with 5.20 p3 if the object has automatic storage duration?


回答1:


Is a an integral constant expression?

In the following contexts:

int b[a]{};
static_assert(a,"");
switch(1){
  case a:
    ;
}

yes, a is an integral constant expression. Starting with your first quote:

An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression.

'a' is an integral type, in your cases it will be implicitly converted to a prvalue, so now is a a core constant expression? Yes, if we go back to paragraph 2 which defines what is not a core constant expression:

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

it has the following clause:

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

with the following exception:

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

which applies to a since it is non-volatile, const and is initialized with a constant expression.


Is a a constant expression?

In the same contexts as above, yes, since we can see from the quote above it is a core constant expression.


Is a glvalue integral constant expression a constant expression?

No, in order for it to be a integral constant expression it must be converted to a prvalue and threfore can not be a glvalue.



来源:https://stackoverflow.com/questions/31527913/is-a-glvalue-integral-constant-expression-a-constant-expression

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