https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126493

            Bug ID: 126493
           Summary: Incorrect overload resolution when an explicit bool
                    conversion operator exists
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rextse.work at gmail dot com
  Target Milestone: ---

Considering the following code snippet:

#include <iostream>

struct A {
    int a;
    template <typename U>
    operator U() { return a; }
    //explicit operator bool() const { return a; } // (1)
};

int main() {
    A a{100};
    a + 1.2; // (2)
}

The code does not compile as expected, albeit for the wrong reason:
error: no match for 'operator+' (operand types are 'A' and 'double')
   12 |     a + 1.2;
      |     ~ ^ ~~~
      |     |   |
      |     A   double

GCC should instead consider all built-in operator+ overloads and rejects for
ambiguous overload. However, I believe this has been reported before and is not
the focus of the bug report.

Actual behaviour

    GCC rejects the first program.

    GCC accepts the second solely because explicit operator bool() is present.

Why this is incorrect

The interesting thing happens when you uncomment (1), after which GCC suddenly
accepts the code. As contextual conversion to bool does not apply here, the
line should have no impact on overload resolution. (2) is not incorrectly
chosen by overload resolution either, as when you change (2) to std::cout << a
+ 1.2, 101.2 is printed.

Clang rejects both versions for ambiguous overload as expected.
  • [Bug c++/126493] New: Incorrect ... rextse.work at gmail dot com via Gcc-bugs

Reply via email to