joboccara / NamedType

Implementation of strong types in C++
MIT License
772 stars 85 forks source link

Make `Comparable`'s operators symmetric, fixing C++20 diagnostics #65

Closed BenHetherington closed 2 years ago

BenHetherington commented 3 years ago

When C++20 is enabled, this fixes a warning when compiling == or != under Clang, and an error when compiling != under MSVC or GCC. (See https://godbolt.org/z/Th4WE4EdP for an example of this error and warning.)

C++20 can now rewrite comparison operators, potentially swapping arguments or rewriting missing operators in terms of other operators. As NamedTypes inheriting from the Comparable skill fulfil both sides of the comparisons, this can lead to ambiguities as to which comparison operator should be selected, once swapped arguments are considered.

This PR fixes this ambiguity by making the comparisons take two Comparable<T>s instead of a Comparable<T> and a T.

If there's a reason for this asymmetry, then I'm happy for a different solution to be used here. I've also effectively reverted bcc4051 (as this seemed to be part of the reason why MSVC and GCC considered != ambiguous enough to be an error), so if this reintroduces a warning, then also let me know.

DarkWingMcQuack commented 2 years ago

nice, i have exactly this problem and cannot compile with clang because of this

usefulcat commented 2 years ago

I believe I have run into this exact problem. I came up with a workaround for my particular case (just operator==), but I suspect this patch is a much more complete solution.

mwiesnero2c commented 2 years ago

I have encountered the same problem and would be very happy if this solution is accepted. The pull request has been ready for half a year now. What is keeping you from merging it @joboccara?