[cfe-dev] Apparently valid C++ code rejected as ambiguous

Csaba Raduly rcsaba at gmail.com
Fri Nov 12 09:26:58 PST 2010


Hi all,
clang 2.8 fails to compile the following code:

template <typename T_type>
class O {
T_type *val;

public:
  O();
  O(T_type); // t

  O(const O& other_value); // copy ctor

  template <typename T_tmp>
  explicit O(const O<T_tmp>& other_value); // other O

  template <typename T_tmp>
  explicit O(const T_tmp& other_value); // other t

  inline bool operator==(const O& other_value) const;

  inline bool operator!=(const O& other_value) const;

  template <typename T_tmp>
  inline bool operator==(const T_tmp& other_value) const; // line 22

  template <typename T_tmp>
  inline bool operator!=(const T_tmp& other_value) const;

};

class C{
public:
  C();
};

class U_C{
public:
  U_C();
  U_C(const U_C&); // copy ctor
  /*explicit*/ U_C(const C&);
};

template<typename T_left, typename T_right>
inline bool operator==(const T_left& left_value, // line 42
  const O<T_right>& right_value)
{ return true; }


template<typename T_left, typename T_right>
inline bool operator!=(const T_left& left_value,
  const O<T_right>& right_value)
{ return false; }

int main()
{
  C cs;
  U_C us;
  O<C> ocs;
  O<U_C> ous;

  if (ocs == ous) {
    return 0;
  }
}
- - - - -
wtf.cc:59:11: error: use of overloaded operator '==' is ambiguous
  if (ocs == ous) {
      ~~~ ^  ~~~
wtf.cc:22:15: note: candidate function [with $0 = O<U_C>]
  inline bool operator==(const T_tmp& other_value) const;
              ^
wtf.cc:42:13: note: candidate function [with T_left = O<C>, T_right = U_C]
inline bool operator==(const T_left& left_value,


The code is accepted by GCC 4.6, the online version of Comeau and even
the online version of LLVM 2.7 !
This appears to be a regression. I had a brief look in Bugzilla but I
didn't spot anything close enough.

If "explicit" is removed from the two constructors, another operator==
(line 17) joins the list of possible candidates (even though GCC
continues to accept it).

Apart from this, it compiled our application (250kloc of C++) with
only a few spurious warnings (and a couple of valid warnings which
were missed by GCC!).
I like the colored error markers :)

Regards, Csaba
-- 
GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++
Life is complex, with real and imaginary parts.
"Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
"People disagree with me. I just ignore them." -- Linus Torvalds



More information about the cfe-dev mailing list