Get void * pointer to boost :: any content

I use an external library that has a method that accepts void *

I want this void * to point to the object contained in boost :: any object.

Is it possible to get the content address of boost :: any object?

I am trying to play with myAny.content but so far no luck! I hope some combination of dynamic_cast or unsafe_any_cast will give me what I need.

Thanks!

+7
source share
2 answers

This is impossible, unfortunately; boost::any_cast will refuse to cast if the type is different from the type it contains.

If you want to use unsupported internal hacking, the current version of the header has an undocumented and unsupported function boost::unsafe_any_cast , which (as the name implies) bypasses the type check performed by boost::any_cast :

 boost::any any_value(value); void *content = boost::unsafe_any_cast<void *>(&any_value); 

The header has something to say about unsafe_any_cast :

 // Note: The "unsafe" versions of any_cast are not part of the // public interface and may be removed at any time. They are // required where we know what type is stored in the any and can't // use typeid() comparison, eg, when our types may travel across // different shared libraries. 
+3
source

You can use boost::any_cast to get a pointer to the base type (if you know it at compile time).

 boost::any any_i(5); int* pi = boost::any_cast<int>(&any_i); *pi = 6; void* vpi = pi; 
+5
source

All Articles