Topic: in which namespace to put operators?
Author: Robert Klemme <klemme@unity.de>
Date: 1999/08/11 Raw View
hi there,
i wonder which is the right place for an overloaded binary operator.
as it stands my compiler does not see an error in having the operator
defined locally to namespace dada but using it in global namespace
without a "using namespace dada" (for example in function main). i
would have expected that the operator should be defined globally in
order to be visible everywhere. so is this a convenience in the
standard or is there something wrong with the compiler. (guess which
....)
namespace dada {
struct dodo {
int c;
};
#if LOCAL_DEFINED
bool operator < (const dodo& a, const dodo& b)
{ return a.c < b.c;}
#endif
}
#if ! LOCAL_DEFINED
bool operator < (const dada::dodo& a, const dada::dodo& b)
{ return a.c < b.c;}
#endif
can somebody please clarify on this? thank you!
robert
--
Robert Klemme
Internet Consultant/Berater
-------------------------------------------------------------
UNITY AG ~ Riemekestra e 160 ~ D-33106 Paderborn
http://www.unity.de ~ E-Mail: mailto:klemme@unity.de
Telefon: 0 52 51 / 6 90 90-207 ~ Fax: 0 52 51 / 6 90 90-199
-------------------------------------------------------------
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Salters <salters@lucent.com>
Date: 1999/08/11 Raw View
Robert Klemme wrote:
> hi there,
> i wonder which is the right place for an overloaded binary operator.
> as it stands my compiler does not see an error in having the operator
> defined locally to namespace dada but using it in global namespace
> without a "using namespace dada" (for example in function main). i
> would have expected that the operator should be defined globally in
> order to be visible everywhere. so is this a convenience in the
> standard or is there something wrong with the compiler. (guess which
> ....)
> namespace dada {
> struct dodo {
> int c;
> };
> #if LOCAL_DEFINED
> bool operator < (const dodo& a, const dodo& b)
> { return a.c < b.c;}
> #endif
> }
> #if ! LOCAL_DEFINED
> bool operator < (const dada::dodo& a, const dada::dodo& b)
> { return a.c < b.c;}
> #endif
> can somebody please clarify on this? thank you!
> robert
You do not show main, so this is only a guess:
int main()
{
dada::dodo x;
dada::dodo y;
cout << x < y ? : "x is smaller\n" : "y is smaller\n" ;
}
When examples like these surfaced, the standard committee indeed
had a problem. Allowing the operators in the global namespace
was troublesome, the natural place would be in dada.
The solution proposed by Andrew Koenig was to look for matching
function in the namespaces where the types of the arguments are
defined. This is therefore called Koenig lookup.
Since x and y have the type dada::dodo, the namespaces considered
are the global namespace (default) and the namespace of dada::dodo,
being dada. That's why both operators work, and it is an error to
have both.
Michiel Salters
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]