4

I am trying to define a constructor for an explicitly specialized class template outside the class definition, as so:

template <typename T>
struct x;

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

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

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

But it seems to be an error. Comeau says: error: "x<int>::x()" is not an entity that can be explicitly specialized, even though the complete class is what being specialized.

What's the issue here?

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 に答える