<div dir="ltr"><font face="monospace, monospace">In our code base, we have the following (code linked below):<br><br>* A class template for "handles" to objects of assorted types, </font><div><font face="monospace, monospace">  Handle<T, Policy>; T is the actual element type</font></div><div><font face="monospace, monospace">* Handle contains a member operator < (and others). It uses the </font></div><div><font face="monospace, monospace">  "address" of the object to do the comparison.<br>* There is a special class where we want to customize operator < </font></div><div><font face="monospace, monospace">  so </font><span style="font-family:monospace,monospace">it looks into the associated object, so we have a templated </span></div><div><font face="monospace, monospace">  overload free function of operator< on Handle<Special, T2>.<br><br>I've got my doubts as to the sanity of this design in some ways, but I don't think I can reasonably change it.<br><br>This compiles with GCC [1], and with MSVC [2], but Clang gives an error about the two operator<s being ambiguous, starting with 3.5 [3].</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">Perhaps importantly, the operator< member of Handle has this "signature":</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">   template <typename T, typename Policy></font></div><div><font face="monospace, monospace">   template <typename RHS_Handle></font></div><div><font face="monospace, monospace">   bool Handle<T, Policy>::operator< (RHS_Handle const &)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">and the free function has this signature:</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    template <</font></div><div><font face="monospace, monospace">        typename LHS_T, typename LHS_Policy,</font></div><div><font face="monospace, monospace">        typename RHS_T, typename RHS_Policy></font></div><div><font face="monospace, monospace">    bool operator<  (</font></div><div><font face="monospace, monospace">        Handle<LHS_T, LHS_Policy> lhs,</font></div><div><font face="monospace, monospace">        Handle<RHS_T, RHS_Policy> rhs);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">At least if we ignore the references and cv-quals, my understanding is that the latter function should have priority over the former because it's more specialized -- the second parameter is Handle<RHS_T, RHS_Policy> rather than a generic RHS_Handle type.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">So if the function is called with </font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    Handle<MyType, SomePolicy> h1, h2;</font></div><div><font face="monospace, monospace">    h1 < h2;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">then it should not be ambiguous, and select the free overload. However, perhaps the cv-qualification matters, and GCC/MSVC get it wrong?</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">Credit to OmegaNaughtEquals1</font><span style="font-family:monospace,monospace"> on Reddit who pointed me to some standards text that seems to support the position that it should work (maybe... it's too late to really tell what should be happening with the cv/ref stuff) [4].</span></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">If I change the free function to take 'lhs' by reference, then Clang compiles it [5].</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">I'm happy to report this to the bug tracker if that's what I should do -- I just wasn't sure if I should with something that I'm not sure is a bug.</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">Thanks,</font></div><div><font face="monospace, monospace">Evan</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">[1] GCC compiles it: <a href="https://godbolt.org/g/9EaoYh">https://godbolt.org/g/9EaoYh</a></font></div><div><font face="monospace, monospace">[2] MSVC does too: <a href="https://godbolt.org/g/ubXeC3">https://godbolt.org/g/ubXeC3</a></font></div><div><font face="monospace, monospace">[3] Clang's error: <a href="https://godbolt.org/g/MhNEky">https://godbolt.org/g/MhNEky</a></font></div><div><font face="monospace, monospace">[4] Reddit thread: <a href="https://www.reddit.com/r/cpp_questions/comments/7ir4j5/-/">https://www.reddit.com/r/cpp_questions/comments/7ir4j5/-/</a></font></div><div><font face="monospace, monospace">[5] Modified copy with ref parameter: <a href="https://godbolt.org/g/v9HtuH">https://godbolt.org/g/v9HtuH</a></font></div></div>