Skip to content

use of overloaded operator== is ambiguous (with reversed parameter order) #45853

@dermojo

Description

@dermojo
Bugzilla Link 46508
Version 10.0
OS Linux
CC @rbock,@zygoloid

Extended Description

Hi,

The following code is a very reduced example from a template expression library:

  class Foo {};

  class X {
  public:
      template <typename T>
      constexpr Foo operator==(const T&) const 
      {
        return Foo{};
      }
  };

  class A : public X {};
  class B : public X {};

  int main() {
    Foo f = A{} == B{};
    return 0;
  }

It fails to compile with C++20:

clang++-10 -c sample.cpp -std=c++20
sample.cpp:16:16: error: use of overloaded operator '==' is ambiguous (with operand types 'A' and 'B')
Foo f = A{} == B{};
~~~ ^ ~~~
sample.cpp:6:18: note: candidate function [with T = B]
constexpr Foo operator==(const T&) const
^
sample.cpp:6:18: note: candidate function (with reversed parameter order) [with T = A]
1 error generated.

I can't figure out why the reversed operator is used here (tbh, I'm not a language lawyer).

Any help is appreciated, thanks!

clang++-10 --version
clang version 10.0.0-4ubuntu1~18.04.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++20clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions