我有这门课:
#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;
。
关于c++ - 提取模板参数的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48326286/