我希望找出变量的类型(变量由模板参数给出,因此我不知道它是什么)。
#include <iostream>
#include <typeinfo>
int main()
{
double test;
std::cout << typeid(test).name() << std::endl;
}
但是代码只输出: $./test
d
但我需要的是double类型。
关键在于,我不知道要期望哪种类型,但是我必须在子程序中写出它,并进行编译。所以使用d并不是个好主意。
std::type_info
对象。当然,并不是所有编译器都已经实现了标准的每一个细节,而且name()
返回的是某些实现定义的内容。 - David Thornley