ctpg icon indicating copy to clipboard operation
ctpg copied to clipboard

Clang C++20 compatibility: made operator== and operator!= friend functions

Open JPenuchot opened this issue 4 years ago • 6 comments

Here is an example error message prior to this fix which I got using Clang 13.0.0 with -std=c++20:

/usr/local/include/ctpg/ctpg.hpp:187:25: error: use of overloaded operator '==' is ambiguous (with operand types 'ctpg::stdex::cvector<unsigned short, 13>::iterator' and 'ctpg::stdex::cvector<unsigned short, 13>::iterator')
            while (!(it == end()))
                     ~~ ^  ~~~~~

The way I understand it from explanations by @jfalcou (who suggested this fix to me) is that C++20 generates a friend operator== by default which is ambiguous with the one provided by ctpg.

This commit fixes that by providing operator== as a friend function rather than a member function, therefore avoiding any ambiguity. friend operator!= is also provided after this commit.

Correct me if I'm wrong however.

Regards, Jules

JPenuchot avatar Jan 07 '22 13:01 JPenuchot

What about gcc? Is it really somthing the standard added in c++20? Declaring == operator as a member was a common thing, this would break a lot of code. Can you point me to some article explaining the change?

peter-winter avatar Jan 07 '22 14:01 peter-winter

What about gcc?

I didn't try anything yet with gcc as it doesn't support constexpr virtual destructors and I'm using them. I may be able to find a minimal example to reproduce the error I'm trying to fix.

Can you point me to some article explaining the change?

https://en.cppreference.com/w/cpp/language/default_comparisons

JPenuchot avatar Jan 07 '22 14:01 JPenuchot

But I'm not using default comparisons here. These are just custom comparisons. Why would a compiler generate a comparison operator for me if I'm explicitly definig one. It looks like a bug in clang. But I may be wrong. I would need some more input on that to make a decision. The fix is trivial and doesn't hurt, but still I would like to know the reason i'm fixing things.

peter-winter avatar Jan 07 '22 15:01 peter-winter

Any progress on this issue?

peter-winter avatar Jan 17 '22 12:01 peter-winter

Not yet, I'm busy with non-literal type compatibility at the moment. I will spend more time on that issue later and try to find better explanations.

JPenuchot avatar Jan 17 '22 13:01 JPenuchot

I found these issues on the clang bug tracker:

https://bugs.llvm.org/show_bug.cgi?id=43765 https://bugs.llvm.org/show_bug.cgi?id=46508 https://bugs.llvm.org/show_bug.cgi?id=47356 https://bugs.llvm.org/show_bug.cgi?id=50366

So it might just be a Clang issue as you said. So far turning the operator== member into a friend function is the only workaround I know of and doesn't seem to break C++17 compatibility with any compiler.

JPenuchot avatar Jan 19 '22 15:01 JPenuchot