Skip to content

Latest commit

 

History

History
65 lines (52 loc) · 2.42 KB

File metadata and controls

65 lines (52 loc) · 2.42 KB
title Warning C26432
description Microsoft C++ Code Analysis warning C26432 for the C++ Core Guidelines case C.21.
ms.date 11/15/2017
f1_keywords
C26432
DEFINE_OR_DELETE_SPECIAL_OPS
helpviewer_keywords
C26432
ms.assetid f587b05a-5c69-4176-baa6-fcb79d228b24

Warning C26432

If you define or delete any default operation in the type 'type-name', define or delete them all (c.21).

C++ Core Guidelines:
C.21: If you define or =delete any default operation, define or =delete them all

Special operations such as constructors are assumed to alter the behavior of types so they rely more on language mechanisms to automatically enforce specific scenarios. The canonical example is resource management. If you explicitly define, default, or delete any of these special operations, it signals you want to avoid any special handling of a type. It's inconsistent to leave the other operations unspecified, that is, implicitly defined as deleted by the compiler.

Remarks

This check implements the rule of five, which treats the following operations as special:

  • copy constructors,
  • move constructors,
  • copy assignment operators,
  • move assignment operators, and
  • destructors.

The rule doesn't check if operations are defined in the same way. It's okay to mix deleted and defaulted operations with explicitly defined ones. However, you must specify all of them if you specify any of them.

Access levels aren't important and can also be mixed.

The warning flags the first non-static function definition of a type, once per type.

Example

In this example, warning::S defines only a default constructor and a destructor. The no_warning::S declaration defines or deletes all five special member functions.

// C26432.cpp
namespace warning
{
    struct S
    {
        S() noexcept { ++_count; }
        ~S() { --_count; } // C26432 because only the constructor and destructor are explicitly defined.
        static unsigned _count;
    };
    unsigned S::_count = 0;
}

namespace no_warning
{
    struct S
    {
        S() noexcept { _count++;  }
        S(const S&) = delete;
        S(S&&) = delete;
        S& operator=(const S&) = delete;
        S& operator=(S&&) = delete;
        ~S() { --_count; }
        static unsigned _count;
    };
    unsigned S::_count = 0;
}