其他分享
首页 > 其他分享> > c-glvalue整数常数表达式是常数表达式吗?

c-glvalue整数常数表达式是常数表达式吗?

作者:互联网

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:是整数常量表达式吗?

Question2:是常数吗?

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

问题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”是整数类型,在您的情况下,它将被隐式转换为prvalue,那么现在是核心常量表达式吗?是的,如果我们回到第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?

不,为了使其成为整数常量表达式,必须将其转换为prvalue,因此不能为glvalue.

标签:constant-expression,c,language-lawyer
来源: https://codeday.me/bug/20191011/1889762.html