3

The code below compiles and works correctly in Clang but in GCC it gives an error:

<source>:9:14: error: explicit specialization in non-namespace scope 'class DecideType<A, B>'

template<int A, int B> class DecideType
{
    template<bool CONDITION = A < B> class RealDecision
    {
    public:
        using type = int;
    };

    template<> class RealDecision<false>
    {
    public:
        using type = char;
    };

public:
    using type = typename RealDecision<>::type;
};

The code can be fixed for GCC very easily by just adding some additional, unused template parameter.

template<int A, int B> class DecideType
{
    template<class X, bool CONDITION = A < B> class RealDecision
    {
    public:
        using type = int;
    };

    template<class X> class RealDecision<X, false>
    {
    public:
        using type = char;
    };

public:
    using type = typename RealDecision<void>::type;
};

Which compiler is correct and why? I've found a change proposal that would suggest that a full specialization should be allowed in this situation since C++17 but I don't know if this is officially in the standard. https://wg21.cmeerw.net/cwg/issue727

If GCC is right, why there is such a difference between partial specialization and explicit specialization? After all, explicit specialization is just special case of partial specialization.

EDIT 0

There is a bug report for C++17 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85282. I think it is rejected but it seems to be pretty inconclusive with my knowledge. I don't know if it allows to conclude anything about C++20.

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
  • Perhaps https://stackoverflow.com/questions/3052579/explicit-specialization-in-non-namespace-scope#:~:text=An%20explicit%20specialization%20shall%20be,class%20template%20is%20a%20member. ? – Severin Pappadeux Jul 30 '20 at 21:37
  • 1
    An explicit specialization isn’t a template, in your case it’s a class, while the partial specialization is a template. – Daniel Jul 30 '20 at 21:38
  • @Dani Isn't an empty array an array? I would be tempted to say that any non-template type is a template with 0 parameters ;) – Piotr Siupa Jul 30 '20 at 21:44
  • @SeverinPappadeux Not exactly. https://stackoverflow.com/q/49707184/3052438 is closer but it's still is a little too old. I'm looking for an answer for C++20 because there is a good chance that the situation has changed in this version. – Piotr Siupa Jul 30 '20 at 21:47
  • @NO_NAME: Nothing has changed in C++ or (unfortunately) GCC since that question was asked; this is a duplicate unless you really want the two-word answer “no news”. – Davis Herring Jul 31 '20 at 01:21
  • @DavisHerring Well, it's still a better answer than no answer. I understand you've confirmed this with the ISO standard? – Piotr Siupa Jul 31 '20 at 07:21
  • @NO_NAME: The default for anything is “nothing has changed”—we wouldn’t want “Does C++20 still have `decltype`?” as a question, so I’m not too sure about this one. The wording changed in the issue is still present in C++20, yes. – Davis Herring Jul 31 '20 at 07:33
  • @DavisHerring Well, if they were debates about removing `decltype` in the international standardization organization, then perhaps we would want that question. Anyway, that answer satisfy me. – Piotr Siupa Jul 31 '20 at 07:46
  • @NO_NAME: There have been no debates about this question either since March 2017. – Davis Herring Jul 31 '20 at 07:52
  • @DavisHerring Yes. You would expect that any decision from 2017 could made into C++20. C++17 was already finalizing. – Piotr Siupa Jul 31 '20 at 08:01
  • @NO_NAME: But the issue page that you linked gives it a status of “C++17”, and no one has suggested that it wasn’t included. – Davis Herring Jul 31 '20 at 08:06

0 Answers0