Files
2025-10-25 03:02:53 +03:00

4.8 KiB

[dcl.fct.def.delete]

9 Declarations [dcl]

9.6 Function definitions [dcl.fct.def]

9.6.3 Deleted definitions [dcl.fct.def.delete]

1

#

A deleted definition of a function is a function definition whose function-body is a deleted-function-body or an explicitly-defaulted definition of the function where the function is defined as deleted.

A deleted function is a function with a deleted definition or a function that is implicitly defined as deleted.

2

#

A construct that designates a deleted function implicitly or explicitly, other than to declare it or to appear as the operand of a reflect-expression ([expr.reflect]), is ill-formed.

Recommended practice: The resulting diagnostic message should include the text of the unevaluated-string, if one is supplied.

[Note 1:

This includes calling the function implicitly or explicitly and forming a pointer or pointer-to-member to the function.

It applies even for references in expressions that are not potentially-evaluated.

For an overload set, only the function selected by overload resolution is referenced.

The implicit odr-use ([basic.def.odr]) of a virtual function does not, by itself, constitute a reference.

The unevaluated-string, if present, can be used to explain the rationale for deletion and/or to suggest an alternative.

— end note]

3

#

[Example 1:

One can prevent default initialization and initialization by non-doubles withstruct onlydouble { onlydouble() = delete; // OK, but redundanttemplate onlydouble(T) = delete; onlydouble(double);};

— end example]

[Example 2:

One can prevent use of a class in certain new-expressions by using deleted definitions of a user-declared operator new for that class.

struct sometype {void* operator new(std::size_t) = delete; void* operator new = delete;}; sometype* p = new sometype; // error: deleted class operator new sometype* q = new sometype[3]; // error: deleted class operator new[] — end example]

[Example 3:

One can make a class uncopyable, i.e., move-only, by using deleted definitions of the copy constructor and copy assignment operator, and then providing defaulted definitions of the move constructor and move assignment operator.

struct moveonly { moveonly() = default; moveonly(const moveonly&) = delete; moveonly(moveonly&&) = default; moveonly& operator=(const moveonly&) = delete; moveonly& operator=(moveonly&&) = default; ~moveonly() = default;}; moveonly* p; moveonly q(*p); // error: deleted copy constructor — end example]

4

#

A deleted function is implicitly an inline function ([dcl.inline]).

[Note 2:

The one-definition rule ([basic.def.odr]) applies to deleted definitions.

— end note]

A deleted definition of a function shall be the first declaration of the function or, for an explicit specialization of a function template, the first declaration of that specialization.

An implicitly declared allocation or deallocation function ([basic.stc.dynamic]) shall not be defined as deleted.

[Example 4: struct sometype { sometype();}; sometype::sometype() = delete; // error: not first declaration — end example]