Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a friend function defined in-class automatically inline?

Tags:

c++

inline

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.

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.

like image 918
Armen Tsirunyan Avatar asked Oct 20 '10 17:10

Armen Tsirunyan


People also ask

Are Friend functions inline?

Friend functions can be defined (given a function body) inside class declarations. These functions are inline functions.

Is friend function always defined outside the class?

What is Friend Function? Friend functions of the class are granted permission to access private and protected members of the class in C++. They are defined globally outside the class scope.

How is a friend function to a class declared?

A friend function can access the private and protected data of a class. We declare a friend function using the friend keyword inside the body of the class.

Are class functions inline by default?

In fact, all the functions defined inside the class are implicitly inline. Thus, all the restrictions of inline functions are also applied here. If you need to explicitly declare inline function in the class then just declare the function inside the class and define it outside the class using inline keyword. };


1 Answers

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.

like image 116
Potatoswatter Avatar answered Oct 21 '22 07:10

Potatoswatter