Skip to content

Latest commit

 

History

History
61 lines (47 loc) · 2.37 KB

c26440.md

File metadata and controls

61 lines (47 loc) · 2.37 KB
title ms.date ms.topic f1_keywords helpviewer_keywords ms.assetid description
C26440
11/15/2017
conceptual
C26440
C26440
b6d2b188-35cc-4de2-878c-6f97d5a2444a
CppCoreCheck rule C26440 that enforces C++ Core Guidelines F.6

C26440 DECLARE_NOEXCEPT

"Function can be declared 'noexcept'."

C++ Core Guidelines F.6: If your function may not throw, declare it noexcept

If code is not supposed to cause any exceptions, it should be marked as such by using the 'noexcept' specifier. This would help to simplify error handling on the client code side, as well as enable compiler to do additional optimizations.

Remarks

  • A function is considered non-throwing if:
    • it has no explicit throw statements;
    • function calls in its body, if any, invoke only functions that unlikely to throw: constexpr or functions marked with any exception specification which entails non-throwing behavior (this includes some non-standard specifications).
  • Non-standard and outdated specifiers like throw() or declspec(nothrow) are not equivalent to 'noexcept'.
  • Explicit specifiers noexcept(false) and noexcept(true) are respected appropriately.
  • Functions marked as constexpr are not supposed to cause exceptions and are not analyzed.
  • The rule also applies to lambda expressions.
  • The logic doesn't consider recursive calls as potentially non-throwing. This may change in the future.

Example

All functions except the destructor will warn because they are missing noexcept.

struct S
{
    S() {} // C26455, Default constructor may not throw. Declare it 'noexcept'
    ~S() {}

    S(S&& s) {/*impl*/} // C26439, This kind of function may not throw. Declare it 'noexcept' (f.6)
    S& operator=(S&& s) {/*impl*/} // C26439, This kind of function may not throw. Declare it 'noexcept' (f.6)

    S(const S& s) {/*impl*/} // C26440, This function can be declared 'noexcept'
    S& operator=(const S& s) {/*impl*/} // C26440, This function can be declared 'noexcept'
};

With noexcept decorating the same structure, all warnings are removed.

struct S
{
    S() noexcept {}
    ~S() {}

    S(S&& s) noexcept {/*impl*/}
    S& operator=(S&& s) noexcept {/*impl*/}

    S(const S& s) noexcept {/*impl*/}
    S& operator=(const S& s) noexcept {/*impl*/}
};