Boost logo

Boost Users :

Subject: Re: [Boost-users] [assign][ptr_container] Exception safety of ptr_list_of, ptr_push_back, ptr_map_insert, etc.
From: Aaron Levy (aaron.levy_at_[hidden])
Date: 2014-09-24 05:52:32


>> šI am trying to understand how ptr_list_of or ptr_push_back provide more exception safety.
>>
>> šI can write something like:
>>
>> šboost::ptr_vector<Foo> vec;
>> špush_back(vec)(new Foo)(new Foo);
>>
>> šIs the order of evaluation of the two "new Foo" expressions indeterminate here? If not, then if the second new throws, the destructor of vec should be invoked. The Foo object allocated by the first new and now part of vec should also get released as part of that.
>
> That would not be safe.
>
> The intention is that you can forward to the constructor such that the
> new expression happens inside the library:
>
> šššpush_back( vec )()();
>
> See
>
> http://www.boost.org/doc/libs/1_56_0_b1/libs/assign/doc/index.html#ptr_push_back
>

Yes I saw that the documentation says these are exception safe and understandably the arguments are forwarded to the constructors. I was just trying to understand the scenarios under which code like the example below cause exception-safety issues.

boost::ptr_vector<Foo> vec;
push_back(vec)(new Foo)(new Foo);

My reasoning was that the nth "new Foo" gets evaluated after the n-1 "new Foo" expressions which by then are already part of the container. So this is possibly no less exception-safe. But what is an example of a scenario where they are exception unsafe? Is it when the constructor takes arguments and some arguments are copied and one of the copy operations throws, or something like that?

Thanks.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net