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 

Koenig lookup Problem - quite arcane!

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





PostPosted: Fri Sep 23, 2005 1:20 pm    Post subject: Koenig lookup Problem - quite arcane! Reply with quote



The code is just simple, as:

namespace My
{
struct X{};
}

using namespace My;

ostream& operator<<(ostream& os,X const& p)
{
return os;
}

int main()
{
vector cout< copy(vp.begin(),vp.end(),
ostream_iterator }

However,when I copy 'ostream_iterator' to my own namespace and use that
fake ostream_iterator, everything just go fine!
Is it about koenig lookup? Why even comeau issues an error???
I don't know why, you guys plz help me,thanks in advance!


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

Back to top
Gianluca Silvestri
Guest





PostPosted: Sat Sep 24, 2005 2:13 am    Post subject: Re: Koenig lookup Problem - quite arcane! Reply with quote




<pongba (AT) gmail (DOT) com> ha scritto nel messaggio
news:1127416666.110879.41570 (AT) f14g2000cwb (DOT) googlegroups.com...
Quote:
The code is just simple, as:

namespace My
{
struct X{};
}

using namespace My;

ostream& operator<<(ostream& os,X const& p)
{
return os;
}

int main()
{
vector cout< copy(vp.begin(),vp.end(),
ostream_iterator }

However,when I copy 'ostream_iterator' to my own namespace and use that
fake ostream_iterator, everything just go fine!
Is it about koenig lookup? Why even comeau issues an error???
I don't know why, you guys plz help me,thanks in advance!

Yes, it's an ADL problem, but also a design fault. You can read this article
by Herb Sutter upon these things:
http://www.gotw.ca/publications/mill08.htm

HTH
Gianluca



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


Back to top
tpochep@mail.ru
Guest





PostPosted: Sat Sep 24, 2005 2:17 am    Post subject: Re: Koenig lookup Problem - quite arcane! Reply with quote



This is result of two-phase name-lookup in templates. One way to fix
the proble - put operator << in your namespace (you know it). Another
(tested with como online, but I'm not shure about it)

namespace My
{
struct X{};
}

#include
using namespace My;
std::ostream &operator<<(std::ostream &os, X const& p)
{
//you can put this definition after inclusion of need
//full definitions
return os;
}

#include <iostream>
#include <vector>
#include <iterator>

int main()
{
std::vector<X> vp;
std::copy(vp.begin(),vp.end(),
std::ostream_iterator<X>(std::cout));
}


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

Back to top
msalters
Guest





PostPosted: Sat Sep 24, 2005 2:19 am    Post subject: Re: Koenig lookup Problem - quite arcane! Reply with quote


[email]pongba (AT) gmail (DOT) com[/email] schreef:

Quote:
The code is just simple, as:

namespace My
{
struct X{};
}

using namespace My;

ostream& operator<<(ostream& os,X const& p)
{
return os;
}

int main()
{
vector cout< copy(vp.begin(),vp.end(),
ostream_iterator }

I'm missing some headers and some std:: qualifications. It's a bit
hard to answer questions about namespaces if you don't show us the
uses of one of the namespaces involved!

HTH,
Michiel Salters


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


Back to top
Bo Persson
Guest





PostPosted: Sat Sep 24, 2005 2:38 am    Post subject: Re: Koenig lookup Problem - quite arcane! Reply with quote


<pongba (AT) gmail (DOT) com> skrev i meddelandet
news:1127416666.110879.41570 (AT) f14g2000cwb (DOT) googlegroups.com...
Quote:
The code is just simple, as:

namespace My
{
struct X{};
}

using namespace My;

This makes the content of namespace My visible here. It doesn't make
this part of the namespace.

Quote:

ostream& operator<<(ostream& os,X const& p)
{
return os;
}

This operator is outside namespace My.

Quote:

int main()
{
vector cout< copy(vp.begin(),vp.end(),
ostream_iterator

You are using types from namespaces std and My, so the compiler looks
for a suitable operator<< in those namespaces. It will find lots of
them, but none that is suitable - error!

Quote:
}

However,when I copy 'ostream_iterator' to my own namespace and use
that
fake ostream_iterator, everything just go fine!

Of course - now the interface to type X is all in its namespace, so
it's found by the lookup.

Quote:
Is it about koenig lookup? Why even comeau issues an error???
I don't know why, you guys plz help me,thanks in advance!


Try reading "Namespaces and the Interface Principle" by Herb Sutter

http://www.gotw.ca/publications/mill08.htm



Bo Persson



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


Back to top
David Abrahams
Guest





PostPosted: Sat Sep 24, 2005 8:16 pm    Post subject: Re: Koenig lookup Problem - quite arcane! Reply with quote

[email]pongba (AT) gmail (DOT) com[/email] writes:

Quote:
The code is just simple, as:

namespace My
{
struct X{};
}

using namespace My;

ostream& operator<<(ostream& os,X const& p)
{
return os;
}

int main()
{
vector cout< copy(vp.begin(),vp.end(),
ostream_iterator }

However,when I copy 'ostream_iterator' to my own namespace and use that
fake ostream_iterator, everything just go fine!

Yes, because then the operator is in an associated namespace of X.

Quote:
Is it about koenig lookup?

Yes, that and 2-phase name lookup

Quote:
Why even comeau issues an error???

Because your code is ill-formed.

Quote:
I don't know why, you guys plz help me,thanks in advance!

14.6.4 Dependent name resolution

1 In resolving dependent names, names from the following sources are
considered:

--- Declarations that are visible at the point of definition of the
template.

--- Declarations from namespaces associated with the types of the
function arguments both from the instantiation context
(14.6.4.1) and from the definition context.

The point of definition of std::copy occurs long before the compiler
ever sees your declaration of operator<<. You could start playing
games with #include order to change that, but that's extremely fragile
approach.

HTH,

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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


Back to top
Frank Chang
Guest





PostPosted: Sat Sep 24, 2005 8:18 pm    Post subject: Re: Koenig lookup Problem - quite arcane! Reply with quote

When I do this it compiles okay:

namespace My
{
struct X1{};

ostream& operator<<(ostream&os,const X1& p)
{
return os;
}
};

As you say this is Koenig lookup(i.e argument dependent lookup) ,
allowing the compiler to find the operator << in namespace my without
having to resort to exotic namespace qualification. Thank you.


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

Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Sat Sep 24, 2005 8:22 pm    Post subject: Re: Koenig lookup Problem - quite arcane! Reply with quote

[email]pongba (AT) gmail (DOT) com[/email] wrote:
Quote:
The code is just simple, as:

namespace My
{
struct X{};
}

using namespace My;

ostream& operator<<(ostream& os,X const& p)
{
return os;
}

int main()
{
vector cout< copy(vp.begin(),vp.end(),
ostream_iterator }

However,when I copy 'ostream_iterator' to my own namespace and use that
fake ostream_iterator, everything just go fine!
Is it about koenig lookup? Why even comeau issues an error???
I don't know why, you guys plz help me,thanks in advance!


Yes it's all about Koenig lookup (aka argument dependent lookup or ADL)
and two-phase name binding. As std::copy is in namespace std, the only
operators << that are considered are:

a) At the point of definition of std::copy (first phase)
1) all operator<< in the global namespace
2) all operator<< in namespace std

b) At the point of instantiation, i.e. just before main (second phase,
using ADL)
3) all operator<< in namespace std (again)
4) all operator<< in namespace My

The problem is that as you put operator<< in the global namespace, it
cannot be found by any of those rules. (it cannot be found by rule 1
because it's not visibile at the point of definition of std::copy!)

Yes, putting the operator in the global namespace is *preventing* ADL to
kick in. Just move your operator<< in same namespace as X so that it can
be found by rule 4 and everything will be ok.

Copying the ostream_iterator in namespace My achieve the same result
(for a different reason, though), but why make things complicated by
duplicating library code, where there's a much simpler solution?

Ganesh

[ 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.