Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of class constructor definition for a specialized class template

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?

like image 890
uj2 Avatar asked Dec 13 '22 18:12

uj2


1 Answers

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.

like image 132
Steve M Avatar answered May 22 '23 05:05

Steve M