Topic: Koenig Lookup and typedefs, why not?
Author: x9wu1@hotmail.com (Xiao-Hui Wu)
Date: Mon, 20 Aug 2001 17:04:33 GMT Raw View
>
> If I move operator<< to the global namespace, the code compiles and
> executes ok.
>
> [Q] Is it correct that koenig lookup does not work for typedefs? If
> yes, anyone knows why?
>
I had a similar problem with defining << operator for std::pair, EVEN
in the global namespace. I was told that Koening lookup prevents
looking up the operator<< in the global namespace.
Can anyone explain why should this be the case? This makes it
inconvenient for users to define their own operators that act on types
in the std namespace.
Xiao-Hui Wu
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: Ren van Oostrum <rene+gnus@cs.uu.nl>
Date: Mon, 20 Aug 2001 19:32:37 GMT Raw View
>> If I move operator<< to the global namespace, the code compiles and
>> executes ok.
>>
>> [Q] Is it correct that koenig lookup does not work for typedefs? If
>> yes, anyone knows why?
>>
Xiao-Hui> I had a similar problem with defining << operator for
Xiao-Hui> std::pair, EVEN in the global namespace. I was told that
Xiao-Hui> Koening lookup prevents looking up the operator<< in the
Xiao-Hui> global namespace.
Xiao-Hui> Can anyone explain why should this be the case? This makes
Xiao-Hui> it inconvenient for users to define their own operators
Xiao-Hui> that act on types in the std namespace.
See:
http://groups.google.com/groups?q=group:*c%2B%2B*+author:rene%2Bgnus%40cs.uu.nl&num=100&safe=off&rnum=1&selm=vlr8zo3kjx.fsf%40telgt.cs.uu.nl
Ren
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: Hyman Rosen <hyrosen@mail.com>
Date: Fri, 10 Aug 2001 10:49:58 CST Raw View
Markus Sch pflin wrote:
> [Q] Is it correct that koenig lookup does not work for typedefs? If
> yes, anyone knows why?
Because a typedef is simply a synonym for the actual type of the object,
and it's that actual type which is used for Koenig lookup.
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: x9wu1@hotmail.com (Xiao-Hui Wu)
Date: Tue, 21 Aug 2001 17:03:59 GMT Raw View
Ren?van Oostrum <rene+gnus@cs.uu.nl> wrote in message news:<vl7kvyh9eg.fsf@telgt.cs.uu.nl>...
>
> See:
>
> http://groups.google.com/groups?q=group:*c%2B%2B*+author:rene%2Bgnus%40cs.uu.nl&num=100&safe=off&rnum=1&selm=vlr8zo3kjx.fsf%40telgt.cs.uu.nl
>
> Ren
>
>
Thanks for the link. The example you gave is somewhat different.
Here the problem is that operator<< for std::pair cannot be found by
the compiler (i.e., compiler complains operator<< is not defined)
instead of a wrong operator being found.
I was told that when looking up operator<< for things defined in std
namespace, Koening lookup specifically excludes the global namespace.
My impression is that this is not the case for other namespaces.
Xiao-Hui
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: Markus =?ISO-8859-1?Q?Sch=F6pflin?=
Date: Thu, 9 Aug 2001 18:25:10 GMT Raw View
Hi all,
if my reading of 3.4.2 [basic.lookup.koenig] of the standard is
correct, my compiler (MSVC6SP5) is right in complaining about the
following piece of code:
---%<---
#include <iostream>
#include <vector>
namespace foo {
typedef std::vector<int> A;
std::ostream &operator<<(std::ostream &os, const A &a)
{
return os << "foo::A";
}
} // namespace foo
int main()
{
foo::A a;
// error C2679: binary '<<' : no operator defined
// which takes a right-hand operand of type
// 'class std::vector<int,class std::allocator<int> >'
// (or there is no acceptable conversion)
std::cout << a << '\n';
return 0;
}
--->%---
If I move operator<< to the global namespace, the code compiles and
executes ok.
[Q] Is it correct that koenig lookup does not work for typedefs? If
yes, anyone knows why?
TIA, Markus
---
[ 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.research.att.com/~austern/csc/faq.html ]