Because the implicit conversion from const char*
to bool
is qualified as standard conversion, while const char*
to std::string
is user-defined conversion. The former has higher ranking and wins in overload resolution.
A standard conversion sequence is always better than a user-defined conversion sequence or an ellipsis conversion sequence.
BTW: mystruct obj(c);
performs direct initialization, explicit
converting constructors including mystruct::mystruct(bool)
are considered too. As the result, c
is converted to bool
then passed to mystruct::mystruct(bool)
as argument to construct obj
.
Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and non-explicit user-defined conversion functions, while direct-initialization considers all constructors and all user-defined conversion functions.
About explicit
specifier,
- Specifies that a constructor
or conversion function (since C++11)
or deduction guide (since C++17)
is explicit, that is, it cannot be used for implicit conversions and copy-initialization.
bool
values. – Healallexplicit
, why not it is referring to the type? – Vandivermystruct obj = c;
), or usedc
in another context where amystruct
object was expected (like calling a function expecting amystruct
object by value). – Healallstd::enable_if
to select the desired constructor in this case. – Oologyexplicit
disables implicit conversion ofX
tomystruct
, not of implicit conversion of arguments types to constructor. – Aerostationexplicit
meant also for argument type to constructor – Vandiver