1

§3.4.1/9 in the C++11 Standard says:

Name lookup for a name used in the definition of a friend function (11.3) defined inline in the class granting friendship shall proceed as described for lookup in member function definitions. If the friend function is not defined in the class granting friendship, name lookup in the friend function definition shall proceed as described for lookup in namespace member function definitions.

My interpretation of this paragraph is that friend functions defined in the class granting friendship follow the rules in the previous paragraph (3.4.1/8). But what about the next case, i.e., where the friend function is not defined in the class granting friendship? What is this, so called lookup in namespace function definition? Would that be paragraph 6? I'm not sure.

Wake up Brazil
  • 3,421
  • 12
  • 19
  • You should see this it may help... http://stackoverflow.com/questions/16718166/friend-function-declaration-definition-inside-a-namespace – Suchit kumar Apr 14 '14 at 12:32

1 Answers1

1

Yes, that refers to 3.4.1§6. Quoting the leading sentence of that paragraph:

A name used in the definition of a function following the function's declarator-id that is a member of namespace N ...

(emphasis mine)

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • How can you be so sure about this? – Wake up Brazil Apr 14 '14 at 12:30
  • @WakeupBrazil Because a name inside a friend function is not in the definition of any `class X`? – Angew is no longer proud of SO Apr 14 '14 at 12:33
  • I'm finally convinced it's paragraph 6. Thanks (+1). – Wake up Brazil Apr 14 '14 at 12:34
  • That 3.4.1p9 is not really clear. Is there a contradiction here? `void f(); struct A { friend void f() { X x; } typedef int X; }; struct B { friend void f(); };`? What is the lookup that is applied to the friend function definition? Shall it proceed "as described for lookup in namespace member function definitions" or "as described for lookup in member function definitions"? – Johannes Schaub - litb Apr 14 '14 at 12:56
  • @JohannesSchaub-litb `f()` is defined inline in class `A`, so it will follow lookup rules for member functions of class `A`. The other declarations of `f()` (which are *not* definitions) do not affect this - why do you think they should? – Angew is no longer proud of SO Apr 14 '14 at 13:06
  • @Angew because "If the friend function is not defined in the class granting friendship, name lookup in the friend function definition shall proceed as described for lookup in namespace member function definitions.". `f` is not defined in `B` (which grants friendship), so the Standard says that the definition of `f` uses certain lookup forms. – Johannes Schaub - litb Apr 14 '14 at 13:19
  • I do *not* think that the other two declarations should affect this, but I *do* think that the Standard says they affect this (in a contradicting way, indeed). – Johannes Schaub - litb Apr 14 '14 at 13:21
  • @JohannesSchaub-litb OK, I get your point now. You're right that it's contradictory. They probably meant the second "If" to be an `else if` ;-) – Angew is no longer proud of SO Apr 14 '14 at 13:26