https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122094
Bug ID: 122094
Summary: Imprecise error message when using copy assignment or
conversion, and the constructor is explicit
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: sandberg.sven at gmail dot com
Target Milestone: ---
The error message when assigning to an object whose copy constructor is
explicit is a little bit imprecise (https://godbolt.org/z/P78WKec93):
```
class C {
public:
C() {}
explicit C(const C &) {}
};
int main() {
C c1;
C c2 = c1;
}
```
The reason that the copy constructor can't be used is that it is explicit, but
gcc simply says "ignored" which may be hard to interpret:
```
<source>: In function 'int main()':
<source>:9:12: error: no matching function for call to 'C::C(C&)'
9 | C c2 = c1;
| ^~
• there are 2 candidates
• candidate 1: 'C::C()'
<source>:3:5:
3 | C() {}
| ^
• candidate expects 0 arguments, 1 provided
• candidate 2: 'C::C(const C&)' (ignored)
<source>:4:14:
4 | explicit C(const C &) {}
| ^
```
Similarly, for converting constructors, the message does not point out why the
candidate is not viable (https://godbolt.org/z/TTG4rb936):
```
class C {
public:
C() {}
explicit C(const int &) {}
};
int main() {
C c = 1;
}
```
gcc just says "conversion from 'int' to non-scalar type 'C' requested" but does
not say why that is wrong:
```
<source>: In function 'int main()':
<source>:8:11: error: conversion from 'int' to non-scalar type 'C' requested
8 | C c = 1;
| ^
```
The reason for the error is sometimes less obvious when there are multiple
conversion functions and/or type constraints.
In both cases, clang gives a clearer message, listing the candidates and
annotating the explicit constructor with "explicit constructor is not a
candidate".