Is there any reason to use auto_ptr?

前端 未结 4 1161
一生所求
一生所求 2021-01-08 00:24

After reading Jossutis\' explanation on auto_ptr from his STL book I\'ve got a strong impression that whatever task I would try to use it in I\'d 100% fail becuase of one of

4条回答
  •  心在旅途
    2021-01-08 00:45

    Clearly, auto_ptr looses against unique_ptr.

    Now, in a 'strict C++03 without boost' world, I use auto_ptr quite often, most notably :

    • For 'factory member functions' which return a dynamically allocated instance of a given type : I like the fact that using std::auto_ptr in the return type explicits that the object must be deleted
    • In functions which allocate an object before attempting to insert it in a container afterwards : for example in order to release() only if std::map<>::insert returns that insertion succeeded
    • In a thread procedure which pops elements from a message queue, I like storing the pop'ed element in a const std::auto_ptr to make it clear that the message will be destroyed no matter what.

提交回复
热议问题