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

            Bug ID: 115163
           Summary: Requesting better diagnostic for explicit constructor
                    failure
           Product: gcc
           Version: 14.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

Here's a simple program:

struct C {
    explicit C(int);
};

int main() {
    C c = 42;
}

gcc's error for this right now is:

<source>:6:11: error: conversion from 'int' to non-scalar type 'C' requested
    6 |     C c = 42;
      |           ^~

clang's error, on the other hand, is much more informative:

<source>:6:7: error: no viable conversion from 'int' to 'C'
    6 |     C c = 42;
      |       ^   ~~
<source>:1:8: note: candidate constructor (the implicit copy constructor) not
viable: no known conversion from 'int' to 'const C &' for 1st argument
    1 | struct C {
      |        ^
<source>:1:8: note: candidate constructor (the implicit move constructor) not
viable: no known conversion from 'int' to 'C &&' for 1st argument
    1 | struct C {
      |        ^
<source>:2:14: note: explicit constructor is not a candidate
    2 |     explicit C(int);
      |              ^

Ideally we could get something that was closer to this (i.e. given that
overload resolution succeeds and picks the explicit constructor if it were a
candidate, that's probably the right thing to tell the user):

<source>:6:7: error: no viable conversion from 'int' to 'C'
    6 |     C c = 42;
      |       ^   ~~
<source>:2:14: note: explicit constructor is not a candidate
    2 |     explicit C(int);
      |              ^
  • [Bug c++/115163] New: Requestin... barry.revzin at gmail dot com via Gcc-bugs

Reply via email to