4

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

template <typename T>
struct x;

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

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

template <>    // Error
x<int>::x() {
}

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

这里有什么问题?

4

1 回答 1

13

不要指定template<>定义:

template <typename T>
struct x;

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

inline x<int>::x(){}

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

于 2010-10-14T17:51:27.043 回答