这个部分类专业化是有问题的还是 clang 或 gcc 中的错误?

vla*_*don 6 c++ gcc partial-specialization clang

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;
}
Run Code Online (Sandbox Code Playgroud)

GCC可以编译这个。

但铿锵不能。错误输出:

<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
Run Code Online (Sandbox Code Playgroud)

use*_*570 1

仅当关键字用作上述代码中函数的返回类型时,这似乎才是问题。auto

例如,如果您不使用auto. 作为函数的返回类型a_method,而是使用任何其他类型作为返回类型,则程序将编译

这似乎是 Clang 中的一个错误。

我注意到的另一件事是,如果您首先为类模板提供显式专业化,那么代码将使用auto.