Edit

Share via


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