【问题标题】:"ambiguating new declaration" error for a templated method in a templated class模板化类中模板化方法的“模糊新声明”错误
【发布时间】: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 上进行模板化?

【问题讨论】:

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


【解决方案1】:

不知道混淆新声明是什么意思,但是你专门化了封闭类模板Foo,所以你需要用一个空的模板参数列表来表明它

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

Live demo

【讨论】:

  • 哦……啊……我明白了。我不知道你可以立即关注template&lt;whatever&gt; 和另一个template&lt;whatever&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
相关资源
最近更新 更多