Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JPenuchot
Copy link
Contributor

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

@peter-winter
Copy link
Owner

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?

@JPenuchot
Copy link
Contributor Author

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

@peter-winter
Copy link
Owner

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
Copy link
Owner

Any progress on this issue?

@JPenuchot
Copy link
Contributor Author

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
Copy link
Contributor Author

JPenuchot commented Jan 19, 2022

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 JPenuchot changed the title C++20 compatibility fix: made operator== and operator!= friend functions Clang C++20 compatibility: made operator== and operator!= friend functions Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants