不应该 decltype 触发其参数的编译吗? [英] Shouldn't decltype Trigger Compilation of its Argument?

查看:25
本文介绍了不应该 decltype 触发其参数的编译吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

取而代之的是它只是打印:

42

它在我可以访问的所有编译器上都是这样工作的.这是正确的行为吗?我向 C++ 标准请求报价.

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

推荐答案

[dcl.spec] :

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

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

如果 e 是一个无括号的 id 表达式,它命名从分解的标识符列表中引入的左值或引用声明,decltype(e) 是引用类型,如在分解声明的规范([dcl.decomp]);

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 表达式或无括号的类成员访问 ([expr.ref]),则 decltype(e) 是e 命名的实体的类型.如果没有这样的实体,或者如果 e命名一组重载函数,程序格式错误;

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 是 xvalue,则 decltype(e) 是 T&&,其中 T 是 e 的类型;

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

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

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

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

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

decltype 说明符的操作数是未计算的操作数(条款[expr]).

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

(强调我的)

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

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

这篇关于不应该 decltype 触发其参数的编译吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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