talles
talles

Reputation: 15134

noexcept specifier and compiler optimizations

I have read unclear things regarding the noexcept specifier and compiler optimizations. When specifying noexcept the compiler may optimize:

Upvotes: 9

Views: 2991

Answers (1)

mirk
mirk

Reputation: 5530

The original reason for noexpect was to enable libraries to use faster move-constructors internally, if the calling function is not allowed to throw by specification.

Next, big performance optimizations can be achieved in containers like STL vector when your type’s move constructor and move assignment are annotated with noexcept. When STL utility std::move_if_noexcept detects that your moves do not throw it will use these safe moves rather than copies for some operations (like resize). This, in case of containers storing millions of elements, will enable huge optimizations.

(quoted from using-noexcept)


Additionally, the compiler does not have to generate extra code for stack-unwinding, if it knows that no exceptions can be thrown due to a noexpect specifier.


I can't see how compile-time is substantially impacted by noexcept-specifiers. The resulting runtime can be a lot faster though.

Upvotes: 12

Related Questions