gpt4 book ai didi

c++ - 为什么 decltype(a, b) 被评估为引用?

转载 作者:IT老高 更新时间:2023-10-28 22:34:58 26 4
gpt4 key购买 nike

据此answer , ref 应该是一个 int

但由于某种原因,它在 gcc 和 MSVC2015 中都被评估为 int&,而 decltype(b) 被正确评估为 int .为什么会这样?

int a = 1, b = 2;
decltype(a, b) ref; // ref is int&
decltype(b) var; // var is int

最佳答案

a, b 是一个表达式。根据表达式的decltype规则,如果表达式的结果是一个左值,类型将被推导出为T&

7.1.6.2/4 Simple type specifiers [dcl.type.simple]
For an expression e, the type denoted by decltype(e) is defined as follows:

  • if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), 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.

e命名实体的类型”e的类型”的混淆部分em> 用例子很容易理解:

如果某个实体 e 被声明为 int& e = x;,那么稍后,在表达式 e , e的类型是inte命名的实体的类型是 int&。简而言之,e 的类型删除了引用限定符。

关于c++ - 为什么 decltype(a, b) 被评估为引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38041494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com