Can I have polymorphic containers with value semantics in C++?

后端 未结 9 1354
半阙折子戏
半阙折子戏 2020-12-02 18:35

As a general rule, I prefer using value rather than pointer semantics in C++ (ie using vector instead of vector). Usuall

9条回答
  •  有刺的猬
    2020-12-02 19:24

    Yes, you can.

    The boost.ptr_container library provides polymorphic value semantic versions of the standard containers. You only have to pass in a pointer to a heap-allocated object, and the container will take ownership and all further operations will provide value semantics , except for reclaiming ownership, which gives you almost all the benefits of value semantics by using a smart pointer.

提交回复
热议问题