What inherent advantages do boost::any
and boost::any_cast
offer over using void*
and dynamic_cast
?

- 30,618
- 31
- 128
- 208
-
11You can't `dynamic_cast` from `void*` – Armen Tsirunyan Jan 06 '12 at 12:01
-
1@ArmenTsirunyan I always assumed that was possible to do, though I never tried it... :| – Paul Manta Jan 06 '12 at 12:04
-
1@PaulManta regarding the comment of Armen, you could have a look at this question http://stackoverflow.com/questions/6771998/dynamic-cast-of-void – Alessandro Teruzzi Jan 06 '12 at 12:12
-
The magic of boost::any comes with some performance cost, though. https://felipedelamuerte.wordpress.com/2012/04/06/why-you-shouldnt-use-boostany-especially-not-in-time-critical-code/ – Rémi Jan 25 '15 at 23:01
4 Answers
The advantage is that boost::any
is way more type-safe than void*
.
E.g.
int i = 5;
void* p = &i;
static_cast<double*>(p); //Compiler doesn't complain. Undefined Behavior.
boost::any a;
a = i;
boost::any_cast<double>(a); //throws, which is good
As to your comment, you cannot dynamic_cast
from a void*
. You can dynamic_cast
only from pointers and references to class types which have at least one virtual function (aka polymorphic types)

- 130,161
- 59
- 324
- 434
-
Yeah, but that's a static cast, not a dynamic cast. Your comment that you can't use `dynamic_cast` on `void*` was a better answer. Or was your point that there's nothing stopping you from using `static_cast` even though you shouldn't? :) – Paul Manta Jan 06 '12 at 12:06
-
boost::any
calls destructors:
{
boost::any x = std::string("Hello, world");
x = std::wstring(L"Goodbye"); // string::~string called here
} // wstring::~wstring called here

- 173,980
- 10
- 155
- 350
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.

- 4,413
- 1
- 19
- 29
This is what boost's reference says:
It supports copying of any value type and safe checked extraction of that value strictly against its type.
Neither of those can be done with a void*
. There are no checks for you and you have to know yourself what you can cast it to.
I don't think dynamic_cast
enters the picture at all, as it hasn't directly anything to do with either.

- 1,781
- 10
- 7