On Sun, Jul 26, 2015 at 11:53 AM, Benedek Thaler
On Sun, Jul 26, 2015 at 5:54 PM, Glen Fernandes
wrote: Some feedback:
Thanks for the feedback!
1. For a given type T whose constructor can throw, and you have a loop constructing n objects of type T via std::allocator_traits<T>::construct - should you not be handling that potential construction failure? (i.e. Destroying those elements that were constructed before the constructor that throws, before re-throwing)
[snip]
(My apologies if you already do this, and I missed it).
There are two such constructors:
explicit devector(size_type n, const Allocator& allocator = Allocator()) devector(size_type n, const T& value, const Allocator& allocator = Allocator())
The first one has a `construction_guard` which takes care cleanup on failure. The second one calls `opt_copy` at the end, which also has the same kind of guard if needed.
The failure scenario for both constructor (and for other metods) is tested:
https://github.com/erenon/container/blob/devector/test/devector_test.cpp#L22...
https://github.com/erenon/container/blob/devector/test/devector_test.cpp#L26...
Did I missed something?
An alternate approach, if you can require C++11, is to use "delegating constructors". See libc++'s dynarray implementation for an example of this. ( https://android.googlesource.com/platform/ndk/+/5de42e6621b3d0131472c3f8838b..., default constructor at line 138, other constructor at line 231) Basic idea: * Call the default constructor first. That can be noexcept. * Insert the elements, one by one, maintaining the class invariants. * If one of the insertions throws, then the destructor will be called, and it can do the cleanup. -- Marshall