joboccara / NamedType

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

warning in c++20 due to == operator #42

Closed Shachlan closed 4 years ago

Shachlan commented 4 years ago
using Foo = fluent::NamedType<int, struct FooTag, fluent::FunctionCallable, fluent::Arithmetic>;

int main(int argc, char *argv[]) {
  auto a = Foo(1);
  auto b = Foo(0);
  return (a == b) ? 0 : 1;
}

Compile using clang 10, get this warning:

main.cpp:9:13: warning: ISO C++20 considers use of overloaded operator '==' (with operand types 'fluent::NamedType<int, FooTag, fluent::FunctionCallable, fluent::Arithmetic>' and 'fluent::NamedType<int, FooTag, fluent::FunctionCallable, fluent::Arithmetic>') to be ambiguous despite there being a unique best viable function [-Wambiguous-reversed-operator]
  return (a == b) ? 0 : 1;
          ~ ^  ~
./underlying_functionalities.hpp:81:20: note: ambiguity is between a regular call to this operator and a call with the argument order reversed
    constexpr bool operator==(T const& other) const { return !(*this < other || *this > other); }
joboccara commented 4 years ago

Thanks for bringing it up! This should now be fixed with https://github.com/joboccara/NamedType/commit/bcc405100e185462f7221824229a0e060107da6c

Tbh I don't understand the rationale of the warning, and I see that other libraries such as Boost have had it too. In their thread they mention this could be a defect in clang. In the meantime we can work around the issue by defining the operator== as a friend rather than a member function.

Shachlan commented 3 years ago

@joboccara, I can't seem to be able to reopen this issue. This seems to be solved in MacOS, but I still get the warning in linux, using clang 10:

test.cpp:8:13: warning: ISO C++20 considers use of overloaded operator '==' (with operand types 'fluent::NamedType<int, FooTag, fluent::FunctionCallable, fluent::Arithmetic>' and 'fluent::NamedType<int, FooTag, fluent::FunctionCallable, fluent::Arithmetic>') to be ambiguous despite there being a unique best viable function [-Wambiguous-reversed-operator]
  return (a == b) ? 0 : 1;
          ~ ^  ~
./third_party/NamedType/include/NamedType/underlying_functionalities.hpp:286:44: note: ambiguity is between a regular call to this operator and a call with the argument order reversed
    FLUENT_NODISCARD friend constexpr bool operator==(Comparable<T> const& self, T const& other)
DarkWingMcQuack commented 2 years ago

i have the same issue with clang-12 on linux, the following code triggers the warning:

#include <NamedType/named_type.hpp>
using Foo = fluent::NamedType<std::size_t, struct FooTag, fluent::Arithmetic>;

int main()
{
    Foo{1} == Foo{2};
}

The warning is:

warning: ISO C++20 considers use of overloaded operator '==' (with operand types 'Foo' (aka 'NamedType<unsigned long, FooTag, fluent::Arithmetic>') and 'Foo') to be ambiguous despite there being a unique best viable function [-Wambiguous-reversed-operator]
    Foo{1} == Foo{2};
    ~~~~~~ ^  ~~~~~~
namedtype/include/NamedType/underlying_functionalities.hpp:286:44: note: ambiguity is between a regular call to this operator and a call with the argument order reversed
    FLUENT_NODISCARD friend constexpr bool operator==(Comparable<T> const& self, T const& other)
                                           ^
src/main.cpp:20:12: warning: equality comparison result unused [-Wunused-comparison]

I am using the current commit of the master branch.