This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: friend function definition
On 27 March 2018 at 18:12, Jonathan Wakely wrote:
> On 27 March 2018 at 17:49, Hans-Christian Stadler
> <hans-christian.stadler@psi.ch> wrote:
>> Thanks a lot for clearing that up.
>>
>> Now I just don't understand the text in the N4700 early version of the C++17
>> standard (under class.friend):
>>
>> A function can be defined in a friend declaration of a class if and only if
>> the class is a non-local class (12.4),
>> the function name is unqualified, and the function has namespace scope. [
>> Example:
>> class M {
>> friend void f() { }
>> // definition of global f, a friend of M,
>> // not the definition of a member function
>> };
>> — end example ]
>>
>> Anyway, it's probably to wierd a case to spend much time on.
>
> That's unrelated to your example, and is a better question for
> stackoverflow, since it's nothing to do with GCC.
If your confusion is with the wording in the example saying "global f"
then that is correct. The friend declaration defines ::f, but that
doesn't make the name visible to name lookup.
Think about a global function defined in foo.cpp that you want to call
from bar.cpp, just because it's global doesn't mean it's visible in
bar.cpp without a declaration. A declaration needs to be present in
bar.cpp for name lookup to find it.