General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
A class is a user-defined type.
A class type is defined by class-specifier, which appears indecl-specifier-seq of thedeclaration syntax. Seeclass declaration for the syntax of the class specifier.
A class can have the following kinds of members:
All members are defined at once in the class definition, they cannot be added to an already-defined class (unlike the members of namespaces)
A member of a classT
cannot useT
as its name if the member is
However, a non-static data member may use the nameT
as long as there are no user-declared constructors.
A class with at least one declared or inheritedvirtual member function ispolymorphic. Objects of this type arepolymorphic objects and have runtime type information stored as part of the object representation, which may be queried withdynamic_cast
andtypeid
. Virtual member functions participate in dynamic binding.
A class with at least one declared or inherited pure virtual member function is anabstract class. Objects of this type cannot be created.
A class with aconstexpr constructor is aLiteralType: objects of this type can be manipulated byconstexpr functions at compile time. | (since C++11) |
Contents |
Trivially copyable classAtrivially copyable class is a class that
Standard-layout classAstandard-layout class is a class that
Astandard-layout struct is a standard-layout class defined with the class keywordstruct or the class keywordclass. Astandard-layout union is a standard-layout class defined with the class keywordunion. | (since C++11) |
Animplicit-lifetime class is a class that
Notes: the implicit-lifetime property is clarified by defect reportP0593R6.
[edit]POD classAPOD class is a class that
APOD struct is a non-union POD class. APOD union is a union that is a POD class. | (deprecated in C++20) |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 148 | C++98 | POD classes could not contain pointers to member, which are themselves POD (scalar) types | restriction removed |
CWG 383 | C++98 | copy assignment operators or destructors could be user-declared in POD classes if they are not defined | not allowed |
CWG 1363 | C++11 | a class that has both trivial default constructors and non-trivial default constructors at the same time could be trivial | it is non-trivial |
CWG 1496 | C++11 | a class that only has constructors that are all defined as deleted could be trivial | it is non-trivial |
CWG 1672 | C++11 | a class could be a standard-layout class if it has multiple empty base classes | it is not a standard-layout class |
CWG 1734 | C++11 | a trivially copyable class could not have non-trivial deleted copy/move constructors/assignment operators | can be trivial if deleted |
CWG 1813 | C++11 | a class was never a standard-layout class if it has a base class that inherits a non-static data member | it can be a standard-layout class |
CWG 1881 | C++11 | for a standard-layout class and its base classes, unnamed bit-fields might be declared in a different class declaring the data members | all non-static data members and bit-fields need to be first declared in the same class |
CWG 1909 | C++98 | a member template could have the same name as its class | prohibited |
CWG 2120 | C++11 | the definition of M(X) in determining a standard- layout class did not consider the case of a class whose first member is an array | addressed this case in the definition of M(X) |
CWG 2605 | C++98 | an implicit-lifetime class could have a user-provided destructor | prohibited |