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

前端 未结 4 543
青春惊慌失措
青春惊慌失措 2021-02-01 07:50

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

4条回答
  •  迷失自我
    2021-02-01 08:42

    boost::any calls destructors:

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

提交回复
热议问题