c++ - 模板化类定义在 VS2017 中无法编译

标签 c++ templates

<分区>

我有这个类定义(这里简化了),它用 VS2008 编译得很好。在 VS2017 中,第一个尖括号出现语法错误 C2059:

template < typename Function, typename Base, typename Specialiser = Base >
class FunctionTermBase : public Base
{
public:
    // typedef typename Function::result_type result_type;
    typedef typename Base term_type;
    typedef typename Specialiser specialiser;

protected:
    FunctionTermBase() { }

public:
    template <typename T>
    struct Specialise {
        typedef typename specialiser::Specialise<T>::type type;
    };
};

如果有人能告诉我这段代码有什么问题,我将不胜感激?

最佳答案

您必须使用 template关键字以指示以下从属名称也具有模板参数。此外,您的 typedef 中不需要 typename,因为标识符是已知的类型(它们是模板参数)。

template < typename Function, typename Base, typename Specialiser = Base >
class FunctionTermBase : public Base
{
public:
    // typedef typename Function::result_type result_type;
    typedef Base term_type;
    typedef Specialiser specialiser;

protected:
    FunctionTermBase() { }

public:
    template <typename T>
    struct Specialise {
        typedef typename specialiser::template Specialise<T>::type type;
        //          Add template here ^^^^^^^^
    };
};

关于c++ - 模板化类定义在 VS2017 中无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44271065/

相关文章:

c++ - 查找资源失败

c++ - 如何 memset 一个 bool 数组?

python - 在 Django 中删除 float 的小数部分?

c++ - 如何使用广度优先搜索确定是否可以在有向图中到达顶点

c++ - 获取临时 SDL_Rect 的地址

C++ 非类型模板参数 const char*

C++ 运算符= return *this 后的结果发生变化

c++ - 如何检查参数包中的每种类型是否唯一?

c++ - 运行时数据类型多态性

c++ - 自定义顶点属性 GLSL