Using std::shared_ptr to point to anything

后端 未结 4 1949
醉梦人生
醉梦人生 2020-12-16 23:42

I\'m using a std::shared_ptr in my application to make a smart pointer which can point to many different types of data structures like structs, vect

4条回答
  •  执念已碎
    2020-12-17 00:19

    If you store void*, then at every use point you need to know the exact type you put in, as a cast-to-void*-and-back is only valid if you go to/from the exact same type (not derived-to-void-to-base, for example).

    Given that every use point will require knowing the type of the stored pointer, why round trip to void at all?

    Have one map per type you store. This has modest overhead over one map per application (O(number of types) memory at run time, similar compile time).

    With C++ templates there will be little code duplication.

提交回复
热议问题