120 lines
4.5 KiB
Markdown
120 lines
4.5 KiB
Markdown
[special]
|
||
|
||
# 11 Classes [[class]](./#class)
|
||
|
||
## 11.4 Class members [[class.mem]](class.mem#special)
|
||
|
||
### 11.4.4 Special member functions [special]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1154)
|
||
|
||
Default constructors ([[class.default.ctor]](class.default.ctor "11.4.5.2 Default constructors")),
|
||
copy constructors, move constructors ([[class.copy.ctor]](class.copy.ctor "11.4.5.3 Copy/move constructors")),
|
||
copy assignment operators, move assignment operators ([[class.copy.assign]](class.copy.assign "11.4.6 Copy/move assignment operator")),
|
||
and prospective destructors ([[class.dtor]](class.dtor "11.4.7 Destructors")) are[*special member functions*](#def:special_member_functions)[.](#1.sentence-1)
|
||
|
||
[*Note [1](#note-1)*:
|
||
|
||
The implementation will implicitly declare these member functions for some class
|
||
types when the program does not explicitly declare them[.](#1.sentence-2)
|
||
|
||
The implementation will implicitly define them
|
||
as needed ([[dcl.fct.def.default]](dcl.fct.def.default "9.6.2 Explicitly-defaulted functions"))[.](#1.sentence-3)
|
||
|
||
â *end note*]
|
||
|
||
An implicitly-declared special member function is declared at the closing} of the [*class-specifier*](class.pre#nt:class-specifier "11.1 Preamble [class.pre]")[.](#1.sentence-4)
|
||
|
||
Programs shall not define implicitly-declared special member functions[.](#1.sentence-5)
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1175)
|
||
|
||
Programs may explicitly refer to implicitly-declared special member functions[.](#2.sentence-1)
|
||
|
||
[*Example [1](#example-1)*:
|
||
|
||
A program may explicitly call or form a pointer to member
|
||
to an implicitly-declared special member function[.](#2.sentence-2)
|
||
|
||
struct A { }; // implicitly declared A::operator=struct B : A { B& operator=(const B &);};
|
||
B& B::operator=(const B& s) {this->A::operator=(s); // well-formedreturn *this;} â *end example*]
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1193)
|
||
|
||
[*Note [2](#note-2)*:
|
||
|
||
The special member functions affect the way objects of class type are created,
|
||
copied, moved, and destroyed, and how values can be converted to values of other types[.](#3.sentence-1)
|
||
|
||
Often such special member functions are called implicitly[.](#3.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
[4](#4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1200)
|
||
|
||
Special member functions obey the usual access rules ([[class.access]](class.access "11.8 Member access control"))[.](#4.sentence-1)
|
||
|
||
[*Example [2](#example-2)*:
|
||
|
||
Declaring a constructor protected
|
||
ensures that only derived classes and friends can create objects using it[.](#4.sentence-2)
|
||
|
||
â *end example*]
|
||
|
||
[5](#5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1208)
|
||
|
||
Two special member functions are of the same kind if
|
||
|
||
- [(5.1)](#5.1)
|
||
|
||
they are both default constructors,
|
||
|
||
- [(5.2)](#5.2)
|
||
|
||
they are both copy or move constructors
|
||
with the same first parameter type, or
|
||
|
||
- [(5.3)](#5.3)
|
||
|
||
they are both copy or move assignment operators
|
||
with the same first parameter type
|
||
and the same [*cv-qualifier*](dcl.decl.general#nt:cv-qualifier "9.3.1 General [dcl.decl.general]")*s* and [*ref-qualifier*](dcl.decl.general#nt:ref-qualifier "9.3.1 General [dcl.decl.general]"), if any[.](#5.sentence-1)
|
||
|
||
[6](#6)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1219)
|
||
|
||
An [*eligible special member function*](#def:special_member_function,eligible "11.4.4 Special member functions [special]") is a special member function for which:
|
||
|
||
- [(6.1)](#6.1)
|
||
|
||
the function is not deleted,
|
||
|
||
- [(6.2)](#6.2)
|
||
|
||
the associated constraints ([[temp.constr]](temp.constr "13.5 Template constraints")), if any, are satisfied, and
|
||
|
||
- [(6.3)](#6.3)
|
||
|
||
no special member function of the same kind
|
||
whose associated constraints, if any, are satisfied
|
||
is more constrained ([[temp.constr.order]](temp.constr.order "13.5.5 Partial ordering by constraints"))[.](#6.sentence-1)
|
||
|
||
[7](#7)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L1229)
|
||
|
||
For a class, its non-static data members, its non-virtual direct base classes,
|
||
and, if the class is not abstract ([[class.abstract]](class.abstract "11.7.4 Abstract classes")), its virtual base
|
||
classes are called its [*potentially constructed subobjects*](#def:potentially_constructed_subobjects)[.](#7.sentence-1)
|