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.
Why?
The copy constructor is even more likely not declared as noexcept.
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.
Noexcept also helps in this regard and is usually trivial to add
to move operations.
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.
With copy constructors the original state is not touched at all so recovering from an exception is much simpler.
Maybe a move constructor w/o noexcept should be a warning, because it
makes no much sense to throw within a move operation.
------ Build started: Project: ConsoleTest2022, Configuration: Debugx64 ------
main.cppC4625: 'bar': copy constructor was implicitly defined as deleted 1>C:\Test\ConsoleTestVS2022\ConsoleTest2022\main.cpp(6,7): warning
C:\Test\ConsoleTestVS2022\ConsoleTest2022\main.cpp(6,7): warning
Done building project "ConsoleTest2022.vcxproj".========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== ========== Build completed at 08:43 and took 01,440 seconds ==========
Sysop: | Tetrazocine |
---|---|
Location: | Melbourne, VIC, Australia |
Users: | 8 |
Nodes: | 8 (0 / 8) |
Uptime: | 115:33:09 |
Calls: | 161 |
Files: | 21,502 |
Messages: | 78,817 |