模板专业化的朋友声明失败 [英] friend declaration of template specialization fails

查看:70
本文介绍了模板专业化的朋友声明失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下包含朋友声明的代码失败,并显示错误消息(请参见 http://ideone.com/Kq5dy ):

The following code containing friend declaration fails with indicated error (see http://ideone.com/Kq5dy):

template<class T> void foo() {}

template<typename T>
class A {
   void foo();
   friend void foo<T>(); // error: variable or field 'foo' declared void
};

int main()
{
   foo<int>();
}

如果朋友声明和成员函数声明的顺序颠倒了,则代码编译不会出现问题(请参见 http://ideone.com/y3hiK ):

If the order of friend declaration and member function declaration reversed, then the code compiles without problems (see http://ideone.com/y3hiK):

template<class T> void foo() {}

template<typename T>
class A {
   friend void foo<T>();
   void foo();
};

int main()
{
   foo<int>();
}

如果friend声明中不包含模板专用化,则不会发生这种情况:非模板朋友以及模板朋友都可以.在模板专业化中也使用合格名称可以编译代码.我的问题是,为什么第一个例子失败了?似乎编译器在好友声明时仅在模板专用化时在类范围内查找名称?在标准中的何处指定了此行为?

This doesn't happen if the friend declaration doesn't contain template specialization: non-template friends are ok, as well as a template friends. Also using qualified name in template specialization allows code to compile. My question is why does the first example fail? It seems compiler looking up names in class scope at the point of friend declaration and only for template specialization? Where in the Standard this behavior is specified?

推荐答案

要明确指出您要成为的功能- friend 在功能名称前加上 :: 表示它在全局名称空间中.

To make it explicit that it's that function you want to be-friend prepend the function name with :: to say that it's in the global namespace.

可编译并执行所需操作的代码段

Snippet that compiles and does what you want:

template<class T> void foo() {}

template<typename T>
class A {
   void foo();
   friend void ::foo<T>();
};

int main()
{
   foo<int>();
}


n1905.pdf

3.4/9名称查询

名称查找,用于定义朋友功能的名称(11.4)在授予友谊的班级中内联定义应按描述了在成员函数定义中的查找.如果朋友未在授予友谊,名称查找的类中定义函数在朋友功能定义中,应按查找说明所述进行在命名空间成员函数定义中

Name lookup for a name used in the definition of a friend function (11.4) defined inline in the class granting friendship shall proceed as described for lookup in member function definitions. If the friend function is not defined in the class granting friendship, name lookup in the friend function definition shall proceed as described for lookup in namespace member function definitions

您的代码段无法编译,原因与以下代码无法编译的原因相同.

Your snippet fails to compile because of the same reason as the code below fails to compile.

template<class T> void foo () {}

template<typename T>
struct A { 

  void foo (); 

  void func () {
    foo<T> (); // when performing the name lookup A::foo is
               // found before ::foo<T>
  }
};

...

14.5.3朋友[临时朋友] 1

一个类或类模板的朋友可以是一个功能模板或类模板,功能模板或类模板,或普通(非模板)功能或类.对于不是模板声明:

A friend of a class or class template can be a function template or class template, a specialization of a function template or class template, or an ordinary (non-template) function or class. For a friend function declaration that is not a template declaration:

-如果朋友的名字是合格的或不合格的template-id,朋友声明指的是功能模板的专业化,否则

— if the name of the friend is a qualified or unqualified template-id, the friend declaration refers to a specialization of a function template, otherwise

-如果名称朋友是一个合格ID,并且找到了匹配的非模板函数在指定的类或名称空间中,friend声明引用该功能,否则,

— if the name of the friend is a qualified-id and a matching non-template function is found in the specified class or namespace, the friend declaration refers to that function, otherwise,

-如果朋友的名字是一个合格的ID并且在中找到功能模板的匹配专业化指定的类或名称空间,朋友声明是指功能模板专业化,否则,

— if the name of the friend is a qualified-id and a matching specialization of a function template is found in the specified class or namespace, the friend declaration refers to that function template specialization, otherwise,

-名称应为声明(或重新声明)普通(非模板)的无限定ID功能.

— the name shall be an unqualified-id that declares (or redeclares) an ordinary (non-template) function.

这篇关于模板专业化的朋友声明失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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