const int a = 1;如果a具有自动存储持续时间,则a是一个常数表达式 [英] `const int a = 1;` is `a` a constant expression, if `a` has automatic storage duration

查看:111
本文介绍了const int a = 1;如果a具有自动存储持续时间,则a是一个常数表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N4527 5.20 [expr.const] p2

N4527 5.20[expr.const]p2

条件表达式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:

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

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

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

(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)—引用字符串文字(2.13.5)的子对象的非易失性glvalue,或者

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

(2.7.3)—非易失性glvalue,它引用用constexpr定义的非易失性对象,或引用 到此类对象的非可变子对象,或

(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)-文字类型的非易失性glvalue,指的是生命周期开始的非易失性对象 在e的评估之内;

(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

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:

-每个引用类型的非静态数据成员都引用一个实体,该实体是常量表达式的允许结果,并且

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

-如果对象或子对象是指针类型,则它包含具有静态存储持续时间的对象的地址,该对象末尾的地址(5.7),函数的地址或空指针值

— 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]{};
}

int b[a]{};中,a是id表达式,a是左值核心常量表达式. a是常量表达式吗?

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

这是对的澄清>是glvalue整数常量表达式一个常量表达式?

推荐答案

a可以是prvalue核心常量表达式,但不能是glvalue核心常量表达式,也不应该这样.您已经在标准中找到了措辞,所以也许最好解释一下规则为何如此.

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
}

这是无效的,因为它将要求在调用foo之前,在 任何a之前知道a的地址.

This cannot be valid because it would require the address of a to be known before foo gets called, before there is any a.

您的int b[a]{};很好,因为它使用a作为prvalue核心常量表达式:它不在乎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.

这篇关于const int a = 1;如果a具有自动存储持续时间,则a是一个常数表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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