“歧义新声明”;模板化类中的模板化方法错误 [英] "ambiguating new declaration" error for a templated method in a templated class

查看:77
本文介绍了“歧义新声明”;模板化类中的模板化方法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下令人震惊的应用程序:

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

当我对此进行编译(gcc 4.9.3 with -std = c ++ 11 ),出现以下错误:

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

为什么这是一个歧义声明,以及我还能如何实现bar对于所有 U ,但对于 Foo 的特定实例?

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

使用clang 3.6.2时,出现错误:

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

我不真的得到这个。如果铛想要一个空的参数列表,我应该如何在U上进行模板化?

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

推荐答案

不知道什么会使新的声明模棱两可的意思是,但是您要专门封闭类模板 Foo ,因此您需要使用空模板参数列表来表明这一点

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() { }

< a href = http://coliru.stacked-crooked.com/a/5e28e36d7e437402 rel = nofollow>实时演示

这篇关于“歧义新声明”;模板化类中的模板化方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆