不应该decltype触发器编译其参数?

时间:2022-08-26 18:52:25

So I'm perplexed as to how this works. Given:

所以我很困惑这是如何工作的。鉴于:

template <typename T>
int foo(T t) { t.foo(); }

It seems like this call should fail:

看起来这个调用应该失败:

decltype(foo(int{ 13 })) fail = 42;

cout << fail << endl;

Instead it just prints:

相反它只是打印:

42

It works this way on all the compilers I have access to. Is this correct behavior? I request a quote from the C++ Standard.

它以我可访问的所有编译器的方式工作。这是正确的行为吗?我要求C ++标准引用。

2 个解决方案

#1


17  

In [dcl.spec] :

在[dcl.spec]中:

For an expression e, the type denoted by decltype(e) is defined as follows:

对于表达式e,由decltype(e)表示的类型定义如下:

if e is an unparenthesized id-expression naming an lvalue or reference introduced from the identifier-list of a decomposition declaration, decltype(e) is the referenced type as given in the specification of the decomposition declaration ([dcl.decomp]);

如果e是未标记的id-expression,命名从分解声明的identifier-list引入的左值或引用,则decltype(e)是在分解声明的规范([dcl.decomp])中给出的引用类型;

otherwise, if e is an unparenthesized id-expression or an unparenthesized class member access ([expr.ref]), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed;

否则,如果e是未表示的id-expression或未表示隐式的类成员访问([expr.ref]),则decltype(e)是由e命名的实体的类型。如果没有这样的实体,或者如果e命名一组重载函数,则该程序是不正确的;

otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;

否则,如果e是x值,则decltype(e)是T &&,其中T是e的类型;

otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;

否则,如果e是左值,则decltype(e)是T&,其中T是e的类型;

otherwise, decltype(e) is the type of e.

否则,decltype(e)是e的类型。

The operand of the decltype specifier is an unevaluated operand (Clause [expr]).

decltype说明符的操作数是未评估的操作数(Clause [expr])。

(Emphasis mine)

So your foo(int{ 13 }) is never evaluated.

所以永远不会评估你的foo(int {13})。

#2


5  

Expressions in decltype are defined by the standard to not be evaluated, they are only parsed to get the type of the expression.

decltype中的表达式由标准定义为不被评估,它们仅被解析以获得表达式的类型。

#1


17  

In [dcl.spec] :

在[dcl.spec]中:

For an expression e, the type denoted by decltype(e) is defined as follows:

对于表达式e,由decltype(e)表示的类型定义如下:

if e is an unparenthesized id-expression naming an lvalue or reference introduced from the identifier-list of a decomposition declaration, decltype(e) is the referenced type as given in the specification of the decomposition declaration ([dcl.decomp]);

如果e是未标记的id-expression,命名从分解声明的identifier-list引入的左值或引用,则decltype(e)是在分解声明的规范([dcl.decomp])中给出的引用类型;

otherwise, if e is an unparenthesized id-expression or an unparenthesized class member access ([expr.ref]), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed;

否则,如果e是未表示的id-expression或未表示隐式的类成员访问([expr.ref]),则decltype(e)是由e命名的实体的类型。如果没有这样的实体,或者如果e命名一组重载函数,则该程序是不正确的;

otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;

否则,如果e是x值,则decltype(e)是T &&,其中T是e的类型;

otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;

否则,如果e是左值,则decltype(e)是T&,其中T是e的类型;

otherwise, decltype(e) is the type of e.

否则,decltype(e)是e的类型。

The operand of the decltype specifier is an unevaluated operand (Clause [expr]).

decltype说明符的操作数是未评估的操作数(Clause [expr])。

(Emphasis mine)

So your foo(int{ 13 }) is never evaluated.

所以永远不会评估你的foo(int {13})。

#2


5  

Expressions in decltype are defined by the standard to not be evaluated, they are only parsed to get the type of the expression.

decltype中的表达式由标准定义为不被评估,它们仅被解析以获得表达式的类型。