Reputation: 4838
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.
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.
Upvotes: 3
Views: 385