llvm / llvm/llvm-project

Binary operator deduction fails when both operands are SFINAE conversion functions

Open
#212,412 3 comments 0 reactions 0 assignees View on GitHub
clang:frontend diverges-from:gcc diverges-from:msvc
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Minimal-ish reproduction:

```c++
template struct is_int {};
template<> struct is_int { using type = int; };

struct T {
template::type>
operator U() { return {}; }
};

int main ()
{
T a;
0+a; // works
a+a; // error: invalid operands to binary expression ('T' and 'T')
}
```

I'm pretty sure both expressions are valid, I can't see anything in the c++ spec that would forbid this.

The first expression (`0+a`) works on clang, but fails on gcc (see [my bugzilla report](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126442)) and msvc.

But the second expression fails on all three compilers.
I checked various version on godbolt from latest trunks to the oldest it has, none seem to behave differently.

As a side note, this slightly different reproduction (without SFINAE) works on gcc/msvc but fails on clang:

```c++
struct T {
template
operator int() { return {}; }
};

int main ()
{
T a;

0+a; // fine on all
a+a; // error on clang only, gcc/msvc work fine
}
```

Contributor guide

Open the contributing guide

Research direction

No LLVM source file or test is identified. Start with the two minimal C++ reproductions and investigate clang's binary-operator overload resolution for templated conversion operators, comparing the results with the C++ standard and the other compiler behaviors. Done means the expected validity of both expressions is established and the clang behavior is addressed with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.