Boost logo

Boost Users :

From: Daniel Krügler (dsp_at_[hidden])
Date: 2008-05-19 03:23:12


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net