"ambiguating new declaration" 模板化 class 中的模板化方法错误

"ambiguating new declaration" error for a templated method in a templated class

我写了下面这个惊天动地的应用:

class SomeA { }; class SomeB { }; class SomeC { };

template <typename A, typename B, typename... Cs>
class Foo {
public:
    template <typename U> static void bar();
};

template <typename U>
void Foo<SomeA, SomeB, SomeC>::bar() { };

int main() { return 0; }

当我编译它时(gcc 4.9.3 with -std=c++11),我得到以下错误:

a.cpp:10:36: error: ambiguating new declaration of ‘static void Foo<SomeA, SomeB, SomeC>::bar()’
 void Foo<SomeA, SomeB, SomeC>::bar() { };
                                    ^
a.cpp:6:36: note: old declaration ‘static void Foo<A, B, Cs>::bar() [with U = U; A = SomeA; B = SomeB; Cs = {SomeC}]’
  template <typename U> static void bar();
                                    ^

为什么这是一个 "ambiguating declaration",除了 Foo 的特定实例,我还能如何为所有 U 实现 bar?

使用 clang 3.6.2,我得到错误:

a.cpp:9:1: error: template parameter list matching the non-templated nested type 'Foo<SomeA, SomeB, SomeC>' should be empty ('template<>')
template <typename U>
^        ~~~~~~~~~~~~

我也不太明白。如果 clang 想要一个空参数列表,我应该如何在 U 上进行模板化?

不知道模棱两可的新声明 是什么意思,但是您正在专门化封闭的 class 模板 Foo,因此您需要用空模板参数列表

template <>
template <typename U>
void Foo<SomeA, SomeB, SomeC>::bar() { }

Live demo