Boost logo

Boost :

Subject: Re: [boost] devector feedback request
From: Marshall Clow (mclow.lists_at_[hidden])
Date: 2015-07-30 11:01:48


On Sun, Jul 26, 2015 at 11:53 AM, Benedek Thaler <thalerbenedek_at_[hidden]>
wrote:

> On Sun, Jul 26, 2015 at 5:54 PM, Glen Fernandes <glen.fernandes_at_[hidden]>
> 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#L229
>
> https://github.com/erenon/container/blob/devector/test/devector_test.cpp#L261
>
> 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/+/5de42e6621b3d0131472c3f8838b7f0ccf3e8963/sources/cxx-stl/llvm-libc++/libcxx/include/experimental/dynarray,
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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk