c++ - 为什么 `typeid(T&).name()` 的输出给出为 `T` 而不是 `T&` ?

标签 c++ c++11 typeinfo

作为主题,您可以在 https://godbolt.org/z/qtjVP6 上查看相关代码.

为方便起见,代码发布如下:

#include<typeinfo>
#include<iostream>

class Widget{};

Widget someWidget;

int main()
{
    Widget&& var1 = Widget{};      // here, “&&” means rvalue reference
    auto&& var2 = var1;              // here, “&&” does not mean rvalue reference

    std::cout << typeid(var2).name() << std::endl;
}

输出:6Widget

echo 6Widget | c++filt -tWidget .

如果能就这个问题得到一些帮助,我将不胜感激。

最佳答案

C++ Reference (强调我的):

1) Refers to a std::type_info object representing the type type. If type is a reference type, the result refers to a std::type_info object representing the referenced type.

因此,对于 type = T&typeid(type) 将给出有关 T 的结果(引用已删除)。

在 IMO 中很容易理解,因为就所有目的而言,T& 类型的变量在功能上等同于 T 类型的变量。

关于c++ - 为什么 `typeid(T&).name()` 的输出给出为 `T` 而不是 `T&` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62125949/

相关文章:

c++ - 以编程方式获取上次重启的日期/时间

c++ - 如何用 C++ 编写 "meta if else if.."?

c++ - 为什么在使用 MinGW 添加 ltalloc 时会出现段错误

C++ 成员函数结果缓存实现

c++ - 获取无限循环而不是值

c++ - typeid 运算符忽略 cv 限定符背后的基本原理是什么

c++ - 关于选择 MFC CListCtrl 项

c++ - 为什么每次使用 std::random_device 和 mingw gcc4.8.1 运行都会得到相同的序列?

c++ - 为什么 std::hash 是结构而不是函数?

powershell - 在什么情况下需要当 -NoTypeInformation *不* 传递给 ConvertTo-Csv 或 Export-Csv 时发出的信息?