This question already has an answer here:
- boost::variant - why is “const char*” converted to “bool”? 2 answers
The Boost Variant documentation says the following of the constructor that accepts arbitrary type:
template<typename T> variant(T & operand);
- Requires: T must be unambiguously convertible to one of the bounded types (i.e., T1, T2, etc.).
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:
- Why does this code compile?
- How does it choose the type (as
const char*
is convertible to bothstd::string
andbool
)?