The Boost Variant documentation says the following of the constructor that accepts arbitrary type:
template<typename T> variant(T & operand);
The same is true of the constructors accepting const T&
and T&&
. So I expect that the following code won't compile:
boost::variant<std::string, bool> v = "text";
But the code compile, and v
becomes a bool, which is something I definitely did not want. Of course the solution is to wrap the string literal in a std::string
constructor. My question is:
const char*
is convertible to both std::string
and bool
)?Generally, user-defined conversions lose overload resolution process to standard conversions.
There is a built-in conversion from const char
pointers to bool
which is preferred over the non-built-in conversion from const char *
to std::string
(e.g. see Implicit conversions).
std::string
, while part of the standard library, is not a built-in type so its conversion constructors are considered only after conversions to built-in types.
Some references:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With