Skip to content

Latest commit

 

History

History
61 lines (47 loc) · 2.31 KB

c26439.md

File metadata and controls

61 lines (47 loc) · 2.31 KB
title ms.date ms.topic f1_keywords helpviewer_keywords ms.assetid description
C26439
11/15/2017
conceptual
C26439
C26439
9df2a1b0-ea94-4884-9d28-c1522ec33a1b
CppCoreCheck rule C26439 that enforces C++ Core Guidelines F.6

C26439 SPECIAL_NOEXCEPT

"This kind of function may not throw. Declare it 'noexcept'."

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

Some kinds of operations should never cause exceptions. Their implementations should be reliable and should handle possible errors conditions gracefully. They should never use exceptions to indicate failure. This rule flags cases where such operations are not explicitly marked as 'noexcept' which means that they may throw exceptions and cannot convey assumptions about their reliability.

Remarks

  • Special kinds of operations are the following:
    • destructors;
    • default constructors;
    • move constructors and move assignment operators;
    • standard functions with move semantics: std::move and std::swap.
  • 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.
  • The warning may still appear for operations that are marked as constexpr. This may change in future releases.

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*/}
};