Is a friend function defined in-class automatically inline?

旧时模样 提交于 2019-12-01 15:00:55

问题


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.

E.g.

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

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


回答1:


Yes, it is. §11.4/5:

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).

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



来源:https://stackoverflow.com/questions/3980627/is-a-friend-function-defined-in-class-automatically-inline

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