18

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

Paul Manta
  • 30,618
  • 31
  • 128
  • 208

4 Answers4

29

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)

Armen Tsirunyan
  • 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
  • @Paul: Added that part to my answer – Armen Tsirunyan Jan 06 '12 at 12:09
16

boost::any calls destructors:

{
    boost::any x = std::string("Hello, world");
    x = std::wstring(L"Goodbye"); // string::~string called here
} // wstring::~wstring called here
MSalters
  • 173,980
  • 10
  • 155
  • 350
9

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.

DXM
  • 4,413
  • 1
  • 19
  • 29
5

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.

visitor
  • 1,781
  • 10
  • 7