gcc and explicit specialization in non-namespace scope

Viewed 738
struct X
{
    template <typename>
    struct Y;

    template <>
    struct Y<int>
    {
    };
};

This code does not compile with GCC (even GCC 10 Link) with the following error message

error: explicit specialization in non-namespace scope 'struct X'

according to cpp reference

Explicit specialization may be declared in any scope where its primary template may be defined

Also we have this defect report where the behavior was changed to allow this code to compile CWG 727

However in this question some comment

So far, this does not seem fixed and gcc dev claims that cwg727 never was approved. – Swift - Friday Pie Nov 22 '19 at 19:11

said that this defect report was not approved. Is that true? Is this a gcc bug failing to comply with the standard or not?

1 Answers

This is gcc failing to comply with the standard, but (as discussed in the linked question) there is a bug report from 2018, so hopefully it will be fixed.

As a solution to your problem, partial template specialization is working correctly, so you simply need to add a dummy template parameter.

struct X
{
    template <typename, typename=int>
    struct Y;

    template <typename DUMMY_T>
    struct Y<int, DUMMY_T>
    {
    };
};

Here it is in compiler explorer.

Related