6
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?

mkmostafa
  • 3,071
  • 2
  • 18
  • 47
  • how is the question different from the one you link? If there is something new to report on the issue, the answers on the other question would need to be updated as well – 463035818_is_not_an_ai May 22 '20 at 09:03
  • 2
    I was unable to find the answer to my question in the other question. Generally people usually get warned about long discussions in the comments I believe. – mkmostafa May 22 '20 at 09:06
  • comments are to ask for clarification. If you want to get clarification wether the the defect report has been approved or not, then imho a comment is appropriate (not suggesting that this is a terribly bad question btw) – 463035818_is_not_an_ai May 22 '20 at 09:10
  • @mkmostafa It’s still exactly the same question — it’s just that the current answer is potentially wrong. You’re normally expected to put a bounty on the existing question to float it back up, but I agree that this process is not ideal. Anyway, this is a good question. But it *is* an exact duplicate. – Konrad Rudolph May 22 '20 at 09:11
  • Also I was not sure if I throw some comment in 2 year old question that it will get noticed by anybody except the people who participated in that old question. – mkmostafa May 22 '20 at 09:11
  • @KonradRudolph so my expectations were somewhat true right? without that bounty only the people who have participated in that comment would notice? – mkmostafa May 22 '20 at 09:13
  • if you put a comment below the answer the answerer will get notified, and if the answer is out-of-date, you actually should comment. Though there was already no reply to the comment you quote. One reason could be that the answerer is still convinced that their ansewr is correct and gcc-dev was wrong. It is not because the answerer is inactive currently (as you can see by visiting their profile) – 463035818_is_not_an_ai May 22 '20 at 09:17
  • unfortunately we can't say for sure that either the answerer or the commenter of that question are correct. The answerer has not refuted the commenter (he must have been notified of that comment according to you, right?) and the commenter did not provide any evidence of his claims about this mysterious gcc dev who said that the DR was not accepted. Thus we need more people to take a look at this and provide some new insights. – mkmostafa May 22 '20 at 10:31
  • Does this answer your question? [Explicit specialization in non-namespace scope does not compile in GCC](https://stackoverflow.com/questions/49707184/explicit-specialization-in-non-namespace-scope-does-not-compile-in-gcc) – fde-capu May 26 '22 at 12:49
  • To me, this question is different from the one linked because here the member templates being specialized are structs, not functions. Structs have slightly more flexible rules when it comes to specialization, though I'm not sure if any of those differences would be at play for this question. In any case, I am currently struggling with the same issue. – Charles Ofria Jul 01 '22 at 19:38

1 Answers1

0

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.

Charles Ofria
  • 1,936
  • 12
  • 24