【发布时间】:2017-06-17 14:07:58
【问题描述】:
我写了以下惊天动地的应用程序:
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 的特定实例化之外,我还能如何为所有 Us 实现 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 上进行模板化?
【问题讨论】:
-
我不知道什么是“模棱两可的新声明”,但 Clang 给出了一个更有帮助的错误消息:coliru.stacked-crooked.com/a/308fd608ff25e9a3
标签: c++ c++11 templates template-specialization ambiguity