Skip to content

Clang can't choose between operators int/bool for operator< #155146

@Alcaro

Description

@Alcaro
struct eee {
	operator int() const { return 5; }
	operator bool() const { return true; }
};

int main(int argc, char** argv)
{
	eee f;
	if (f < 0) {}
}

https://godbolt.org/z/4Gnfo4Pqf

Expected: There is no operator<(bool,int), so choosing operator<(int,int) would require the fewest conversion operations, and should be chosen.
Actual: Prints a dozen candidate operator<, and thinks operator<(float,int) is the most relevant one.

<source>:9:8: error: use of overloaded operator '<' is ambiguous (with operand types 'eee' and 'int')
    9 |         if (f < 0) {}
      |             ~ ^ ~
<source>:9:8: note: because of ambiguity in conversion of 'eee' to 'float'
<source>:2:2: note: candidate function
    2 |         operator int() const { return 5; }
      |         ^
<source>:3:2: note: candidate function
    3 |         operator bool() const { return true; }
      |         ^
<source>:9:8: note: built-in candidate operator<(float, int)
    9 |         if (f < 0) {}
      |               ^
<source>:9:8: note: built-in candidate operator<(double, int)
<source>:9:8: note: built-in candidate operator<(long double, int)
<source>:9:8: note: built-in candidate operator<(__float128, int)
<source>:9:8: note: built-in candidate operator<(int, int)
<source>:9:8: note: built-in candidate operator<(long, int)
<source>:9:8: note: built-in candidate operator<(long long, int)
<source>:9:8: note: built-in candidate operator<(__int128, int)
<source>:9:8: note: built-in candidate operator<(unsigned int, int)
<source>:9:8: note: built-in candidate operator<(unsigned long, int)
<source>:9:8: note: built-in candidate operator<(unsigned long long, int)
<source>:9:8: note: built-in candidate operator<(unsigned __int128, int)
1 error generated.

I think this is a rejects-valid (GCC, MSVC, and my intuition all agree that operator int() should be chosen), but even if it's instead an accepts-invalid in the others, naming float in the error is rather unhelpful, isn't it?

(Bonus issue: Change to operator float(), and Clang prints an error naming 146 different operator<. I think that one is also rejects-valid, for the same reason. https://godbolt.org/z/Y8joWar89)

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions