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] Incorrect Diagnostic Message for Ambiguous Overloaded Operator in Clang #122579

Open
wangbo15 opened this issue Jan 11, 2025 · 0 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@wangbo15
Copy link

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
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jan 11, 2025
@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed clang Clang issues not falling into any other category labels Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

No branches or pull requests

3 participants