怎么知道一个表达式是啥类型的值?

typeid(x).name()可以返回x的类型,但是并不能区分三种值(详见左值、右值、将亡值 | 左值引用、右值引用)。如:

// typeid的局限性
int a;
// class type_info
std::cout << typeid(a).name() << std::endl;
std::cout << typeid(decltype(a)).name() << std::endl;

int &b = a;
// type_info
std::cout << typeid(b).name() << std::endl;
std::cout << typeid(decltype(b)).name() << std::endl;

// 比较两个类型是否相等
std::cout << std::boolalpha;
std::cout << (typeid(a) == typeid(b)) << std::endl;

输出结果:

int
int
int
int
true

为了能区分不同的值,利用函数签名__FUNCSIG__中关于函数模板的信息来反映调用函数时规定的类型参数。使用时通过decltype(x)函数返回表达式x的类型,通过模板调用语句调用type_to_string()即可返回指示表达式值的类型的字符串:

template<typename T>
string type_to_string() {
#if defined(_MSC_VER)
    std::string type_name{ __FUNCSIG__ };
    // __FUNCSIG__可以获取当前函数状态,返回这样的字符串:class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl type_to_string<int&>(void)
    // 截取最后的<>中的字符串,即模板调用语句中表达式的值的类型:
    // &作为限定符 : 左值
    // &&作为限定符 : 将亡值
    // 无限定符 : 纯右值
    auto start_pos = type_name.find_first_of('<',
        std::string(typeid(std::string).name()).size()) + 1;
    auto end_pos = type_name.find_last_of('>');
    return type_name.substr(start_pos, (end_pos - start_pos));
#elif defined(__clang__)
    std::string type_name{ __PRETTY_FUNCTION__ };
    auto start_pos = type_name.find_first_of('=') + 2;
    auto end_pos = type_name.find_first_of(']', start_pos);
    return type_name.substr(start_pos, (end_pos - start_pos));

#elif defined(__GNUC__)
    std::string type_name{ __PRETTY_FUNCTION__ };
    // std::__cxx11::string type_to_string() [with T = int&; std::__cxx11::string = std::__cxx11::basic_string<char>]
    auto start_pos = type_name.find_first_of('=') + 2;
    auto end_pos = type_name.find_first_of(';', start_pos);
    return type_name.substr(start_pos, (end_pos - start_pos));
#endif
}

使用例:

int a = 10;
cout << "The type of a : " << type_to_string<decltype(a)>() << endl;		//打印a的类型
cout << "The value type of a : " << type_to_string<decltype((a))>() << endl;		//再套一层括号,返回a的值的类型
cout << "The value type of std::move(a) : " << type_to_string<decltype((move(a)))>() << endl;

输出:

The type of a : int
The value type of a : int&
The value type of std::move(a) : int&&

参考文章

[1]C++(二):如何确定表达式的值类型?

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值