-->

“ambiguating new declaration” error for a template

2019-09-09 22:52发布

问题:

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