Re: Why std::vector requires noexcept move constructor?

Liste des GroupesRevenir à cl c++ 
Sujet : Re: Why std::vector requires noexcept move constructor?
De : news.5.maazl (at) *nospam* spamgourmet.org (Marcel Mueller)
Groupes : comp.lang.c++
Date : 16. Jun 2025, 19:50:28
Autres entêtes
Organisation : MB-NET.NET for Open-News-Network e.V.
Message-ID : <102pp1k$8g7$4@gwaiyur.mb-net.net>
References : 1 2
User-Agent : Mozilla Thunderbird
Am 16.06.25 um 19:00 schrieb Paavo Helde:
On 15.06.2025 11:25, Marcel Mueller wrote:
I discovered shortly that a move constructor that is not declared as noexcept is ignored by std::vector. Instead the objects are moved by the copy constructor.
 A move constructor is a way to squeeze out max performance from the code.
... or to return non-copyable objects etc.

Noexcept also helps in this regard and is usually trivial to add to move operations.
Indeed. But if only one member has a move constructor w/o noexcept any auto generated move constructor silently misses noexcept too. I shortly run into this pitfall, and this caused excessive copying of objects at insert operations (with reallocation).

Why?
The copy constructor is even more likely not declared as noexcept.
 Depending on the operation, it might be needed to restore the original state of existing data if an exception appears middle-way.
Ah, I wasn't aware that the operation is logically atomic.
This is of course impossible with exceptions in between.

With copy constructors the original state is not touched at all so recovering from an exception is much simpler.
Indeed.
Maybe a move constructor w/o noexcept should be a warning, because it makes no much sense to throw within a move operation.
Marcel

Date Sujet#  Auteur
15 Jun 25 * Why std::vector requires noexcept move constructor?5Marcel Mueller
16 Jun 25 +* Re: Why std::vector requires noexcept move constructor?3Paavo Helde
16 Jun 25 i`* Re: Why std::vector requires noexcept move constructor?2Marcel Mueller
17 Jun 25 i `- Re: Why std::vector requires noexcept move constructor?1Paavo Helde
1 Jul 25 `- Re: Why std::vector requires noexcept move constructor?1Bonita Montero

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal