| Deutsch English Français Italiano |
|
<102pp1k$8g7$4@gwaiyur.mb-net.net> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!2.eu.feeder.erje.net!feeder.erje.net!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail From: Marcel Mueller <news.5.maazl@spamgourmet.org> Newsgroups: comp.lang.c++ Subject: Re: Why std::vector requires noexcept move constructor? Date: Mon, 16 Jun 2025 20:50:28 +0200 Organization: MB-NET.NET for Open-News-Network e.V. Message-ID: <102pp1k$8g7$4@gwaiyur.mb-net.net> References: <102m01r$3knoc$1@gwaiyur.mb-net.net> <102pij2$1op2f$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 16 Jun 2025 18:50:28 -0000 (UTC) Injection-Info: gwaiyur.mb-net.net; logging-data="8711"; mail-complaints-to="abuse@open-news-network.org" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:pQ4Fivypof7EKboBMWr2z/ss18c= sha256:U+yGwQN/S/vUWxXg2LQMf8Ddxr5zx9zWKFDP0UkoPF0= sha1:FHhoqydxoSSQLCFyeLIuMbjMGB8= sha256:sMknnjhNeaWp1LQhU9dq865lQ4EgiI3mNgvi/DhMHvk= Content-Language: en-US In-Reply-To: <102pij2$1op2f$1@dont-email.me> Bytes: 2458 Lines: 39 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