C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

friend definitions within the class body

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
emild@collectivestudios.c
Guest





PostPosted: Thu Dec 16, 2004 1:39 pm    Post subject: friend definitions within the class body Reply with 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.
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





PostPosted: Fri Dec 17, 2004 1:32 pm    Post subject: Re: friend definitions within the class body Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.