glvalue整型常量表达式是常量表达式吗? [英] Is a glvalue integral constant expression a constant expression?

查看:191
本文介绍了glvalue整型常量表达式是常量表达式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N4527 5.20 [expr.const] p3

N4527 5.20 [expr.const]p3

整数常量表达式 是整数或无范围枚举类型的表达式,它隐式转换为prvalue,其中转换后的表达式是核心常量表达式.

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

5.20 [expr.const]p5

常量表达式 是glvalue核心常量表达式,其值引用的是一个常量表达式(如下定义)的允许结果的实体,或者是prvalue核心常量表达式,其 值是一个对象,其中对于该对象及其子对象:

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)-每个引用类型的非静态数据成员都引用一个实体,该实体是常量的允许结果 表达式,以及

(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)—如果对象或子对象是指针类型,则它包含具有静态存储的对象的地址 持续时间,此类对象末尾的地址(5.7),函数的地址或空指针 值.

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

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

问题4:

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

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

推荐答案

a是整数常量表达式吗?

Is a an integral constant expression?

在以下情况下:

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

是,a是整数常量表达式.从您的第一个报价开始:

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

整数常量表达式是整数或无作用域枚举类型的表达式,已隐式转换 到prvalue,其中转换后的表达式是核心常量表达式.

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,所以a现在是核心常量表达式吗?是的,如果我们回到第2段,该段定义了什么不是核心常量表达式:

'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:

条件表达式e是核心常数表达式,除非按照e的规则对e求值. 抽象机(1.9),将评估以下表达式之一

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

它具有以下子句:

从左值到右值的转换(4.1),除非将其应用于

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

,但以下情况除外:

整数或枚举类型的非易失性glvalue,它引用完整的非易失性const 具有先前初始化的对象,该对象使用常量表达式初始化,或者

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并使用常量表达式初始化.

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

a是常量表达式吗?

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.

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

Is a glvalue integral constant expression a constant expression?

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

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.

这篇关于glvalue整型常量表达式是常量表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆