What inherent advantages do boost::any
and boost::any_cast
offer over using void*
and dynamic_cast
?
somehow nobody mentioned that boost::any<> acts as a value type, where as void* is a pointer. That means that any<> can store ANY object of ANY size. Once stored, you can pass the variable of any<> type anywhere you want; it lives on it's own.
On the other hand void* is of pointer size, so you either have to make sure that sizeof (your data) <= sizeof(void*) or your void* is just a pointer to a real data which is stored somewhere else. But in that case, it is completely different from any<> because now you need to worry about that "somewhere else" and make sure it stays valid as long as void* is valid, which sometimes could become a challenge, especially in multithreaded applications.
Plus as other's have mentioned any<> is very type safe, it'll store anything you want, but the only way to get it back is to know the exact type or it fails (which can be kinda annoying when one API gives you unsigned int and your code wants int are treated as different types). void* will let you do anything you want with it and if you start reading or paving over heap and/or uninitialized memory, it won't stop you or even let you know that you are doing that.