You cannot invoke the A1(char* name) constructor A1(char* name) using a string literal because the string literal cannot be converted to char* (such an obsolete conversion existed before C ++ 11). Rather, the program calling the constructor is poorly formed, and implementations are allowed to refuse compilation.
Thus, overload resolution is looking for other alternatives. The only potential alternative that has the same number of arguments is a copy constructor.
For some reason, clang seems to prefer an implicit conversion from a string literal, A1 , thereby creating a temporary one that can be used to initialize copying using a direct literal construct. This behavior leads to a confusing compilation error.
Both alternatives are poorly formed, and clang warns about it accordingly: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings] . The program will compile if you set the standard mode to an older one than C ++ 11 (in this case, the program will be well-formed, even if it uses an obsolete conversion). Interestingly, if we refuse the conversion, then the program compiles even in the current standard mode:
class A1 { public: explicit A1(char* name){}
g ++ behaves differently and your program compiles fine (with the appropriate warning, of course). Both compilers seem to conform to the standard in this regard.
Moral of the story: always read the warnings. In this case, the warning was completely clear and easy to solve, while the same error indirectly caused an error that did not help solve the problem.
user2079303
source share