Shouldn#39;t decltype Trigger Compilation of its Argument?(不应该 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 触发其参数的编译吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不应该 decltype 触发其参数的编译吗?
基础教程推荐
- c++ STL设置差异 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
