14

In the next program, struct template A<int> has a specialization A<char>:

template <int>
struct A { constexpr operator int() { return 1; } };

template <char c>
struct A<c> { constexpr operator int() { return 2; } };

int main() {
    static_assert( A<1000>{} == 1 ); //ok in Clang and GCC
    static_assert( A<1>{} == 2 ); //ok in Clang only
}
  • Clang accepts the whole program.
  • GCC accepts the specialization definition, but ignores it in A<1>{}.
  • MSVC complains on such specialization:
error C2753: 'A<c>': partial specialization cannot match argument list for primary template

Demo: https://gcc.godbolt.org/z/Ef95jv5E5

Which compiler is right here?

Fedor
  • 17,146
  • 13
  • 40
  • 131
  • 2
    I don't think that this is what people meant by partial specialisation when they wrote the standard. So you get various random stuff. – ALX23z Jan 15 '22 at 10:09

1 Answers1

14

The active CWG issue 1647 mentions exactly this case of specializing an int non-type template parameter to a char.

It also mentions that the standard is currently lacking wording to handle type mismatches between non-type template parameters in primary templates and their partial specializations and that there is implementation divergence on the issue.

user17732522
  • 53,019
  • 2
  • 56
  • 105