我编写了以下惊天动地的应用程序:
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() { }
关于c++ - 模板化类中的模板化方法的 "ambiguating new declaration"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35166344/