www.digitalmars.com         C & C++   DMDScript  

c++.beta - Best viable function

reply Christof Meerwald <cmeerw web.de> writes:
Here are a few test-cases where I think DMC is wrong (see 13.3.3 Best Viable
Function [over.match.best]):

struct B
{ };

struct A
{
  A()
  { }

  A(const B &init_val);
  operator const B &() const;
};

inline bool operator!=(const B &x, const B &y);
inline bool operator!=(const A &x, const A &y);

int main()
{
  B b;
  A a;

  if (a != b)
  // DMC chooses:
  //  A::operator const B &()
  //  operator!=(const B &, const B &)
  {
    return 0;
  }

  return 1;
}

But I think it should be ambiguous. Both operator!= functions are viable
functions and I don't see any reason why operator!=(const B &, const B &)
should be better than the other one.


struct A
{
  A()
  { }

  A(const int &init_val);
  operator const int &() const;
};

bool operator!=(const A &x, const A &y);

int main()
{
  A a;

  if (a != 1)
  // DMC chooses:
  //  A::operator const int &()
  //  operator!=(const A &, const A &)
  {
    return 0;
  }

  return 1;
}

But again, this should also be ambiguous. operator!=(const A &, const A &)
and the built-in operator!=(int, int) are both viable function. And again I
don't see why one of them should be better than the other one.


struct A
{
  A()
  { }

  A(const int &init_val);
  operator const int &() const;
};

template<class T>
bool operator!=(const T &x, const T &y);

int main()
{
  A a;

  if (a != 1)
  // DMC chooses:
  //  A(const int &);
  //  operator!=(const T &, const T &) [T = A]
  {
    return 0;
  }

  return 1;
}

But in this case the built-in operator!=(int, int) is a better function than
the template function (because the built-in operator is a non-template
function)


bye, Christof

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

...and what have you contributed to the Net?
Jun 08 2003
parent "Paul McKenzie" <paul paul.net> writes:
You're right.  The first two cases are bugs and the compiler should have
generated an error.

The third case is valid, since non-template functions, in this case operator
!=,  is a "better match" than the template function.

Paul
Jun 09 2003