专用类模板的类构造函数定义

uj2*_*uj2 4 c++ templates template-specialization

我正在尝试为类定义之外的显式专用类模板定义构造函数,如下所示:

template <typename T>
struct x;

template <>
struct x<int> {
    inline x();

    /* This would have compiled:
    x() {
    }
    */
};

template <>    // Error
x<int>::x() {
}
Run Code Online (Sandbox Code Playgroud)

但这似乎是一个错误.Comeau说:error: "x<int>::x()" is not an entity that can be explicitly specialized尽管完整的课程是专业的.

这是什么问题?

Ste*_*e M 13

不要指定template<>定义:

template <typename T>
struct x;

template <>
struct x<int> {
  x();
};

inline x<int>::x(){}
Run Code Online (Sandbox Code Playgroud)

编辑:构造函数定义不是特化,因此template<>是不必要的.它是专业化构造函数的定义.因此,您只需要为任何其他非模板类指定类型.