模板化类中模板化方法的“模糊新声明”错误

ein*_*ica 1 c++ templates ambiguity template-specialization c++11

我写了以下惊天动地的应用程序:

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

当我编译这个(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();
                                    ^
Run Code Online (Sandbox Code Playgroud)

为什么这是一个“模棱两可的声明”,U除了特定的 s 实例化之外,我还能如何为所有s实现 bar Foo

使用 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>
^        ~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

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

Pra*_*ian 5

不知道有歧义的 new 声明意味着什么,但是您正在专门化封闭类 template Foo,因此您需要使用空的模板参数列表来表示

template <>
template <typename U>
void Foo<SomeA, SomeB, SomeC>::bar() { }
Run Code Online (Sandbox Code Playgroud)

现场演示