如何在模板元编程中创建动态列表?

问题描述

我一直在尝试模板元编程,但在制作动态列表时遇到了一些麻烦。我尝试过

#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用吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)