Why is `boost::any` better than `void*`?

∥☆過路亽.° 提交于 2019-12-02 20:51:12

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)

boost::any calls destructors:

{
    boost::any x = std::string("Hello, world");
    x = std::wstring(L"Goodbye"); // string::~string called here
} // wstring::~wstring called here

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!