1

Normally when we define a member function directly in the class body, it is implicitly inline. But what about this:

class Foo {
public:
    friend void swap(Foo& a, Foo& b) { ... }

    ...
};

Is the swap implicitly inline or not?

Zizheng Tai
  • 6,170
  • 28
  • 79
  • I think it must be inline. Non-inline functionality can't go inside headers. Class bodies must be insertable inside headers. Ergo, all functionality provided inside class definition bodies must be inlinable functionality. – Petr Skocik Jul 12 '16 at 01:11
  • Is that even allowed? What does it mean to have a friend function defined inside the class body? Does it go into the enclosing namespace rather than being a member? – jtbandes Jul 12 '16 at 01:12
  • @jtbandes Yes. This defines a function in the same namespace as the class is in. – Zizheng Tai Jul 12 '16 at 01:15
  • @jtbandes It's quite well explained in Johannes's answer here: http://stackoverflow.com/questions/381164/friend-and-inline-method-whats-the-point . – Petr Skocik Jul 12 '16 at 01:23

1 Answers1

5

Yes. See [class.friend/6-7] from the Standard:

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.

Community
  • 1
  • 1
aschepler
  • 70,891
  • 9
  • 107
  • 161