C++ Logo

std-discussion

Advanced search

Re: Vector implementation of behavior when inserting objects whose copy constructor can throw

From: Brian Bi <bbi5291_at_[hidden]>
Date: Tue, 27 Jul 2021 17:22:42 -0400
On Tue, Jul 27, 2021 at 5:07 PM Jason McKesson via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> On Tue, Jul 27, 2021 at 3:47 PM Giuseppe D'Angelo via Std-Discussion
> <std-discussion_at_[hidden]> wrote:
> >
> > Hi,
> >
> > On 25/07/2021 20:45, Jason McKesson via Std-Discussion wrote:
> > > The
> > > standard makes it clear that if `T` is not CopyInsertable, and `T`'s
> > > move constructor throws, then the state of the `vector` is
> > > unspecified. But otherwise, the `vector<T>`'s state in the event of an
> > > exception is unchanged.
> >
> > The Standard puts a lot of requirements for this to happen; do you have
> > a reference to the relevant parts?
>
> Yes: https://timsong-cpp.github.io/cppwp/n4861/vector.modifiers#1
>
> From that section:
>
> > If an exception is thrown other than by the copy constructor, move
> constructor, assignment operator, or move assignment operator of T or by
> any InputIterator operation there are no effects. If an exception is thrown
> while inserting a single element at the end and T is Cpp17CopyInsertable or
> is_­nothrow_­move_­constructible_­v<T> is true, there are no effects.
> Otherwise, if an exception is thrown by the move constructor of a
> non-Cpp17CopyInsertable T, the effects are unspecified.
>
> I see no section here which allows "the effects are unspecified" if a
> copy constructor throws.
>

I think it's unspecified by omission. It doesn't say that if a copy
constructor throws and the insertion is not at the end, then there are no
effects.


>
> >
> > If you asked me randomly, I would've said that for a type 2 (throwing
> > copiable, throwing movable) the only guarantee (if any) could've been
> > the basic one, not the strong one...
> >
> > Thanks,
> > --
> > Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
> > KDAB (France) S.A.S., a KDAB Group company
> > Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
> > KDAB - The Qt, C++ and OpenGL Experts
> >
> > --
> > Std-Discussion mailing list
> > Std-Discussion_at_[hidden]
> > https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>


-- 
*Brian Bi*

Received on 2021-07-27 16:22:56