 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
emild@collectivestudios.c Guest
|
Posted: Thu Dec 16, 2004 1:39 pm Post subject: friend definitions within the class body |
|
|
Consider this code:
class a
{
friend void b( a const & ) { }
};
What was the motivation for the standard to restrict the visibility of
b to Koenig lookup only? It is especially puzzling to me because if we
define b outside of a, then the restriction does not apply.
Thanks,
Emil
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Dec 17, 2004 1:32 pm Post subject: Re: friend definitions within the class body |
|
|
In article <1103137689.460924.82840 (AT) z14g2000cwz (DOT) googlegroups.com>,
[email]emild (AT) collectivestudios (DOT) com[/email] writes
| Quote: | Consider this code:
class a
{
friend void b( a const & ) { }
};
What was the motivation for the standard to restrict the visibility of
b to Koenig lookup only? It is especially puzzling to me because if we
define b outside of a, then the restriction does not apply.
|
It concerns the problem of injecting names into the surrounding scope.
This is particularly nasty when templates are being used. We did not
want the existence of a friend declaration inside a template to result
in injecting that name into the enclosing scope with possible surprises
to the programmer. So the rule is:
If the function is already declared in the outer scope (with the same
parameter types), then the friend declaration binds to that declaration.
If it isn't the name will remain invisible in the enclosing scope unless
an appropriate declaration is later made in that scope.
To put it simply, names have to be declared in the scope for them to be
visible in the scope. The Koenig lookup rule was allow the name to be
found when it is clearly intended to be found.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|