views:

60

answers:

1

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?

+8  A: 

Don't specify template<> for the definition:

template <typename T>
struct x;

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

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

Edit: The constructor definition isn't a specialization, so template<> is unnecessary. It's the definition of the constructor of a specialization. So, you just need to specify the type like for any other non-template class.

Steve M