“ambiguating new declaration” error for a templated method in a templated class

烈酒焚心 提交于 2019-12-12 01:38:47

问题


I have written the following earth-shattering application:

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; }

When I compile this (gcc 4.9.3 with -std=c++11), I get the following error:

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

Why is this an "ambiguating declaration", and how else can I implement bar for all Us but for a specific instantiation of Foo?

With clang 3.6.2, I get the error:

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>
^        ~~~~~~~~~~~~

I don't really get this either. How am I supposed to template over U if clang wants an empty parameter list?


回答1:


No idea what ambiguating new declaration means, but you're specializing the enclosing class template Foo, so you need to indicate that with an empty template parameter list

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

Live demo



来源:https://stackoverflow.com/questions/35166344/ambiguating-new-declaration-error-for-a-templated-method-in-a-templated-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!