Explicit instantiation definition: constructor template of class template -- is it possible? (Clang versus GCC)

155 Views Asked by At

Consider the following class template:

template <typename> 
struct S 
{
    template <typename T>
    void f(T) { /* ... */ }
};

It is possible to provide a explicit instantiation definition (or declaration via extern template) of both S itself and S::f:

template struct S<int>;
template void S<int>::f<int>(int);

But what if instead of a regular member function template we had the exact same situation with a constructor template?

template <typename> 
struct S 
{
    template <typename T>
    S(T) { /* ... */ }
};

According to Clang, it is still possible to provide an explicit instantiation definition for S itself, but not for the constructor:

template struct S<int>;       // OK
template S<int>::S<int>(int); // ERROR (!)
error: out-of-line constructor for 'S' cannot have template arguments
template S<int>::S<int>(int);
                 ^~~~~~

On the other hand, GCC seems to accept both definitions. See this live example on godbolt.org.


Is a definition such as template S<int>::S<int>(int) legal, or is Clang correct in rejecting it?

0

There are 0 best solutions below