The Boost Variant documentation says the following constructor, which takes an arbitrary type:
template<typename T> variant(T & operand);
- Required: T must be uniquely convertible to one of the restricted types (i.e. T1, T2, etc.).
The same goes for constructors that accept const T& and T&& . Therefore, I expect the following code to not compile:
boost::variant<std::string, bool> v = "text";
But compiling the code and v becomes bool, which I definitely did not want. Of course, the solution is to wrap the string literal in the constructor of std::string . My question is:
- Why is this code compiling?
- How does he choose the type (since
const char* converted to both std::string and bool )?
c ++ boost boost-variant
petersohn
source share