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 

Re: Using friend member definition for specialization

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Graeme Prentice
Guest





PostPosted: Tue Jul 22, 2003 6:47 pm    Post subject: Re: Using friend member definition for specialization Reply with quote



On 17 Jul 2003 22:56:46 -0400, "Mirek Fidler" <cxl (AT) volny (DOT) cz> wrote:


[multiposted to comp.std.c++ and c++.moderated]


Quote:
Update:

Comeau seems to accept this variant:

[ quotation prefix removed from Mirek's code ]


template <class T>
void Foo(const T& x) { *x; }

template<typename T>
struct Special {
friend void Foo(const T& x) {}
};

struct Alfa : Special<Alfa>
{
};

void g()
{
Foo(Alfa());
// Foo(int()); // this fails in generic Foo variant on '*x'...
}

Quote:
Can I rely on that this is a 100% legal C++ ?


It's legal but the standard is unclear (especially 11.4 para 5) on how
name lookup is supposed to work in this situation IMO. 14.6.5 para 2
suggests that only argument dependent lookup works if the friend
function is not explicitly declared at namespace scope. Since it's
inconvenient to declare the non template Foo at namespace scope in this
case, I guess you're hoping that arg. dependent lookup will always work.
Comeau seems to use only ADL for this and GCC/Borland manage to find the
function without ADL - this is probably because the standard is
unclear.


::Foo( Alfa() ); // fails with Comeau, ok with GCC

Special<float> abc;
// ...
Foo( float(3) ); // fails with Comeau, ok with GCC & Borland



Foo( Alfa() ) succeeds with Comeau in calling the friend function
because argument dependent lookup finds it - Alfa() is an
associated class. Argument dependent lookup is used only in unqualified
function call, so with Comeau, the qualified call fails and so does Foo
( float ) because Special<float> is not an associated class.


< 3.4.2 para 3 (arg dependent lookup) says >
Any namespace scope friend functions declared in associated classes are
visible within their respective namespaces even if they are not visible
during an ordinary lookup (11.4).
<>


11.4 para 5 says ...
<quote>
A friend function defined in a class is in the (lexical) scope of the
class in which it is defined. A friend function defined outside the
class is not (3.4.1).
<>

What on earth do those two sentences mean ?? - if the friend function
is defined in the class, then (obviously) it's in the lexical scope of
the class - so what? How does this affect name lookup - it doesn't
say.

Defect report 143 removed the words "and classes" from a sentence near
the end of 3.4.2 para 2 - this might make your code illegal,
depending on how you interpret 3.4.2 para 3. This is where I get lost.


11.4 para 5 also says ...
A function can be defined in a friend declaration of a class if and only
if the class is a nonlocal class (9.Cool, 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
};


What does "and the function has namespace scope" mean? - how could it
not have namespace scope? Why does it say "global f" - what if the
class is inside a named namespace - which namespace does f belong to
then? Do other f functions found in that namespace get considered.
Since f() takes no arguments in this case, an unqualified call could
never find it if it wasn't explicitly declared at namespace scope as
well, in this case.

Graeme

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Graeme Prentice
Guest





PostPosted: Wed Jul 23, 2003 3:43 am    Post subject: Re: Using friend member definition for specialization Reply with quote



On 17 Jul 2003 22:56:46 -0400, "Mirek Fidler" <cxl (AT) volny (DOT) cz> wrote:

[multiposted to comp.std.c++ and c++.moderated]


Quote:
Update:

Comeau seems to accept this variant:

[ quotation prefix removed from Mirek's code ]


template <class T>
void Foo(const T& x) { *x; }

template<typename T>
struct Special {
friend void Foo(const T& x) {}
};

struct Alfa : Special<Alfa>
{
};

void g()
{
Foo(Alfa());
// Foo(int()); // this fails in generic Foo variant on '*x'...
}

Quote:
Can I rely on that this is a 100% legal C++ ?


It's legal but the standard is unclear (especially 11.4 para 5) on how
name lookup is supposed to work in this situation IMO. 14.6.5 para 2
suggests that only argument dependent lookup works if the friend
function is not explicitly declared at namespace scope. Since it's
inconvenient to declare the non template Foo at namespace scope in this
case, I guess you're hoping that arg. dependent lookup will always work.
Comeau seems to use only ADL for this and GCC/Borland manage to find the
function without ADL - this is probably because the standard is
unclear.


::Foo( Alfa() ); // fails with Comeau, ok with GCC

Special<float> abc;
// ...
Foo( float(3) ); // fails with Comeau, ok with GCC & Borland



Foo( Alfa() ) succeeds with Comeau in calling the friend function
because argument dependent lookup finds it - Alfa() is an
associated class. Argument dependent lookup is used only in unqualified
function call, so with Comeau, the qualified call fails and so does Foo
( float ) because Special<float> is not an associated class.


< 3.4.2 para 3 (arg dependent lookup) says >
Any namespace scope friend functions declared in associated classes are
visible within their respective namespaces even if they are not visible
during an ordinary lookup (11.4).
<>


11.4 para 5 says ...
<quote>
A friend function defined in a class is in the (lexical) scope of the
class in which it is defined. A friend function defined outside the
class is not (3.4.1).
<>

What on earth do those two sentences mean ?? - if the friend function
is defined in the class, then (obviously) it's in the lexical scope of
the class - so what? How does this affect name lookup - it doesn't
say.

Defect report 143 removed the words "and classes" from a sentence near
the end of 3.4.2 para 2 - this might make your code illegal,
depending on how you interpret 3.4.2 para 3. This is where I get lost.


11.4 para 5 also says ...
A function can be defined in a friend declaration of a class if and only
if the class is a nonlocal class (9.Cool, 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
};


What does "and the function has namespace scope" mean? - how could it
not have namespace scope? Why does it say "global f" - what if the
class is inside a named namespace - which namespace does f belong to
then? Do other f functions found in that namespace get considered.
Since f() takes no arguments in this case, an unqualified call could
never find it if it wasn't explicitly declared at namespace scope as
well, in this case.

Graeme

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards 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.