在c++中使用模板时,我遇到了将typename转换为字符串的问题。例如:
template <typename T>
class Matrix {
public:
Matrix() {
//my_type = string type of T. i.e. if T is char. I want my_type to be "char".
}
string my_type;
}如何将T转换为表示T是什么的字符串。
注意:我只是随便玩玩,所以请不要担心什么时候会需要这样的东西。
发布于 2010-12-20 04:46:37
如果类型是基类型之一,则在string中获取类型名称是不可能的。对于用户定义的类型,可以使用typeid(my_type).name()。您还需要#include <typeinfo> :) more info...
https://stackoverflow.com/questions/4484982
复制相似问题