【发布时间】:2015-08-30 18:56:50
【问题描述】:
如何打印类型列表??
这是我尝试过的 + 我的类型列表:
template<typename ...T>
struct typeList;
template<typename H, typename ...T>
struct typeList<H, T...> {
using head = H;
using tail = typeList<T...>;
public:
static inline void print(){
std::cout << H::name << " "; // How can I get the name of H ???
typeList<T...>::print();
}
};
template<>
struct typeList<>{
static inline void print(){
std::cout << endl;
}
};
这样typeList<A,B,C,int>::print() 会给出A b C int,
A B 和 C 是一些用户定义的结构
我可以不在每个结构中添加名为name 的静态函数来执行此操作吗?
没有返回类型名称的编译时函数?
编辑:
这不是this 的重复,我在哪里提到了变量??
【问题讨论】:
-
@CaptainGiraffe
type_info对象不需要给你一个有用的名字。 -
我已将这个问题作为重复问题关闭,因为主要问题似乎是关于打印类型的名称。如果是这种情况,this 也可能会有所帮助。
-
@Jefffrey 无论如何,我的问题不是 stackoverflow.com/questions/81870/print-variable-type-in-c 的重复项
-
@OthmanBenchekroun 我知道,但是那个问题有this 的答案,它处理从变量中获取类型后如何打印类型,这确实与您在此处询问的内容重复。你也试过reading the other question我链接了吗?