Topic: namespaces and overloaded operators


Author: kuhlins%anne@anne.wifo.uni-mannheim.de (Stefan Kuhlins)
Date: 1996/07/18
Raw View

How to call overloaded operators, which are defined
in namespaces?

#include <iostream>
using namespace std;

namespace N {
  class X {
  public:
    X() { /* ... */ }
    friend ostream& operator<<(ostream& os, const X&) {
      //...
      return os;
    }
    // ...
  };
}

int main() {
  N::X x;
  cout << x;                // Is this okay?
  N::operator<<(cout, x);   // Or is this the only way?
}


[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/07/18
Raw View
In article AA01516@hawk.wifo.uni-mannheim.de, kuhlins%anne@anne.wifo.uni-mannheim.de (Stefan Kuhlins) writes:
>
>How to call overloaded operators, which are defined
>in namespaces?
>
>#include <iostream>
>using namespace std;
>
>namespace N {
>  class X {
>  public:
>    X() { /* ... */ }
>    friend ostream& operator<<(ostream& os, const X&) {
>      //...
>      return os;
>    }
>    // ...
>  };
>}
>
>int main() {
>  N::X x;
>  cout << x;                // Is this okay?
>  N::operator<<(cout, x);   // Or is this the only way?
>}

Under the original namespace rules,
 cout << x
would not compile, which is clearly undesirable.  The new lookup rules
say that an overloaded operator is first looked for in the namespaces
where the types of its arguments are found. In the expression
 cout << x
x is found in namespace N, so the operator<< is also looked for
in namespace N. The code will then work. I don't know what compilers
currently support the new lookup rules.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/07/22
Raw View
>>>>> "SC" == Steve Clamage <clamage@Eng.Sun.COM> writes:
[...]
SC> The new lookup rules say that an
SC> overloaded operator is first looked for in the namespaces where
SC> the types of its arguments are found.
[...]

For those interested in all sorts of C++ trivia, this lookup rule
is sometimes called `Koenig lookup' (after Andrew Koenig who proposed
it originally, I believe). This rule has since then been generalized
in various ways and I heard the result being refered to as `extended
Koenig lookup', although I'm not sure Andrew Koenig wishes his name
to be attached to the extended scheme ;-)

 Daveed
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]