c++ - 模板化类中的模板化方法的 "ambiguating new declaration"错误

标签 c++ c++11 templates template-specialization ambiguity

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

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();
                                    ^

为什么这是一个“模棱两可的声明”,除了 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 上进行模板化?

最佳答案

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

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

Live demo

关于c++ - 模板化类中的模板化方法的 "ambiguating new declaration"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35166344/

相关文章:

c++ - 将方法作为模板参数传递 C++11

c++ - 如何对模板参数实现 child-of-X 限制?

c++ - 将二进制数据转换为可打印的十六进制

java - 游戏开发——制作 map

c++ - 用 2 个指针反转链表

c++ - 我可以在 C 中使用仿函数吗?

c++ - char数组声明中字符串文字周围的大括号有效吗? (例如 char s[] = {"Hello World"})

c++ - 什么时候在其他std::thread机制上使用std::promise是个好主意?

c++11 - Sfinae 和模板特化

css - Joomla 模板问题