boost::variant implictly change a pointer type to bool

Can someone give me a hint about this. If I define a variant type boost::variant<double,std::string> var; and it will not compile if I do the following: int an_int(8); int* p_int(&an_int); var=p_int; std::cout << var.which() << std::endl; But if I define the type including bool: boost::variant<double,std::string,bool> var; the above code can compile and prints 2. It seems that when type bool is in the list, any pointer assigned to var will convert to a bool. The bad thing is that the compiler will not complain about it. Is there a way to prevent the assignment of a pointer type to a boost::variant variable? I am new to boost and I would be very appreciated if someone can help me on this. Yanbin Zhang

Zhang, Yanbin wrote:
Can someone give me a hint about this.
If I define a variant type boost::variant<double,std::string> var; and it will not compile if I do the following: int an_int(8); int* p_int(&an_int); var=p_int; std::cout << var.which() << std::endl;
But if I define the type including bool: boost::variant<double,std::string,bool> var; the above code can compile and prints 2. It seems that when type bool is in the list, any pointer assigned to var will convert to a bool. The bad thing is that the compiler will not complain about it. Is there a way to prevent the assignment of a pointer type to a boost::variant variable? I am new to boost and I would be very appreciated if someone can help me on this.
Unfortunately this must be nominated as a programmer error, because the implicit pointer->bool conversion is part of the C++ language and is natural from the perspective of the language. To make this a bit clearer, consider the following C++ program: #include <string> int main() { int* pi = 0; bool b = pi; // OK double d = pi; // Error std::string s = pi; // Error } So, if you compare this with the behavior of boost::variant, it becomes clear that this class template behaves as the language would behave for individual entities. HTH & Greetings from Bremen, Daniel Krügler
participants (2)
-
Daniel Krügler
-
Zhang, Yanbin