Is this partial class specialization buggy or is it a bug in clang or gcc?

template <class A>
struct Foo {
  template <class Bar>
  constexpr auto a_method();
};

template <class A>
template <class Bar>
constexpr auto Foo<A>::a_method() {
    return 42;
}

template <>
template <class Bar>
constexpr auto Foo<void>::a_method() {
    return 42;
}

GCC can compile this.

But Clang cannot. The errors output:

<source>:15:27: error: conflicting types for 'a_method'
constexpr auto Foo<void>::a_method() {
                          ^
<source>:4:18: note: previous declaration is here
  constexpr auto a_method();
                 ^
1 error generated.
Compiler returned: 1

Solution 1:

This seems to be a problem only when the keyword auto is used as the return type of the function in your above code.

For example, if you don't use auto as the return type of the function a_method and instead use any other type as the return type then the program will compile.

This seems to be a bug in Clang.

Another thing i have noticed is that if you first provide an explicit specialization for the class template, then the code will compile with auto .