【发布时间】:2020-08-30 13:40:49
【问题描述】:
我一直在尝试模板元编程,但在制作动态列表时遇到了一些麻烦。我试过了
#include <iostream>
template<int I>
struct Int {
};
template<class _Value, class ..._Others>
struct List {
typedef _Value Value;
typedef List<_Others...> Next;
};
template<class _Value>
struct List<_Value, void> {
typedef _Value Value;
typedef void Next;
};
template<class _List>
void PrintList() {
std::cout << typename _List::Value::I << "\n";
PrintList<typename _List::Next>();
};
template<>
void PrintList<void>() {};
int main() {
PrintList<List<Int<1>, Int<2>, Int<3>>>();
}
但是我得到了 2 个我无法弄清楚的编译错误。第一个是expected '(' before '<<' token 上的std::cout << typename _List::Value::I << "\n";。我不知道如何打印出 int 值(我也试过(typename _List::Value)::I)。
第二个错误是typedef List<_Others...> Next; 的模板参数数量错误。第一个参数不应该被限制为Value,其余的不是Others吗?
【问题讨论】:
-
这里有两个独立的编译错误,根本原因完全不同,彼此无关,会有不同的答案。请为每个 stackoverflow.com 问题提出一个问题。你能把你的问题限制在一个问题上吗?
标签: c++ list templates variadic-templates