
Hi all, I have a class, call it "Property", containing a value, which can be of many different types, like int, string, bool, but also a pointer to binary data. So I decided to use boost::variant. The problem is that when I assign a pointer to the variant, it is implicitly casted to a bool. So I do not get the pointer out of the variant again. Is boost::variant not suitable for my needs or is there a recommended workaround? I will post some code to make my problem more clear. boost::variant<void*, bool, int, std::string> value; void* p; value = p; bool b = boost::get<bool>(value); //OK void* p2 = boost::get<void*>(value); //Error Thanks for your help Philipp

On 12/13/10 10:02, Philipp Hamann wrote:
Hi all, I have a class, call it "Property", containing a value, which can be of many different types, like int, string, bool, but also a pointer to binary data. So I decided to use boost::variant.
The problem is that when I assign a pointer to the variant, it is implicitly casted to a bool. So I do not get the pointer out of the variant again.
Is boost::variant not suitable for my needs or is there a recommended workaround?
I will post some code to make my problem more clear.
boost::variant<void*, bool, int, std::string> value; void* p; value = p; bool b = boost::get<bool>(value); //OK void* p2 = boost::get<void*>(value); //Error
Thanks for your help
Philipp
I think you'll have to explicitly cast the argument as shown in post: http://article.gmane.org/gmane.comp.lib.boost.user/59167 One other option is to wrap the actual type with a tag and then used those wrapped and tagged types as args to variant. IOW, instead of: variant<T1,T2,...,Tn> where for some i and j, Ti can be constructed from a Tj, you would have: variant<wrap<1,T1>,wrap<2,T2>,...,wrap<n,Tn> > where: template<int Tag, typename T> struct wrap { T my_t; wrap(T const& a_t):my_t(a_t){} }; At least that's the only way I can figure out to workaround this variant limitation. OTOH, there's an alternative, non-boost variant which doesn't suffer from this problem but does require one to be more explicit by requiring the use of an inject<Tag_i>(Ti) member function. The template is here: http://svn.boost.org/svn/boost/sandbox/variadic_templates/boost/composite_st... and the test driver is here: http://svn.boost.org/svn/boost/sandbox/variadic_templates/libs/composite_sto... Unfortunately, this alternative to variant might take more compile time, but I've not tested that. HTH. -Larry

AMDG On 12/13/2010 8:02 AM, Philipp Hamann wrote:
I have a class, call it "Property", containing a value, which can be of many different types, like int, string, bool, but also a pointer to binary data. So I decided to use boost::variant.
The problem is that when I assign a pointer to the variant, it is implicitly casted to a bool. So I do not get the pointer out of the variant again.
Is boost::variant not suitable for my needs or is there a recommended workaround?
I will post some code to make my problem more clear.
boost::variant<void*, bool, int, std::string> value; void* p; value = p; bool b = boost::get<bool>(value); //OK void* p2 = boost::get<void*>(value); //Error
It works fine for me with the current trunk. What version of Boost are you using and what compiler? In Christ, Steven Watanabe

On 12/13/10 12:35, Steven Watanabe wrote:
AMDG
On 12/13/2010 8:02 AM, Philipp Hamann wrote: [snip]
boost::variant<void*, bool, int, std::string> value; void* p; value = p; bool b = boost::get<bool>(value); //OK void* p2 = boost::get<void*>(value); //Error
It works fine for me with the current trunk. What version of Boost are you using and what compiler?
Works for me too. Apparently changes to boost::vartiant were made after the reference I provided in my reply. The code used to test it is attached after doing `svn update` which gave output: Updated to revision 67223. Sorry for noise of my other post. -Larry

Hi, thank you both for your quick and good help. It is working now for me, too. I am sorry. I was wrong. I was not very correct using const. This caused the problem, but not boost::variant. I thougt the void pointer was the problem, since I had a similar problem with strings. Sorry for bothering you. Philipp Am 13.12.2010 21:45, schrieb Larry Evans:
On 12/13/10 12:35, Steven Watanabe wrote:
AMDG
On 12/13/2010 8:02 AM, Philipp Hamann wrote: [snip]
boost::variant<void*, bool, int, std::string> value; void* p; value = p; bool b = boost::get<bool>(value); //OK void* p2 = boost::get<void*>(value); //Error
It works fine for me with the current trunk. What version of Boost are you using and what compiler?
Works for me too. Apparently changes to boost::vartiant were made after the reference I provided in my reply.
The code used to test it is attached after doing `svn update` which gave output:
Updated to revision 67223.
Sorry for noise of my other post.
-Larry
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Larry Evans
-
Philipp Hamann
-
Steven Watanabe