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

前端 未结 4 549
青春惊慌失措
青春惊慌失措 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:41

    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.

提交回复
热议问题