AUTOSAR C++14 Rule A15-5-1
All user-provided class destructors, deallocation functions, move constructors, move assignment operators and swap functions shall not exit with an exception. A noexcept exception specification shall be added to these functions as appropriate
Since R2020b
Description
Rule Definition
All user-provided class destructors, deallocation functions, move constructors, move assignment operators and swap functions shall not exit with an exception. A noexcept exception specification shall be added to these functions as appropriate.
Rationale
This rule states that certain functions must not exit with an exception.
Destructors and deallocation functions: When an exception is raised, the compiler invokes the destructors and deallocation functions to safely delete the objects in the stack. If a destructor or a deallocation function exits with an exception at that time, the compiler terminates the program execution abnormally. Depending on the software or hardware that you use, abnormal program termination can result in resource leaks and security vulnerabilities. To prevent these issues, avoid destructors and deallocator functions that might exit with an exception. Default destructors and deallocators are
noexcept
functions. When you provide a custom destructor or deallocation function, specify them asnoexcept
and handle all exceptions within the function so that they do not exit with exceptions. For a polymorphic class hierarchy, this rule applies to the destructors of the base and all derived classes.Move constructors and move assignment operators: If a move constructor or a move assignment operator exits with an exception, it cannot be guaranteed that the program will revert to the state it was before the move operation. Avoid a move constructor or a move assignment operator that might exit with an exception. Specify these functions as
noexcept
because standard library functions might avoid move operations unless they are declared asnoexcept
. You can also declare these special member functions as=default
. For more information on when you can declare the special member functions as=default
, seeAUTOSAR C++14 Rule A12-0-1
.Swap functions: Developers expect that a swap function does not exit with an exception. If a swap function exits with an exception, standard library algorithms and copy operations might not work in your code as expected. Specify swap functions as
noexcept
. Avoid operations that might exit with an exception in swap functions.
When you use templates as generic move constructors, generic move assignment operators, and generic swap functions, these templates can have dynamic exception specifications without violating this rule.
Polyspace Implementation
Polyspace® flags a user-defined destructor, deallocation function, move constructor, move
assignment operator, and swap function if it might raise an exception. If a function is
named swap
or Swap
and takes a reference as input,
Polyspace considers it a swap function.
Polyspace ignores functions that are declared but not defined.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Exception handling |
Category: Required, Automated |
Version History
Introduced in R2020b