关于在需要常量表达式的上下文中使用的glvalue常量表达式的问题 [英] The question about a glvalue constant expression used in a context that requires a constant expression

查看:67
本文介绍了关于在需要常量表达式的上下文中使用的glvalue常量表达式的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
constexpr int func2(int const& id){
    return id;
}
template<int v>
struct Test{

};
int main(){
   const int v = 0;
   Test<func2(v)> c;
}

考虑上面的代码,我只是不明白为什么代码格式正确.我的观点是,当evalute表达式 func2 时,名称 v 被用作glvalue.code>,因为 func2 的参数是引用类型,所以 v 需要绑定到id表达式 id .根据glvalue常量表达式的要求,在此引用了这些内容.

Consider the above code,I just don't understand why the code is well-formed.My pointview is that the name v is used as a glvalue when evalute expression func2,becuase the parameter of func2 is of reference type,the v need to be bound to the id-expression id.So we look at the requirement of a glvalue constant expression,here are quotes about that.

常量表达式可以是glvalue核心常量表达式,指的是作为常量表达式(定义如下)的允许结果的实体,也可以是prvalue核心常量表达式,其值满足以下约束.

A constant expression is either a glvalue core constant expression that refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value satisfies the following constraints.

我们忽略prvalue的情况,因为这里 v 用作glvalue.

We ignore the case of prvalue,because here v is used as a glvalue.

如果实体是具有静态存储持续时间的对象,它不是临时对象或者其值满足上述约束的对象,则它是常量表达式的允许结果,或者是一个功能.

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.

在我的程序部分中, const int v = 0; 没有静态存储持续时间,只是具有自动存储持续时间.因此,在计算表达式 func2(v)来确定它是否是一个常量表达式.首先, v 必须是glvalue核心常量表达式,它引用一个实体,该实体是常量表达式的允许结果,因此,为什么程序在这里格式正确?如果我丢失任何重要的报价,请纠正我.

In my program portion,The const int v = 0; does not have static storage duration,it just has automatic storage duration.So when evaluting the expression func2(v) to determine whether it is a constant expression,Firstly,the v must be a glvalue core constant expression that refers to an entity that is a permitted result of a constant expression,therefore,why the program is well-formed here?If I lose any important quote,Please correct me.

推荐答案

我尝试回答这个问题.为什么 func2(v)是一个常量表达式,因为对于表达式func2(v),评估此后缀表达式时,在将要评估以下表达式之一:"列表中,不要求 v 必须是glvalue常量表达式.潜在核心常量表达式中的表达式将是glvalue常量表达式,只要求该表达式不违反列出的要求.因此,让我们继续,在初始化参数时,这是另一条规则:

I try to answer this question.why the func2(v) is a constant expression,Becuase For expression func2(v),when evaluting this postfix-expression,there's no requirement that v must be a glvalue constant expression in the list of "would evaluate one of the following expressions:",Even,these rules does not mandate that the one expression within a potentially core constant expression would be a glvalue constant expression,only require the expression does not violate the listed requirement.So let's continue,When initialization of the parameter,It's another rule here:

完整表达式为:

  • [...]
  • init-declarator 或mem初始化器,包括初始化器的组成表达式
  • [...]
  • an init-declarator or a mem-initializer, including the constituent expressions of the initializer

因此,当评估此完整表达式时,它仅不违反这些列出的条件,因此 func2(v)将被评估为一个常量表达式,因此,让我们看一下这些规则:

So,when evalute this full-expression,it only does not violate these listed condition,then the func2(v) would be evaluted as a constant expression,So let's look at these rules:

一个id表达式,它引用引用类型的变量或数据成员,除非引用具有前面的初始化和

an id-expression that refers to a variable or data member of reference type unless the reference has a preceding initialization and either

  • 使用常量表达式或
  • 进行初始化
  • 其寿命始于e的评估;

对于id表达式 id ,其先前的初始化是相应的参数,因为此规则:

For id-expression id,its preceding initialization is the corresponding argument,Because of this rule:

当调用一个函数时,每个参数([dcl.fct])应使用其相应的参数进行初始化([dcl.init],[class.copy],[class.ctor]).

When a function is called, each parameter ([dcl.fct]) shall be initialized ([dcl.init], [class.copy], [class.ctor]) with its corresponding argument.

因此,第一个条件为true.用常量表达式初始化"为false,条件其寿命始于e的求值"为true.结论中,表达式 func2(v)确实是一个常量表达式

So,the first condition is true.And "it is initialized with a constant expression" is false,the condition "its lifetime began within the evaluation of e" is true.In conlusion,the expression func2(v) indeed a constant expression

这篇关于关于在需要常量表达式的上下文中使用的glvalue常量表达式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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