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

            Bug ID: 118282
           Summary: GCC fails to identify feasible type conversion scheme
                    between operands of conditional expression
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wangbopku15 at gmail dot com
  Target Milestone: ---

Consider the following code:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

struct A
{
  explicit A(int);
  operator void*() const;
};

void foo(const A& x) {
     auto res = 0 ? x : 0; 
}

int main(){
    A a{5};
    foo(a);
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The conditional expression inside 'foo' seems feasible. Although conversion
from type 'int' to type 'A' is forbidden due to the explicit declaration of A's
constructor, the conversion from type 'A' to type 'void *' using the overloaded
type conversion operator of 'A' is still fluent when the last operand '0' is
treated as a pointer.

GCC seems not to realize this by giving the following diagnostic:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<source>: In function 'void foo(const A&)':
<source>:8:19: error: operands to '?:' have different types 'const A' and 'int'
    8 |      auto res = 0 ? x : 0;
      |                 ~~^~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Clang, ICC, and EDG accept this. Note that clang will normally reject it if the
overloaded 'operator void*()' is removed.

Please see https://godbolt.org/z/v8ecGKPz1

Reply via email to