提取模板参数的值

Extracting template arguments' value

本文关键字:参数 提取      更新时间:2023-10-16

我有这个类:

#include <iostream>
template<typename T, typename... TT>
class List
{
public:
    typedef T head;
    typedef List<TT...> next;
    enum { size = sizeof...(TT)+1 };
};

而这个主要:

#include <iostream>
#include "List.h"
using namespace std;
template <int T>
struct Int {
    enum { value = T };
};
int main() {
    typedef List<Int<1>, Int<2>, Int<3>> list;
    cout << list::template head.value << endl; // Error
    cout << list::size; // Works
    return 0;
}

错误信息:

error: expected primary-expression before '.' token
     cout << list::template head.value << endl;

我将不胜感激任何帮助..在过去的半个小时里,我一直在试图解决这个问题,这可能是一件非常愚蠢的事情,我只是无法说清楚。

head是一种类型。这意味着你不能用template消除它的歧义,也不能用.访问它。修复它没有太多可做的:

std::cout << list::head::value << std::endl;

另外,请摆脱using namespace std;