Files
cppdraft_translate/cppdraft/class/prop.md
2025-10-25 03:02:53 +03:00

10 KiB

[class.prop]

11 Classes [class]

11.2 Properties of classes [class.prop]

1

#

A trivially copyable class is a class:

that has at least one eligible copy constructor, move constructor, copy assignment operator, or move assignment operator ([special], [class.copy.ctor], [class.copy.assign]),

where each eligible copy constructor, move constructor, copy assignment operator, and move assignment operator is trivial, and

that has a trivial, non-deleted destructor ([class.dtor]).

2

#

A class C is default-movable if

overload resolution for direct-initializing an object of type C from an xvalue of type C selects a constructor that is a direct member of C and is neither user-provided nor deleted,

overload resolution for assigning to an lvalue of type C from an xvalue of type C selects an assignment operator function that is a direct member of C and is neither user-provided nor deleted, and

C has a destructor that is neither user-provided nor deleted.

3

#

A class is eligible for trivial relocation unless it

has any virtual base classes,

has a base class that is not a trivially relocatable class,

has a non-static data member of an object type that is not of a trivially relocatable type, or

has a deleted destructor,

except that it is implementation-defined whether an otherwise-eligible union having one or more subobjects of polymorphic class type is eligible for trivial relocation.

4

#

A class C is a trivially relocatable class if it is eligible for trivial relocation and

has the trivially_relocatable_if_eligible class-property-specifier,

is a union with no user-declared special member functions, or

is default-movable.

5

#

[Note 1:

A class with const-qualified or reference non-static data members can be trivially relocatable.

— end note]

6

#

A class C is eligible for replacement unless

it has a base class that is not a replaceable class,

it has a non-static data member that is not of a replaceable type,

overload resolution fails or selects a deleted constructor when direct-initializing an object of type C from an xvalue of typeC ([dcl.init.general]),

overload resolution fails or selects a deleted assignment operator function when assigning to an lvalue of type C from an xvalue of typeC ([expr.assign], [over.assign]), or

it has a deleted destructor.

7

#

A class C is a replaceable class if it is eligible for replacement and

has the replaceable_if_eligible class-property-specifier,

is a union with no user-declared special member functions, or

is default-movable.

8

#

[Note 2:

Accessibility of the special member functions is not considered when establishing trivial relocatability or replaceability.

— end note]

9

#

[Note 3:

Not all trivially copyable classes are trivially relocatable or replaceable.

— end note]

10

#

A class S is a standard-layout class if it:

has no non-static data members of type non-standard-layout class (or array of such types) or reference,

has no virtual functions and novirtual base classes,

has the same access control for all non-static data members,

has no non-standard-layout base classes,

has at most one base class subobject of any given type,

has all non-static data members and bit-fields in the class and its base classes first declared in the same class, and

has no element of the set M(S) of types as a base class, where for any type X, M(X) is defined as follows.88 [Note 4: M(X) is the set of the types of all non-base-class subobjects that can be at a zero offset in X. — end note]

  • (10.7.1)

    If X is a non-union class type with no non-static data members, the set M(X) is empty.

  • (10.7.2)

    If X is a non-union class type with a non-static data member of type X0 that is either of zero size or is the first non-static data member of X (where said member may be an anonymous union), the set M(X) consists of X0 and the elements ofM(X0).

  • (10.7.3)

    If X is a union type, the set M(X) is the union of all M(Ui) and the set containing all Ui, where each Ui is the type of the ith non-static data member of X.

  • (10.7.4)

    If X is an array type with element type Xe, the set M(X) consists of Xe and the elements of M(Xe).

  • (10.7.5)

    If X is a non-class, non-array type, the set M(X) is empty.

11

#

[Example 1: struct B { int i; }; // standard-layout classstruct C : B { }; // standard-layout classstruct D : C { }; // standard-layout classstruct E : D { char : 4; }; // not a standard-layout classstruct Q {};struct S : Q { };struct T : Q { };struct U : S, T { }; // not a standard-layout class — end example]

12

#

A standard-layout struct is a standard-layout class defined with the class-key struct or theclass-key class.

A standard-layout union is a standard-layout class defined with theclass-key union.

13

#

[Note 5:

Standard-layout classes are useful for communicating with code written in other programming languages.

Their layout is specified in [class.mem.general] and [expr.rel].

— end note]

14

#

[Example 2: struct N { // neither trivially copyable nor standard-layoutint i; int j; virtual ~N();};

struct T { // trivially copyable but not standard-layoutint i;private:int j;};

struct SL { // standard-layout but not trivially copyableint i; int j; ~SL();};

struct POD { // both trivially copyable and standard-layoutint i; int j;}; — end example]

15

#

[Note 6:

Aggregates of class type are described in [dcl.init.aggr].

— end note]

16

#

A class S is an implicit-lifetime class if

it is an aggregate whose destructor is not user-provided or

it has at least one trivial eligible constructor and a trivial, non-deleted destructor.

88)88)

This ensures that two subobjects that have the same class type and that belong to the same most derived object are not allocated at the same address ([expr.eq]).