如何在C 中检查确切的类型信息(具有CV-REF-POIRT特征)

How to check the exact type info (with cv-ref-pointer traits) in C++?

本文关键字:信息 类型 具有 CV-REF-POIRT 特征 检查      更新时间:2023-10-16
#include <iostream>
using namespace std;
int main()
{
    cout << typeid(int).name() << endl;
    cout << typeid(int&).name() << endl;
    cout << typeid(int&&).name() << endl;
    cout << typeid(const int).name() << endl;
    cout << typeid(const int&).name() << endl;
    cout << typeid(const int&&).name() << endl;
}

我认为输出应该是:

int
int&
int&&
const int
const int&
const int&&

但是,实际输出是:

int
int
int
int
int
int

Clang&amp;VC 以相同的方式做。

是否有可靠的方法来检查C ?

中的确切类型名称(具有CV-REF-POINTS特征(

请注意,当您将参考类型传递给TypeID操作员时,结果std::type_info对象表示引用类型。

将忽略CV-Qualifier

1(指的是代表类型typestd::type_info对象。如果type是参考类型,则结果是指代表引用类型的std::type_info对象

在所有情况下,CV-Qualifier都被TypeID忽略(即typeid(const T) == typeid(T)(

并注意std::type_info::name返回的是实现定义。

您可以从有效的现代C (Scott Meyers(项目4:知道如何查看推论类型,以在编译时获取类型名称。例如

template <typename>
struct TD;

然后将其用作

TD<int> td;

您会收到一些提及类型名称的消息,例如

error: implicit instantiation of undefined template 'TD<int>'

live(clang(