Classes
A guide to Clause 11: how class definitions are written and their type-level properties, what members a class can contain, which special member functions the compiler generates for you, and where inheritance, access control, initialization and defaulted comparisons are covered.
Key points
- A class is a complete type at its closing brace
}; but inside a complete-class context, such as a function body or a default argument, the whole class is visible even from within the class. - The special member functions (construction, copy, move, assignment, destruction) can be implicitly declared and defined by the compiler; whether they are trivial determines whether objects can be copied byte by byte.
- Three properties, trivially copyable, standard-layout and implicit-lifetime, determine whether you can
memcpy, whetheroffsetofis usable, and whether objects can be used directly in memory obtained frommalloc. - An implicit conversion goes through at most one user-defined conversion (a converting constructor or a conversion function).
- Suggested order: [class.pre] → [class.mem] → [special] and its subclauses → [class.prop], then inheritance, access control and initialization as needed.
Class definitions: rules at the type level#
A class is a type. Two classes with exactly the same members are still distinct types ([class.name]/1). [class.pre] covers the syntax of a class definition (the three forms class, struct, union, and final), and the identity of the class's own name inside the class, the injected-class-name.
A class is complete at }: from there on sizeof, defining objects and accessing members all work, even if not a single member function has been defined yet. Inside the class definition, function bodies, default arguments, default member initializers, noexcept specifiers and contract specifiers are complete-class contexts ([class.mem.general]/10), where members declared later can be used. That is why a member function body can call a member function written below it, while the type of a data member cannot refer to a later declaration.
Members#
[class.mem] specifies what can go in a class: data members, member functions, nested types, enumerations, member templates, bit-fields, static members, plus layout rules such as non-static data members being laid out in memory in declaration order. Static members, bit-fields, class-specific operator new/delete and nested classes also live under 11.4; this site folds them into [class.mem].
Special member functions#
The default constructor, copy constructor, move constructor, copy assignment, move assignment and destructor can be implicitly declared by the compiler when you don't declare them, implicitly defined when they are used, and defined as deleted (= delete) when necessary. The way they suppress one another is the language-level basis of the Rule of 0/3/5. [special] gives the overall table; [class.ctor], [class.copy.ctor], [class.copy.assign] and [class.dtor] cover the details.
A special member function that does nothing extra (byte-wise copy, a destructor that does nothing) is trivial. Triviality determines the three properties below.
Three properties of classes#
[class.prop] defines the three classifications used most often when dealing with C and hardware:
| Property | What it gives you |
|---|---|
| Trivially copyable | Can be copied with memcpy or std::bit_cast, can be put in std::atomic |
| Standard-layout | The first member has the same address as the object, offsetof is guaranteed, data can be exchanged with C |
| Implicit-lifetime | Such objects can be used directly in storage obtained from malloc or memcpy |
User-defined conversions#
A non-explicit constructor provides an implicit conversion from its parameter type to the class type, and a conversion function operator T() provides an implicit conversion from the class type to T ([class.conv]). An implicit conversion uses at most one user-defined conversion ([class.conv.general]/4), so a two-step conversion like A → B → C never happens automatically, and overload resolution doesn't expand indefinitely because of it.
The remaining subclauses#
| Subclause | Contents | On this site |
|---|---|---|
| 11.5 | Unions and anonymous unions | [class.union] |
| 11.6 | Local classes | [class.local] |
| 11.7 | Derived classes, multiple inheritance, virtual functions, abstract classes | [class.derived] |
| 11.8 | Member access control, friends | [class.access] |
| 11.9 | Member initializer lists, inheriting constructors, rules during construction and destruction, copy elision | [class.init] |
| 11.10 | = default for operator== and operator<=> |
[class.compare] |
Changes to this clause in C++26#
- Trivial unions C++26: a union's default constructor and destructor are no longer defined as deleted just because a variant member has a non-trivial default constructor or destructor (see [class.default.ctor] and [class.dtor]).
- The notion of a trivial class, together with
std::is_trivial, moved to Annex D and is deprecated C++26 ([depr.meta.types]); [class.prop] keeps only the three properties above. - Data member descriptions were introduced for static reflection's
std::meta::define_aggregate([class.mem.general]/32) C++26. - Member declarations can contain
constevalblocks and contract specifiers C++26.
Contents
- 11.1Preamble[class.pre]draft
- 11.2Properties of classes[class.prop]draft
- 11.3Class names[class.name]
- 11.4Class members[class.mem]draft
- 11.4.1General[class.mem.general]
- 11.4.2Member functions[class.mfct]
- 11.4.3Non-static member functions[class.mfct.non.static]
- 11.4.4Special member functions[special]draft
- 11.4.5Constructors[class.ctor]done
- 11.4.6Copy/move assignment operator[class.copy.assign]done
- 11.4.7Destructors[class.dtor]done
- 11.4.8Conversions[class.conv]done
- 11.4.9Static members[class.static]
- 11.4.10Bit-fields[class.bit]
- 11.4.11Allocation and deallocation functions[class.free]
- 11.4.12Nested class declarations[class.nest]
- 11.5Unions[class.union]done
- 11.6Local class declarations[class.local]done
- 11.7Derived classes[class.derived]done
- 11.8Member access control[class.access]done
- 11.8.1General[class.access.general]
- 11.8.2Access specifiers[class.access.spec]
- 11.8.3Accessibility of base classes and base class members[class.access.base]
- 11.8.4Friends[class.friend]done
- 11.8.5Protected member access[class.protected]done
- 11.8.6Access to virtual functions[class.access.virt]
- 11.8.7Multiple access[class.paths]
- 11.8.8Nested classes[class.access.nest]
- 11.9Initialization[class.init]done
- 11.9.1General[class.init.general]
- 11.9.2Explicit initialization[class.expl.init]
- 11.9.3Initializing bases and members[class.base.init]done
- 11.9.4Initialization by inherited constructor[class.inhctor.init]done
- 11.9.5Construction and destruction[class.cdtor]done
- 11.9.6Copy/move elision[class.copy.elision]done
- 11.10Comparisons[class.compare]done