www.digitalmars.com         C & C++   DMDScript  

c++.beta - namespaces and operator overloading

namespace ns
{
template<class T>
bool operator!=(const T &x, const T &y)
{ }
}
using ns::operator !=;
// Error: identifier expected

Other compilers accept it and I haven't found anything in the C++ standard
which disallows it.


namespace ns
{
template<class T>
bool operator!=(const T &x, const T &y)
{ }
}

template<class T>
struct A
{ };

template<class T, class U>
bool operator!=(A<T> const & a, A<U> const & b)
{ }


int main()
{
  A<int> a;

  return a != a;
  // Error: ambiguous reference to symbol
  // Had: ns::operator !=(const T&,const T&)
  // and: operator !=(const A<T>&,const A<U>&)

  // return operator!=(a, a); works
}

DMC seems to lookup the operator in every namespace, but I haven't found
anything in the C++ standard why the name lookup rules for operators should
be any different than for functions (see 13.5.2 Binary operators
[over.binary]: "Thus, for any binary operator  , x z can be interpreted as
either x.operator (y) or operator(x, y).")


bye, Christof

-- 
http://cmeerw.org                                 JID: cmeerw jabber.at
mailto cmeerw at web.de

...and what have you contributed to the Net?
Jun 14 2003