是在类中自动内联定义的一个friend函数吗? [英] Is a friend function defined in-class automatically inline?

查看:156
本文介绍了是在类中自动内联定义的一个friend函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在类中定义了成员函数,它是一个内联函数。例如

If a member function is defined inside the class, it is an inline function. E.g.

struct X
{
   void mem_f() {} //mem_f is inline
};

我的问题是类中定义的非成员友元函数是否也自动内联。

My question is whether a nonmember friend function defined inside the class is also automatically inline.

例如

struct Y
{ 
   friend void friend_f() {} //is friend_f inline?
};

标准中的相关报价/ paragraph_no将受到欢迎。谢谢。

A relevant quote/paragraph_no from the standard would be much welcome. Thanks.

推荐答案

是的,是的。 §11.4/ 5:

Yes, it is. §11.4/5:


函数可以在朋友中定义
类的声明,如果且仅当
类是非本地类(9.8),
函数名是不合格的,而
函数具有命名空间范围。
这样的函数是内联的。
中定义的一个友元函数a类位于
的(词法)范围中。在
类外部定义的
friend函数不是(3.4.1)。

A function can be defined in a friend declaration of a class if and only if the class is a non-local class (9.8), the function name is unqualified, and the function has namespace scope. Such a function is implicitly inline. A friend function defined in a class is in the (lexical) scope of the class in which it is defined. A friend function defined outside the class is not (3.4.1).

可能在头文件中,函数将被乘法定义,因此需要 inline

Since the class definition is presumably in a header file, the function will be multiply-defined, so it needs to be inline.

这篇关于是在类中自动内联定义的一个friend函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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