I looked through a lot of questions related to the conversion, but it seems that none of them discussed the explicit keyword in this way. Here is the code:
struct B; struct A{ A(const B&){ cout << 1; }
Result: (Y: uncommented, N: commented, X: either)
# | *1 | *2 | *3 |output| 1 | N | N | N |error | 2 | N | N | Y | 1 | 3 | N | Y | N | 1 | 4 | N | Y | Y | 1 | 5 | Y | N | N | 2 | 6 | Y | N | Y | 1 | 7 | Y | Y | N |error | 8 | Y | Y | Y | 1 |
1, 7 are errors that are normal (ambiguous and automatic conversion)
2 the constructor seems to have a higher priority, but why?
3, 5 is easy to understand.
4 is strange since it does not cause explicit. why?
6 may be called by an explicit or higher priority constructor. What reason? 8 it looks like the constructor has a higher priority, but why?
Can someone offer some explanation? Thanks!
c ++ explicit type-conversion
Asu
source share