作为主题,您可以在 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 -t
说Widget
.
如果能就这个问题得到一些帮助,我将不胜感激。
最佳答案
每C++ Reference (强调我的):
1) Refers to a
std::type_info
object representing the type type. If type is a reference type, the result refers to astd::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/