[ptr_container] usage of auto_type
Hello, I have the following code fragment: typedef boost::ptr_vector<T> vector_t; typedef message_vector_t::auto_type auto_t; .... vector_t v; { auto_t x(new T) // do something with x v.push_back(x.release()); // here rise my question } according http://www.boost.org/doc/libs/1_53_0/libs/ptr_container/doc/examples.html #6 Do I have to release x all the time? E.g. I want to use it in the loop, where at the end I have to push_back the value, where for my understanding x goes out of scope and hence is released automatically. After studying the header, there is no push_back for auto_type (which is static_move_ptr), but for std::auto_ptr (which is imo deprecated in C++11) and T (which converts it into an auto_type again). Is it correct or do I miss something?
On 08-07-2014 22:32, Olaf Peter wrote:
Hello,
I have the following code fragment:
typedef boost::ptr_vector<T> vector_t; typedef message_vector_t::auto_type auto_t;
.... vector_t v; { auto_t x(new T) // do something with x v.push_back(x.release()); // here rise my question }
according http://www.boost.org/doc/libs/1_53_0/libs/ptr_container/doc/examples.html #6
Do I have to release x all the time?
E.g. I want to use it in the loop, where at the end I have to push_back the value, where for my understanding x goes out of scope and hence is released automatically. After studying the header, there is no push_back for auto_type (which is static_move_ptr), but for std::auto_ptr (which is imo deprecated in C++11) and T (which converts it into an auto_type again). Is it correct or do I miss something?
You are not missing anything AFAICT. The library has not been upgraded yet to take advantage of C++11. That process would add move-construction/assignment to the containers and expose auto_type as std::unique_ptr<T> as well as provide push_back overload for unique_ptr. In your example above, if you want to avoid calling release, simply use std::auto_ptr<T>. -Thorsten
participants (2)
-
Olaf Peter
-
Thorsten Ottosen