应该不应该decltype Trigger编译它的参数? [英] Shouldn't decltype Trigger Compilation of its Argument?
问题描述
所以我对如何工作感到困惑。给定:
模板< typename T>
int foo(T t){t.foo(); }
看起来这个调用应该失败:
decltype(foo(int {13}))fail = 42;
cout<<失败< endl;
而是只打印:
42
它以这种方式在我可以访问的所有编译器。这是正确的行为吗?
在 [dcl.spec] :
对于表达式e, (e)被定义为
如下:
如果e是未加括号的id-表达式,命名从分解的标识符列表引入的值b $ b声明,decltype(e)是分解声明([dcl.decomp])的
规范中给出的引用类型;否则为
,如果e是未加括号的id表达式或未加括号的类成员访问([expr.ref]),则decltype(e)是由e命名的实体的
类型。如果没有这样的实体,或者如果e
命名一组重载的函数,则程序是错误的;
否则,如果e是x值,decltype(e)是T&& amp;& amp;其中T是e的类型;否则,decltype(e)是e的类型。
decltype说明符的操作数是未求值的操作数
(Clause [expr])。
(强调我的)
所以你的 foo(int {13})
从不评估。
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.
In [dcl.spec] :
For an expression e, the type denoted by decltype(e) is defined as follows:
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]);
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;
otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;
otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;
otherwise, decltype(e) is the type of e.
The operand of the decltype specifier is an unevaluated operand (Clause [expr]).
(Emphasis mine)
So your foo(int{ 13 })
is never evaluated.
这篇关于应该不应该decltype Trigger编译它的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!