What is the difference between defining friend function inside the class or declaring inside and define outside of the class. Also why it is possible to place definition inside the class, as friend function are not member of the class.
-
possible duplicate of [what is the difference between friend function and friend class?](http://stackoverflow.com/questions/3793063/what-is-the-difference-between-friend-function-and-friend-class) – Roger Rowland Nov 21 '13 at 07:18
-
4@RogerRowland If this is a duplicate of that, I’m drunk. – Nov 21 '13 at 07:27
-
@rightfold "possible duplicate", so possibly you *are* drunk ;-) – Roger Rowland Nov 21 '13 at 07:50
6 Answers
Friend functions defined inside the class can only be looked up through ADL when called from outside the class. Functions defined outside of the class can be found even without ADL.
-
+1 for expliciting the quote that Joachim chose, it is perhaps the most important difference. – Matthieu M. Nov 21 '13 at 07:42
-
2Just ran into this issue. In my case this was an `operator==` defined as friend inside a class, which compiler couldn't find when both of its arguments required an implicit conversion... – anxieux Aug 28 '18 at 23:07
-
Actually, I still don't quite get that, so I've created a separate question: https://stackoverflow.com/questions/52067502/difference-between-lookup-rules-for-friend-function-defined-inside-vs-outside-of – anxieux Aug 28 '18 at 23:41
-
@anxieux implicit conversions might be good or evil, also [this answer](https://stackoverflow.com/a/52068027/1906174) gives more detailed view. – Wormer Aug 15 '19 at 17:53
-
@Wormer, Yes, thanks, the answer you referenced are from my question which I accepted :-) – anxieux Aug 21 '19 at 23:28
As long as the friend function is declared inside the class (it has to be) it doesn't matter where it's declared.
Also, defining a friend
function inside a class implicitly makes the function inline
as well.
Also (from the C++11 spec, §11.3/7):
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

- 400,186
- 35
- 402
- 621
There are subtle implications of the following regarding member access.
C++11 §11.4/5
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).
Still there as of C++17 §14.3/7
Such a function is implicitly an inline function (10.1.6). 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 (6.4.1).
Consider the condenced example from cppreference [Friend function definition], where f1
finds class static member and f2
finds global variable.
int i = 3;
struct X {
friend void f1(int x) {
i = x; // finds and modifies X::i
}
friend inline void f2(int);
static const int i = 2;
};
inline void f2(int x) {
i = x; // finds and modifies ::i
}
Of course this can't affect design desicions for the friend function. The main consideration between choices is a difference in name look up as already mentioned in the other answer. Don't forget the inline for f2
to match those of f1
implied by default.

- 665
- 7
- 11
A function can be defined in a friend declaration of a class if and only if the class is a non-local class , 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 .
C++11, [class.friend], ¶6-7

- 123,740
- 17
- 206
- 299

- 838
- 1
- 7
- 21
If you define it inside the class it's inline, if it's outside then it isn't.

- 85,011
- 4
- 57
- 81
It is not proper to define a friend function inside a class.
We use friend function to grant access to private members of the class to a function.
If we define friend function inside class then in a way it becomes a data member of the class and we know that data member of the own class has access to its private members.
So if you really want to make use of the friend concept in C++ then declare it outside of the private class.

- 467
- 6
- 11
-
There are cases where you would like the `friend` method to be defined inside. – Marine Galantin Jan 30 '22 at 14:32
-
See: https://stackoverflow.com/questions/381164/friend-and-inline-method-whats-the-point – Marine Galantin Jan 30 '22 at 14:34