Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top