获取C++变量类型的方法

正文

直接上代码

#include <type_traits>
#include <typeinfo>
#include <memory>
#include <string>
#include <cstdlib>
#include <iostream>
#ifndef _MSC_VER
#include <cxxabi.h>
#endif

using namespace std;

template <class T>
string type_name()
{
    typedef typename remove_reference<T>::type TR;
    unique_ptr<char, void (*)(void *)> own(
#ifndef _MSC_VER
        abi::__cxa_demangle(typeid(TR).name(), nullptr, nullptr, nullptr),
#else
        nullptr,
#endif
        free);
    string r = own != nullptr ? own.get() : typeid(TR).name();
    if (is_const<TR>::value)
        r += " const";
    if (is_volatile<TR>::value)
        r += " volatile";
    if (is_lvalue_reference<T>::value)
        r += "&";
    else if (is_rvalue_reference<T>::value)
        r += "&&";
    return r;
}

int main()
{
    int a = 1;
    const int &b = a;
    cout << type_name<decltype(b)>() << endl;
}

  定义了一个模板函数type_name(),可以对传入的模板类型T进行类型判断,结合指针、左值/右值引用、常类型,准确得出变量的类型。在调用该函数时使用了decltype关键字,传入待确定的变量,然后输出变量类型。
  以上运行的结果为

int const&
  • 如果要输出int指针的类型:
int main()
{
    int a = 1;
    int* b = &a;
    cout << type_name<decltype(b)>() << endl;
}

结果为:

int*

  可以看出该函数得出的结果是非常准确而又精简的。

与传统方法的对比

  传统输出C++类型的方法是使用typeid(var).name(),其弊端在于输出的结果不完整或者过于冗长。
例如:
1. 传统方法打印int类型

#include <iostream>

int main()
{
  int a = 10;
  std::cout << typeid(a).name() << std::endl;
}

运行结果为:

i

只得到了int的缩写i

2. 传统方法打印string类型

#include <iostream>
#include <string>

int main()
{
  std::string a = "abc";
  std::cout << typeid(a).name() << std::endl;
}

运行结果为:

NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

过于冗长,不便于看出类型名称。

3. 传统方法打印引用类型

#include <iostream>

int main()
{
  int a0 = 23;
  int& a = a0;
  std::cout << typeid(a).name() << std::endl;
}

运行结果为:

i

并没有准确地输出我们想要的结果。

总结

  使用了稍微复杂的模板函数,可以准确输出C++的变量类型,而非大多数人提到的type_id(var).name()打印出来的结果有各种不恰当的地方。

  • 10
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值