Passing object ownership in C++

后端 未结 1 1732
醉酒成梦
醉酒成梦 2021-02-07 16:42

What is the best way to indicate that an object wants to take ownership of another object? So far, I\'ve been using a std::auto_ptr in the public interface, so the

1条回答
  •  北恋
    北恋 (楼主)
    2021-02-07 16:59

    boost::interprocess is a library for interprocess communication, so I wouldn't use it for different purposes.

    As discussed on this forum:

    http://objectmix.com/c/113487-std-auto_ptr-deprecated.html

    std::auto_ptr will be declared deprecated in the next version of the standard, where it will be recommended the usage of std::unique_ptr, which requires rvalue references and move semantics to be implemented (that's a fairly complicated feature).

    Until the new standard is released, I would simply try to disable the warning if possible, or ignore it, for maximum portability.

    If you want to already switch to the next language standard, it is possible since rvalue references have been implemented (see http://russ.yanofsky.org/rref/), so also std::unique_ptr should be supported.

    On of the advantages of the new semantics is that you can pass to the move constructor also a temporary or any rvalue; in other cases, this allows avoiding to copy (for instance) objects contained inside a std::vector (during reallocation) before destroying the original ones.

    0 讨论(0)
提交回复
热议问题