Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I write noexcept only to constructors and move operators?

Tags:

c++

c++11

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?

like image 244
Bruice Avatar asked Mar 30 '21 22:03

Bruice


People also ask

Should constructors be Noexcept?

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.

Why should move constructors be Noexcept?

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.

When should you use Noexcept?

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.

Is default constructor a Noexcept move?

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 ...

What happens if the move constructor is not declared as noexcept?

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.

What does the noexcept operator do in C++?

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.

What is the difference between inner noexcept and outer noexcept?

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.

How to check if the copy constructor is non-throwing?

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.


1 Answers

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:

  • F.6: If your function must not throw, declare it noexcept
  • C.37: Make destructors noexcept
  • C.66: Make move operations noexcept
  • C.85: Make swap noexcept
  • C.86: Make == symmetric with respect of operand types and noexcept
  • C.89: Make a hash noexcept
  • E.12: Use noexcept when exiting a function because of a throw is impossible or unacceptable

It'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.

like image 183
Woodford Avatar answered Oct 06 '22 19:10

Woodford