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 | ||||||||||||||||
Specifies that a class isreplaceable (replaceable_if_eligible),trivially relocatable (trivially_relocatable_if_eligible), or that a class cannot bederived from (final).
Contents |
Class property specifiers appear at the beginning of the class definition, immediately after the name of the class, and cannot appear in a class declaration.
class-keyattr (optional)class-head-nameclass-prop-specifier-seq (optional)base-clause (optional) | |||||||||
class-prop-specifier-seq | - | one or moreclass-prop-specifier s, but each can appear at most once. |
class-prop-specifier | - | one offinal,replaceable_if_eligible andtrivially_relocatable_if_eligible. |
Before(C++26), there was theclass-virt-specifier (optional) in place ofclass-prop-specifier-seq (optional), which only allowed thefinal forfinal
specifier(since C++11).
final,replaceable_if_eligible andtrivially_relocatable_if_eligible are identifiers with a special meaning when used in a class head. In other contexts, it is not reserved and may be used to name objects and functions.
final specifies that this class may not appear in thebase-specifier-list of another class definition (in other words, cannot be derived from). The program is ill-formed otherwise (a compile-time error is generated).final can also be used with aunion definition, in which case it has no effect (other than on the outcome ofstd::is_final)(since C++14), since unions cannot be derived from.
replaceable_if_eligible specifies that this class isreplaceable if it iseligible for replacement .
trivially_relocatable_if_eligible specifies that this class istrivially relocatable if it iseligible for trivial relocation .
A classC
isreplaceable if it iseligible for replacement and either:
union
with no user-declaredspecial member functionsA classC
iseligible for replacement unless either:
C
from anxvalue of typeC
C
from anxvalue of typeC
A class istrivially relocatable if it iseligible for trivial relocation and either:
union
with no user-declaredspecial member functionsA class iseligible for trivial relocation unless it has either:
except that it is implementation-defined whether an otherwise-eligibleunion
having one or more subobjects of polymorphic class type iseligible for trivial relocation .
A classC
isdefault movable if all following conditions are met:
C
from anxvalue of typeC
selects a constructor that is a direct member ofC
and is neither user-provided nor deletedC
from anxvalue of typeC
selects an assignment operator function that is a direct member ofC
and is neither user-provided nor deletedC
has adestructor that is neither user-provided nor deleted.final,replaceable_if_eligible,trivially_relocatable_if_eligible.
union
s with no user-declaredspecial member functions anddefault movable classes are bothreplaceable andtrivially relocatable , even when defined without class property specifiers.Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_trivial_relocatability | 202502L | (C++26) | Trivial relocatability |
struct final;// OK; declares a class named 'final',// does not use class property specifiers.struct IF final;// Ill-formed: class property specifiers// cannot appear at function declaration.struct F final{};// OK; specifier marks class F as non-derivable.struct D: F{};// Ill-formed: class F cannot be derived from. // OK; specifier marks class R as 𝘳𝘦𝘱𝘭𝘢𝘤𝘦𝘢𝘣𝘭𝘦 if eligible.struct R replaceable_if_eligible{}; // OK; specifier marks class T as 𝘵𝘳𝘪𝘷𝘪𝘢𝘭𝘭𝘺 𝘳𝘦𝘭𝘰𝘤𝘢𝘵𝘢𝘣𝘭𝘦 if eligible.struct T trivially_relocatable_if_eligible{}; // OK; a class can be marked with multiple class property specifiers.struct FRT final replaceable_if_eligible trivially_relocatable_if_eligible{}; // Ill-formed: each class property specifier can appear at most once.struct FRF final replaceable_if_eligible final{}; int main(){}
final specifier(C++11) | declares that a method cannot be overridden or a class be derived from[edit] |
(C++14) | checks if a type is a final class type (class template)[edit] |