There is a quite (very) popular question about the usage of noexcept
, but it's from 8 years ago, and other popular answers are just as old. Seeing some code from other people in different projects, it seems to me that noexcept
didn't become a very common thing for many developers to write.
Besides move constructor and move assignment operator, the benefit of using noexcept
doesn't seems clear enough to think about for every function ("should I add noexcept
here or not?").
Has somebody found a good usage of this specifier, and can share their experience?
In general, you should use noexcept when you think it will actually be useful to do so. Some code will take different paths if is_nothrow_constructible is true for that type. If you're using code that will do that, then feel free to noexcept appropriate constructors.
First, because noexcept functions are no-throw/no-fail, they implicitly meet the criteria for the strong exception guarantee . Thus, a noexcept move constructor is guaranteed to succeed. Second, we can use the standard library function std::move_if_noexcept() to determine whether a move or a copy should be performed.
E. 12: Use noexcept when exiting a function because of a throw is impossible or unacceptable. you don't care in case of an exception. You are willing to crash the program because you can not handle an exception such as std::bad_alloc due to memory exhaustion.
Inheriting constructors and the implicitly-declared default constructors, copy constructors, move constructors, destructors, copy-assignment operators, move-assignment operators are all noexcept(true) by default, unless they are required to call a function that is noexcept(false) , in which case these functions are ...
The initialisation of a container may cheap move the elements into the container if the move constructor is declared as noexcept. If not declared as noexcept, the elements may be expensive copied into the container. Each function in C++ is either non-throwing or potentially throwing.
The noexcept operator does not evaluate the expression. It can be used in a noexcept specifier of a function template to declare that the function may throw exceptions depending on the current type. To make my description clear here is a simple example of a function template which copies it return value.
The inner noexcept ist the noexcept operator and the outer the noexcept specifier. The expression noexcept (T (src)) checks in this case if the copy constructor is non-throwing. This is the case for the class Noexcept (2) but not for the class NonNoexcept (3) b ecause of the copy constructor of std::vector that may throw.
In particular, the expression noexcept (noexcept (T (src)). The inner noexcept ist the noexcept operator and the outer the noexcept specifier. The expression noexcept (T (src)) checks in this case if the copy constructor is non-throwing.
Besides the answers in the question that you linked (which continue to be relevant today), there are quite a few specific cases called out in the C++ Core Guidelines that you might want to check out:
noexcept
noexcept
noexcept
swap
noexcept
==
symmetric with respect of operand types and noexcept
hash
noexcept
noexcept
when exiting a function because of a throw
is impossible or unacceptableIt's not a topic to easily summarize as noted by the continued lack of content (as of 03-2021) in the Discussion: Usage of noexcept
section.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With