c++ - 提取模板参数的值

标签 c++ templates

我有这门课:

#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/

相关文章:

c++ - 将我的对象声明为 extern 会出现 "not declared in this scope"错误

c++ - 线程安全有界队列在 Boost 1.54 中挂起

c++ - 非类型模板参数

c++ - 模板和继承过滤

C++ 模板 : allocator for template container

c++ - 打印时间(秒)

c++ - 负分母让GMP崩溃?

c++ - 将编译时已知函数参数转换为 std::integral_constant 的有效方法

c++ - 访问模板类型实例成员

c++ - 根据 C++ 中的用户输入更改模板类型