glvalue 整数常量表达式是常量表达式吗?

Is a glvalue integral constant expression a constant expression?

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:
        ;
    }
}

问题1:a整数常量表达式吗?

问题2:a常量表达式吗?

问题3:泛左值整数常量表达式是常量表达式吗?

问题 4:

如果问题3的答案是肯定的, 如果对象具有自动存储期限,这是否与 5.20 p3 冲突?

Is a an integral constant expression?

在以下情况下:

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

是的,a是整型常量表达式。从你的第一句话开始:

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' 是一个整数类型,在你的情况下它会被隐式转换为纯右值,所以现在 a 是一个核心常量表达式吗?是的,如果我们回到定义什么不是核心常量表达式的第 2 段:

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

它有以下子句:

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

但有以下例外:

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

适用于 a,因为它是非易失性的,const 并使用常量表达式初始化。


Is a a constant expression?

在与上面相同的上下文中,是的,因为我们从上面的引用中可以看出它是一个核心常量表达式。


Is a glvalue integral constant expression a constant expression?

不,为了使其成为整型常量表达式,必须将其转换为纯右值,因此不能是泛右值。