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&&