Expansion of the ambiguous design option

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 )?
+8
c ++ boost boost-variant
source share
1 answer

As a rule, user transformations lose the process of resolving overloads to standard transformations.

There is a built-in conversion of const char pointers to bool , which is preferable to a built-in conversion from const char * to std::string (for example, see Implicit conversions ).

std::string , while part of the standard library is not a built-in type, therefore its conversion constructors are taken into account only after conversion to built-in types.

Some links:

  • Why does the compiler select a bool string on top of the string to implicitly cast to type L ""?
  • Why is my overloaded C ++ constructor not being called?
+2
source share

All Articles