Skip to content

[Clang] Incorrect Diagnostic Message for Ambiguous Overloaded Operator in Clang #122579

@wangbo15

Description

@wangbo15

Given the following code:

#include<iostream>
struct operand
{
  int x;
  operator int (){return x;};
  operator bool() {return true;};
  operator char() {return 'c';};
};

bool bar (operand i, operand j)
{
  bool tst = (i == j);
  return tst;
}

int main(){
    operand o1{1}, o2{0};
    std::cout<<bar(o1, o2);
}

Clang rejects it with the diagnostic, which dose not make sense, because there is no float in the source code:

<source>:12:17: error: use of overloaded operator '==' is ambiguous (with operand types 'operand' and 'operand')
   12 |   bool tst = (i == j);
      |               ~ ^  ~
<source>:12:17: note: because of ambiguity in conversion of 'operand' to 'float'
<source>:5:3: note: candidate function
    5 |   operator int (){return x;};
      |   ^
<source>:6:3: note: candidate function
    6 |   operator bool() {return true;};
      |   ^
<source>:7:3: note: candidate function
    7 |   operator char() {return 'c';};
      |   ^
<source>:12:17: note: because of ambiguity in conversion of 'operand' to 'float'
   12 |   bool tst = (i == j);
      |                 ^
<source>:5:3: note: candidate function
    5 |   operator int (){return x;};
      |   ^
<source>:6:3: note: candidate function
    6 |   operator bool() {return true;};
      |   ^
<source>:7:3: note: candidate function
    7 |   operator char() {return 'c';};
      |   ^
<source>:12:17: note: built-in candidate operator==(float, float)
   12 |   bool tst = (i == j);

Moreover, MSVC's output seems correct:

example.cpp
<source>(12): error C2593: 'operator ==' is ambiguous
<source>(12): note: could be 'built-in C++ operator==(int, int)'
<source>(12): note: or       'built-in C++ operator==(int, bool)'
<source>(12): note: or       'built-in C++ operator==(int, char)'
<source>(12): note: or       'built-in C++ operator==(bool, int)'
<source>(12): note: or       'built-in C++ operator==(bool, bool)'
<source>(12): note: or       'built-in C++ operator==(bool, char)'
<source>(12): note: or       'built-in C++ operator==(char, int)'
<source>(12): note: or       'built-in C++ operator==(char, bool)'
<source>(12): note: or       'built-in C++ operator==(char, char)'
<source>(12): note: while trying to match the argument list '(operand, operand)'
cl : Command line warning D9002 : ignoring unknown option '-std=c++20'
Compiler returned: 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions