如何找出变量的类型
How to find out the type of a variable?
我想找出一个变量的类型(变量是由模板参数给出的,所以我不知道它是什么)。
#include <iostream>
#include <typeinfo>
int main()
{
double test;
std::cout << typeid(test).name() << std::endl;
}
但是代码只发出:美元。/测试
d但是我需要double。
关键是,我不知道该期待哪种类型,但我必须在必须编译的子程序中编写它。所以d是个坏主意
如果您知道必须支持的类型列表,您可以编写自己的函数来完成此操作:
template <typename T>
void printtype()
{
if (typeid(T) == typeid(double))
std::cout << "double";
else if (typeid(T) == typeid(int))
std::cout << "int";
}
注意,由于函数没有类型为T的参数,它必须始终显式地声明类型:
printtype<double>()
当然,类型可以是参数类型:
printtype<U>()
在GNU ABI中,有一个帮助程序来请求typeid
的name()
<子>子>
免责声明如果不是很明显,当然 GNU ABI只支持从GNU ABI中提取名称(甚至可能不支持大量不同的版本)。
#include <cxxabi.h>
#include <stdlib.h>
#include <string>
template <typename T> std::string nameofType(const T& v)
{
int status;
char *realname = abi::__cxa_demangle(typeid(v).name(), 0, 0, &status);
std::string name(realname? realname : "????");
free(realname);
return name;
}
你可以尝试使用该表达式作为参数在模板中强制错误,编译器错误消息将具有你所寻找的类型。
例如,使用GCC:#include <map>
#include <string>
template<typename T> void ErrorType(T &t)
{
char x[sizeof(t)==0 ? 1 : -1];
}
template<typename T> void ErrorType(const T &t)
{
char x[sizeof(t)==0 ? 1 : -1];
}
int main()
{
double d = 3;
const double cd = 3;
ErrorType(d);
ErrorType(cd);
ErrorType(3);
std::map<std::string, int> x;
ErrorType(x.begin());
}
$ g++ -c test.cpp
test.cpp: In function ‘void ErrorType(T&) [with T = double]’:
test.cpp:17:20: instantiated from here
test.cpp:6:14: error: size of array is negative
test.cpp: In function ‘void ErrorType(const T&) [with T = double]’:
test.cpp:18:21: instantiated from here
test.cpp:10:14: error: size of array is negative
test.cpp: In function ‘void ErrorType(const T&) [with T = int]’:
test.cpp:19:20: instantiated from here
test.cpp:10:14: error: size of array is negative
test.cpp: In function ‘void ErrorType(const T&) [with T = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, int> >]’:
test.cpp:22:28: instantiated from here
test.cpp:10:14: error: size of array is negative
因此倾倒的类型为double
、int
和std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, int> >
。第一个重载是非const的,这意味着表达式是非const的左值。
需要sizeof(t)==0
的技巧来使整个表达式依赖于模板参数,并将错误延迟到实例化。当然,错误本身(数组的大小为负)是没有意义的。
如果你正在使用c++ 11,你可以改进:
#include <map>
#include <string>
template<typename T> void ErrorType(T &&t)
{
static_assert(sizeof(t)==0, "Reporting type name");
}
int main()
{
double d = 3;
const double cd = 3;
ErrorType(d);
ErrorType(cd);
ErrorType(3);
std::map<std::string, int> x;
ErrorType(x.begin());
}
$ g++ -c test.cpp -std=gnu++0x
test.cpp: In function ‘void ErrorType(T&&) [with T = double&]’:
test.cpp:15:20: instantiated from here
test.cpp:6:9: error: static assertion failed: "Reporting type name"
test.cpp: In function ‘void ErrorType(T&&) [with T = const double&]’:
test.cpp:16:21: instantiated from here
test.cpp:6:9: error: static assertion failed: "Reporting type name"
test.cpp: In function ‘void ErrorType(T&&) [with T = int]’:
test.cpp:17:20: instantiated from here
test.cpp:6:9: error: static assertion failed: "Reporting type name"
test.cpp: In function ‘void ErrorType(T&&) [with T = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, int> >]’:
test.cpp:20:28: instantiated from here
test.cpp:6:9: error: static assertion failed: "Reporting type name"
作为额外的奖励,它可以区分const的左值和右值。第一个是double
的l值,第二个是const double
的l值,另外两个是r值。
注意:type_info的成员名返回的字符串取决于编译器和库的具体实现。事实并非如此必须是具有典型类型名称的简单字符串,如用于产生此输出的编译器。
编译器在this函数中调用type_info::name返回的结果例如,我们的编译器生成了易于理解的名称但这不是必需的:编译器可能只是返回任何字符串。
来源:http://www.cplusplus.com/doc/tutorial/typecasting/
我的GCC产生了混乱的名字。例如,d表示double
, i表示int
, c表示char
, St6vectorIiSaIiEE表示std::vector<int>
来自GCC的typeinfo:
/** Returns an @e implementation-defined byte string; this is not
* portable between compilers! */
const char* name() const
{ return __name[0] == '*' ? __name + 1 : __name; }
——编辑——
你不能在不同的编译器中"重复地"得到这个名字。如果你想做到这一点,你必须像John gordon在他的帖子中描述的那样硬编码。
- 从模板创建通用打印函数,以打印基元数据类型变量的值
- 指针数据类型变量如何包含对象?
- 将 static_cast<int>(-15) 分配给静态常量字符类型变量
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- 如何为结构类型变量赋值
- 如何将字符类型变量附加到字符串中?
- 共享或私有 openmp 代码中的结构化类型变量
- 有没有办法在 c++ 中制作类型变量?
- 从其他类声明类类型变量会给出 C2143 错误(在"*"之前缺少";")
- 如何检查类中定义的枚举类型变量是否被分配给?
- 将子类实例保存在父类型变量中并通过父变量使用 Child 函数?
- 是否可以在没有结构的情况下创建新的类型变量?
- 为什么类型变量;不调用默认 CTR
- 如何使用CIN将输入整数值获取到char类型变量中
- 如何为 char** 类型变量提供适当的值,以便将其分配给字符串变量
- 在C++标准中哪里说必须初始化 const 内置类型变量的定义
- 为什么可以将其他类型变量用作C 中常量参考参数参数的参数
- Eclipse CDT 打开声明的自动类型变量
- 如何计算长长类型变量中的位数
- 将自动类型变量初始化为零.这种类型安全吗?