From Jason Turner

[class]

Large diff (220.6 KB) - rendering may be slow on some devices

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpcg3oqk03/{from.md → to.md} +5008 -390
tmp/tmpcg3oqk03/{from.md → to.md} RENAMED
@@ -1,18 +1,20 @@
1
  # Classes <a id="class">[[class]]</a>
2
 
3
- A class is a type. Its name becomes a *class-name* ([[class.name]])
4
- within its scope.
 
 
5
 
6
  ``` bnf
7
  class-name:
8
  identifier
9
  simple-template-id
10
  ```
11
 
12
- *Class-specifier*s and *elaborated-type-specifier*s ([[dcl.type.elab]])
13
- are used to make *class-name*s. An object of a class consists of a
14
  (possibly empty) sequence of members and base class objects.
15
 
16
  ``` bnf
17
  class-specifier:
18
  class-head '{' member-specificationₒₚₜ '}'
@@ -29,42 +31,60 @@ class-head-name:
29
  nested-name-specifierₒₚₜ class-name
30
  ```
31
 
32
  ``` bnf
33
  class-virt-specifier:
34
- 'final'
35
  ```
36
 
37
  ``` bnf
38
  class-key:
39
- 'class'
40
- 'struct'
41
- 'union'
42
  ```
43
 
44
- A *class-specifier* whose *class-head* omits the *class-head-name*
45
- defines an unnamed class.
 
 
 
46
 
47
  [*Note 1*: An unnamed class thus can’t be `final`. — *end note*]
48
 
49
  A *class-name* is inserted into the scope in which it is declared
50
  immediately after the *class-name* is seen. The *class-name* is also
51
  inserted into the scope of the class itself; this is known as the
52
  *injected-class-name*. For purposes of access checking, the
53
  injected-class-name is treated as if it were a public member name. A
54
- *class-specifier* is commonly referred to as a class definition. A class
55
- is considered defined after the closing brace of its *class-specifier*
56
- has been seen even though its member functions are in general not yet
57
- defined. The optional *attribute-specifier-seq* appertains to the class;
58
- the attributes in the *attribute-specifier-seq* are thereafter
59
- considered attributes of the class whenever it is named.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  If a class is marked with the *class-virt-specifier* `final` and it
62
- appears as a *class-or-decltype* in a *base-clause* (Clause 
63
- [[class.derived]]), the program is ill-formed. Whenever a *class-key* is
64
- followed by a *class-head-name*, the *identifier* `final`, and a colon
65
- or left brace, `final` is interpreted as a *class-virt-specifier*.
66
 
67
  [*Example 1*:
68
 
69
  ``` cpp
70
  struct A;
@@ -78,75 +98,70 @@ struct X {
78
  };
79
  ```
80
 
81
  — *end example*]
82
 
83
- Complete objects and member subobjects of class type shall have nonzero
84
- size.[^1]
 
85
 
86
- [*Note 2*: Class objects can be assigned, passed as arguments to
87
- functions, and returned by functions (except objects of classes for
88
- which copying or moving has been restricted; see  [[class.copy]]). Other
89
- plausible operators, such as equality comparison, can be defined by the
90
- user; see  [[over.oper]]. *end note*]
 
 
91
 
92
- A *union* is a class defined with the *class-key* `union`; it holds at
93
- most one data member at a time ([[class.union]]).
94
-
95
- [*Note 3*: Aggregates of class type are described in 
96
- [[dcl.init.aggr]]. — *end note*]
97
 
98
  A *trivially copyable class* is a class:
99
 
100
- - where each copy constructor, move constructor, copy assignment
101
- operator, and move assignment operator ([[class.copy]], [[over.ass]])
102
- is either deleted or trivial,
103
- - that has at least one non-deleted copy constructor, move constructor,
104
- copy assignment operator, or move assignment operator, and
105
- - that has a trivial, non-deleted destructor ([[class.dtor]]).
106
 
107
  A *trivial class* is a class that is trivially copyable and has one or
108
- more default constructors ([[class.ctor]]), all of which are either
109
- trivial or deleted and at least one of which is not deleted.
110
 
111
- [*Note 4*: In particular, a trivially copyable or trivial class does
112
  not have virtual functions or virtual base classes. — *end note*]
113
 
114
  A class `S` is a *standard-layout class* if it:
115
 
116
  - has no non-static data members of type non-standard-layout class (or
117
  array of such types) or reference,
118
- - has no virtual functions ([[class.virtual]]) and no virtual base
119
- classes ([[class.mi]]),
120
- - has the same access control (Clause  [[class.access]]) for all
121
- non-static data members,
122
  - has no non-standard-layout base classes,
123
  - has at most one base class subobject of any given type,
124
  - has all non-static data members and bit-fields in the class and its
125
  base classes first declared in the same class, and
126
- - has no element of the set M(S) of types (defined below) as a base
127
- class.[^2]
128
-
129
- M(X) is defined as follows:
130
-
131
- - If `X` is a non-union class type with no (possibly inherited (Clause 
132
- [[class.derived]])) non-static data members, the set M(X) is empty.
133
- - If `X` is a non-union class type whose first non-static data member
134
- has type X (where said member may be an anonymous union), the set
135
  M(X) consists of X₀ and the elements of M(X₀).
136
- - If `X` is a union type, the set M(X) is the union of all M(Uᵢ) and the
137
- set containing all Uᵢ, where each Uᵢ is the type of the ith non-static
138
- data member of `X`.
139
- - If `X` is an array type with element type Xₑ, the set M(X) consists of
140
- Xₑ and the elements of M(Xₑ).
141
  - If `X` is a non-class, non-array type, the set M(X) is empty.
142
 
143
- [*Note 5*: M(X) is the set of the types of all non-base-class
144
- subobjects that are guaranteed in a standard-layout class to be at a
145
- zero offset in `X`. — *end note*]
146
-
147
- [*Example 2*:
148
 
149
  ``` cpp
150
  struct B { int i; }; // standard-layout class
151
  struct C : B { }; // standard-layout class
152
  struct D : C { }; // standard-layout class
@@ -159,26 +174,19 @@ zero offset in `X`. — *end note*]
159
  ```
160
 
161
  — *end example*]
162
 
163
  A *standard-layout struct* is a standard-layout class defined with the
164
- *class-key* `struct` or the *class-key* `class`. A *standard-layout
165
- union* is a standard-layout class defined with the *class-key* `union`.
 
166
 
167
- [*Note 6*: Standard-layout classes are useful for communicating with
168
  code written in other programming languages. Their layout is specified
169
  in  [[class.mem]]. — *end note*]
170
 
171
- A *POD struct*[^3] is a non-union class that is both a trivial class and
172
- a standard-layout class, and has no non-static data members of type
173
- non-POD struct, non-POD union (or array of such types). Similarly, a
174
- *POD union* is a union that is both a trivial class and a
175
- standard-layout class, and has no non-static data members of type
176
- non-POD struct, non-POD union (or array of such types). A *POD class* is
177
- a class that is either a POD struct or a POD union.
178
-
179
- [*Example 3*:
180
 
181
  ``` cpp
182
  struct N { // neither trivial nor standard-layout
183
  int i;
184
  int j;
@@ -203,19 +211,16 @@ struct POD { // both trivial and standard-layout
203
  };
204
  ```
205
 
206
  — *end example*]
207
 
208
- If a *class-head-name* contains a *nested-name-specifier*, the
209
- *class-specifier* shall refer to a class that was previously declared
210
- directly in the class or namespace to which the *nested-name-specifier*
211
- refers, or in an element of the inline namespace set (
212
- [[namespace.def]]) of that namespace (i.e., not merely inherited or
213
- introduced by a *using-declaration*), and the *class-specifier* shall
214
- appear in a namespace enclosing the previous declaration. In such cases,
215
- the *nested-name-specifier* of the *class-head-name* of the definition
216
- shall not begin with a *decltype-specifier*.
217
 
218
  ## Class names <a id="class.name">[[class.name]]</a>
219
 
220
  A class definition introduces a new type.
221
 
@@ -241,29 +246,29 @@ are type mismatches, and that
241
  ``` cpp
242
  int f(X);
243
  int f(Y);
244
  ```
245
 
246
- declare an overloaded (Clause  [[over]]) function `f()` and not simply a
247
- single function `f()` twice. For the same reason,
248
 
249
  ``` cpp
250
  struct S { int a; };
251
- struct S { int a; }; // error, double definition
252
  ```
253
 
254
  is ill-formed because it defines `S` twice.
255
 
256
  — *end example*]
257
 
258
  A class declaration introduces the class name into the scope where it is
259
  declared and hides any class, variable, function, or other declaration
260
- of that name in an enclosing scope ([[basic.scope]]). If a class name
261
- is declared in a scope where a variable, function, or enumerator of the
262
  same name is also declared, then when both declarations are in scope,
263
- the class can be referred to only using an *elaborated-type-specifier* (
264
- [[basic.lookup.elab]]).
265
 
266
  [*Example 2*:
267
 
268
  ``` cpp
269
  struct stat {
@@ -320,21 +325,21 @@ class Vector {
320
  // ...
321
  friend Vector operator*(const Matrix&, const Vector&);
322
  };
323
  ```
324
 
325
- Declaration of `friend`s is described in  [[class.friend]], operator
326
  functions in  [[over.oper]].
327
 
328
  — *end example*]
329
 
330
  — *end note*]
331
 
332
- [*Note 2*: An *elaborated-type-specifier* ([[dcl.type.elab]]) can also
333
- be used as a *type-specifier* as part of a declaration. It differs from
334
- a class declaration in that if a class of the elaborated name is in
335
- scope the elaborated name will refer to it. — *end note*]
336
 
337
  [*Example 5*:
338
 
339
  ``` cpp
340
  struct s { int a; };
@@ -362,15 +367,12 @@ the name of a pointer to an object of that class. This means that the
362
  elaborated form `class` `A` must be used to refer to the class. Such
363
  artistry with names can be confusing and is best avoided.
364
 
365
  — *end note*]
366
 
367
- A *typedef-name* ([[dcl.typedef]]) that names a class type, or a
368
- cv-qualified version thereof, is also a *class-name*. If a
369
- *typedef-name* that names a cv-qualified class type is used where a
370
- *class-name* is required, the cv-qualifiers are ignored. A
371
- *typedef-name* shall not be used as the *identifier* in a *class-head*.
372
 
373
  ## Class members <a id="class.mem">[[class.mem]]</a>
374
 
375
  ``` bnf
376
  member-specification:
@@ -381,14 +383,17 @@ member-specification:
381
  ``` bnf
382
  member-declaration:
383
  attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
384
  function-definition
385
  using-declaration
 
386
  static_assert-declaration
387
  template-declaration
 
388
  deduction-guide
389
  alias-declaration
 
390
  empty-declaration
391
  ```
392
 
393
  ``` bnf
394
  member-declarator-list:
@@ -397,96 +402,108 @@ member-declarator-list:
397
  ```
398
 
399
  ``` bnf
400
  member-declarator:
401
  declarator virt-specifier-seqₒₚₜ pure-specifierₒₚₜ
 
402
  declarator brace-or-equal-initializerₒₚₜ
403
- identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression
404
  ```
405
 
406
  ``` bnf
407
  virt-specifier-seq:
408
  virt-specifier
409
  virt-specifier-seq virt-specifier
410
  ```
411
 
412
  ``` bnf
413
  virt-specifier:
414
- 'override'
415
- 'final'
416
  ```
417
 
418
  ``` bnf
419
  pure-specifier:
420
- '= 0'
421
  ```
422
 
423
  The *member-specification* in a class definition declares the full set
424
  of members of the class; no member can be added elsewhere. A *direct
425
  member* of a class `X` is a member of `X` that was first declared within
426
- the *member-specification* of `X`, including anonymous union objects (
427
- [[class.union.anon]]) and direct members thereof. Members of a class are
428
- data members, member functions ([[class.mfct]]), nested types,
429
- enumerators, and member templates ([[temp.mem]]) and specializations
430
  thereof.
431
 
432
  [*Note 1*: A specialization of a static data member template is a
433
  static data member. A specialization of a member function template is a
434
  member function. A specialization of a member class template is a nested
435
  class. — *end note*]
436
 
437
  A *member-declaration* does not declare new members of the class if it
438
  is
439
 
440
- - a friend declaration ([[class.friend]]),
441
  - a *static_assert-declaration*,
442
- - a *using-declaration* ([[namespace.udecl]]), or
443
  - an *empty-declaration*.
444
 
445
  For any other *member-declaration*, each declared entity that is not an
446
- unnamed bit-field ([[class.bit]]) is a member of the class, and each
447
- such *member-declaration* shall either declare at least one member name
448
- of the class or declare at least one unnamed bit-field.
449
 
450
  A *data member* is a non-function member introduced by a
451
  *member-declarator*. A *member function* is a member that is a function.
452
- Nested types are classes ([[class.name]],  [[class.nest]]) and
453
- enumerations ([[dcl.enum]]) declared in the class and arbitrary types
454
- declared as members by use of a typedef declaration ([[dcl.typedef]])
455
- or *alias-declaration*. The enumerators of an unscoped enumeration (
456
- [[dcl.enum]]) defined in the class are members of the class.
457
 
458
  A data member or member function may be declared `static` in its
459
  *member-declaration*, in which case it is a *static member* (see 
460
- [[class.static]]) (a *static data member* ([[class.static.data]]) or
461
- *static member function* ([[class.static.mfct]]), respectively) of the
462
  class. Any other data member or member function is a *non-static member*
463
  (a *non-static data member* or *non-static member function* (
464
  [[class.mfct.non-static]]), respectively).
465
 
466
  [*Note 2*: A non-static data member of non-reference type is a member
467
- subobject of a class object ([[intro.object]]). — *end note*]
468
 
469
  A member shall not be declared twice in the *member-specification*,
470
  except that
471
 
472
  - a nested class or member class template can be declared and then later
473
  defined, and
474
  - an enumeration can be introduced with an *opaque-enum-declaration* and
475
  later redeclared with an *enum-specifier*.
476
 
477
  [*Note 3*: A single name can denote several member functions provided
478
- their types are sufficiently different (Clause 
479
- [[over]]). — *end note*]
480
-
481
- A class is considered a completely-defined object type (
482
- [[basic.types]]) (or complete type) at the closing `}` of the
483
- *class-specifier*. Within the class *member-specification*, the class is
484
- regarded as complete within function bodies, default arguments,
485
- *noexcept-specifier*s, and default member initializers (including such
486
- things in nested classes). Otherwise it is regarded as incomplete within
487
- its own class *member-specification*.
 
 
 
 
 
 
 
 
 
 
 
488
 
489
  In a *member-declarator*, an `=` immediately following the *declarator*
490
  is interpreted as introducing a *pure-specifier* if the *declarator-id*
491
  has function type, otherwise it is interpreted as introducing a
492
  *brace-or-equal-initializer*.
@@ -501,10 +518,31 @@ struct S {
501
  };
502
  ```
503
 
504
  — *end example*]
505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
506
  A *brace-or-equal-initializer* shall appear only in the declaration of a
507
  data member. (For static data members, see  [[class.static.data]]; for
508
  non-static data members, see  [[class.base.init]] and 
509
  [[dcl.init.aggr]]). A *brace-or-equal-initializer* for a non-static data
510
  member specifies a *default member initializer* for the member, and
@@ -519,36 +557,40 @@ also declared `static`.
519
 
520
  The *decl-specifier-seq* may be omitted in constructor, destructor, and
521
  conversion function declarations only; when declaring another kind of
522
  member the *decl-specifier-seq* shall contain a *type-specifier* that is
523
  not a *cv-qualifier*. The *member-declarator-list* can be omitted only
524
- after a *class-specifier* or an *enum-specifier* or in a `friend`
525
- declaration ([[class.friend]]). A *pure-specifier* shall be used only
526
- in the declaration of a virtual function ([[class.virtual]]) that is
527
- not a `friend` declaration.
528
 
529
  The optional *attribute-specifier-seq* in a *member-declaration*
530
  appertains to each of the entities declared by the *member-declarator*s;
531
  it shall not appear if the optional *member-declarator-list* is omitted.
532
 
533
  A *virt-specifier-seq* shall contain at most one of each
534
- *virt-specifier*. A *virt-specifier-seq* shall appear only in the
535
- declaration of a virtual member function ([[class.virtual]]).
536
 
537
- Non-static data members shall not have incomplete types. In particular,
538
- a class `C` shall not contain a non-static member of class `C`, but it
539
- can contain a pointer or reference to an object of class `C`.
540
 
541
- [*Note 4*: See  [[expr.prim]] for restrictions on the use of non-static
542
- data members and non-static member functions. *end note*]
 
543
 
544
- [*Note 5*: The type of a non-static member function is an ordinary
 
 
 
545
  function type, and the type of a non-static data member is an ordinary
546
  object type. There are no special member function types or data member
547
  types. — *end note*]
548
 
549
- [*Example 2*:
550
 
551
  A simple example of a class definition is
552
 
553
  ``` cpp
554
  struct tnode {
@@ -573,44 +615,47 @@ object to which `sp` points; `s.left` refers to the `left` subtree
573
  pointer of the object `s`; and `s.right->tword[0]` refers to the initial
574
  character of the `tword` member of the `right` subtree of `s`.
575
 
576
  — *end example*]
577
 
578
- Non-static data members of a (non-union) class with the same access
579
- control (Clause  [[class.access]]) are allocated so that later members
580
- have higher addresses within a class object. The order of allocation of
581
- non-static data members with different access control is unspecified
582
- (Clause  [[class.access]]). Implementation alignment requirements might
583
- cause two adjacent members not to be allocated immediately after each
584
- other; so might requirements for space for managing virtual functions (
585
- [[class.virtual]]) and virtual base classes ([[class.mi]]).
 
586
 
587
  If `T` is the name of a class, then each of the following shall have a
588
  name different from `T`:
589
 
590
  - every static data member of class `T`;
591
- - every member function of class `T` \[*Note 6*: This restriction does
592
- not apply to constructors, which do not have names (
593
- [[class.ctor]]) — *end note*] ;
594
  - every member of class `T` that is itself a type;
595
  - every member template of class `T`;
596
  - every enumerator of every member of class `T` that is an unscoped
597
  enumerated type; and
598
  - every member of every anonymous union that is a member of class `T`.
599
 
600
- In addition, if class `T` has a user-declared constructor (
601
- [[class.ctor]]), every non-static data member of class `T` shall have a
602
  name different from `T`.
603
 
604
- The *common initial sequence* of two standard-layout struct (Clause 
605
- [[class]]) types is the longest sequence of non-static data members and
606
- bit-fields in declaration order, starting with the first such entity in
607
- each of the structs, such that corresponding entities have
608
- layout-compatible types and either neither entity is a bit-field or both
609
- are bit-fields with the same width.
 
 
610
 
611
- [*Example 3*:
612
 
613
  ``` cpp
614
  struct A { int a; char b; };
615
  struct B { const int b1; volatile char b2; };
616
  struct C { int c; unsigned : 0; char b; };
@@ -623,25 +668,25 @@ either class. The common initial sequence of `A` and `C` and of `A` and
623
  `D` comprises the first member in each case. The common initial sequence
624
  of `A` and `E` is empty.
625
 
626
  — *end example*]
627
 
628
- Two standard-layout struct (Clause  [[class]]) types are
629
- *layout-compatible classes* if their common initial sequence comprises
630
- all members and bit-fields of both classes ([[basic.types]]).
631
 
632
  Two standard-layout unions are layout-compatible if they have the same
633
  number of non-static data members and corresponding non-static data
634
- members (in any order) have layout-compatible types ([[basic.types]]).
635
 
636
- In a standard-layout union with an active member ([[class.union]]) of
637
  struct type `T1`, it is permitted to read a non-static data member `m`
638
  of another union member of struct type `T2` provided `m` is part of the
639
  common initial sequence of `T1` and `T2`; the behavior is as if the
640
  corresponding member of `T1` were nominated.
641
 
642
- [*Example 4*:
643
 
644
  ``` cpp
645
  struct T1 { int a, b; };
646
  struct T2 { int c; double d; };
647
  union U { T1 t1; T2 t2; };
@@ -651,61 +696,61 @@ int f() {
651
  }
652
  ```
653
 
654
  — *end example*]
655
 
656
- [*Note 7*: Reading a volatile object through a non-volatile glvalue has
657
- undefined behavior ([[dcl.type.cv]]). — *end note*]
658
 
659
  If a standard-layout class object has any non-static data members, its
660
- address is the same as the address of its first non-static data member.
661
- Otherwise, its address is the same as the address of its first base
662
- class subobject (if any).
663
 
664
- [*Note 8*: There might therefore be unnamed padding within a
665
- standard-layout struct object, but not at its beginning, as necessary to
666
- achieve appropriate alignment. *end note*]
 
667
 
668
- [*Note 9*: The object and its first subobject are
669
  pointer-interconvertible ([[basic.compound]],
670
  [[expr.static.cast]]). — *end note*]
671
 
672
  ### Member functions <a id="class.mfct">[[class.mfct]]</a>
673
 
674
- A member function may be defined ([[dcl.fct.def]]) in its class
675
- definition, in which case it is an *inline* member function (
676
- [[dcl.inline]]), or it may be defined outside of its class definition if
677
- it has already been declared but not defined in its class definition. A
678
- member function definition that appears outside of the class definition
679
- shall appear in a namespace scope enclosing the class definition. Except
680
- for member function definitions that appear outside of a class
681
- definition, and except for explicit specializations of member functions
682
- of class templates and member function templates ([[temp.spec]])
 
 
 
 
 
683
  appearing outside of the class definition, a member function shall not
684
  be redeclared.
685
 
686
- An inline member function (whether static or non-static) may also be
687
- defined outside of its class definition provided either its declaration
688
- in the class definition or its definition outside of the class
689
- definition declares the function as `inline` or `constexpr`.
690
-
691
- [*Note 1*: Member functions of a class in namespace scope have the
692
- linkage of that class. Member functions of a local class (
693
- [[class.local]]) have no linkage. See  [[basic.link]]. — *end note*]
694
-
695
  [*Note 2*: There can be at most one definition of a non-inline member
696
- function in a program. There may be more than one `inline` member
697
- function definition in a program. See  [[basic.def.odr]] and 
698
  [[dcl.inline]]. — *end note*]
699
 
 
 
 
700
  If the definition of a member function is lexically outside its class
701
  definition, the member function name shall be qualified by its class
702
  name using the `::` operator.
703
 
704
- [*Note 3*: A name used in a member function definition (that is, in the
705
- *parameter-declaration-clause* including the default arguments (
706
- [[dcl.fct.default]]) or in the member function body) is looked up as
707
  described in  [[basic.lookup]]. — *end note*]
708
 
709
  [*Example 1*:
710
 
711
  ``` cpp
@@ -724,21 +769,21 @@ type `T` refers to the typedef member `T` declared in class `X` and the
724
  default argument `count` refers to the static data member `count`
725
  declared in class `X`.
726
 
727
  — *end example*]
728
 
729
- [*Note 4*: A `static` local variable or local type in a member function
730
  always refers to the same entity, whether or not the member function is
731
- `inline`. — *end note*]
732
 
733
- Previously declared member functions may be mentioned in `friend`
734
  declarations.
735
 
736
  Member functions of a local class shall be defined inline in their class
737
  definition, if they are defined at all.
738
 
739
- [*Note 5*:
740
 
741
  A member function can be declared (but not defined) using a typedef for
742
  a function type. The resulting member function has exactly the same type
743
  as it would have if the function declarator were provided explicitly,
744
  see  [[dcl.fct]]. For example,
@@ -761,42 +806,37 @@ Also see  [[temp.arg]].
761
  — *end note*]
762
 
763
  ### Non-static member functions <a id="class.mfct.non-static">[[class.mfct.non-static]]</a>
764
 
765
  A non-static member function may be called for an object of its class
766
- type, or for an object of a class derived (Clause  [[class.derived]])
767
- from its class type, using the class member access syntax (
768
- [[expr.ref]],  [[over.match.call]]). A non-static member function may
769
- also be called directly using the function call syntax ([[expr.call]], 
770
- [[over.match.call]]) from within the body of a member function of its
771
- class or of a class derived from its class.
772
 
773
  If a non-static member function of a class `X` is called for an object
774
  that is not of type `X`, or of a type derived from `X`, the behavior is
775
  undefined.
776
 
777
- When an *id-expression* ([[expr.prim]]) that is not part of a class
778
- member access syntax ([[expr.ref]]) and not used to form a pointer to
779
- member ([[expr.unary.op]]) is used in a member of class `X` in a
780
- context where `this` can be used ([[expr.prim.this]]), if name lookup (
781
- [[basic.lookup]]) resolves the name in the *id-expression* to a
782
  non-static non-type member of some class `C`, and if either the
783
  *id-expression* is potentially evaluated or `C` is `X` or a base class
784
  of `X`, the *id-expression* is transformed into a class member access
785
- expression ([[expr.ref]]) using `(*this)` ([[class.this]]) as the
786
  *postfix-expression* to the left of the `.` operator.
787
 
788
  [*Note 1*: If `C` is not `X` or a base class of `X`, the class member
789
  access expression is ill-formed. — *end note*]
790
 
791
- Similarly during name lookup, when an *unqualified-id* ([[expr.prim]])
792
- used in the definition of a member function for class `X` resolves to a
793
- static member, an enumerator or a nested type of class `X` or of a base
794
- class of `X`, the *unqualified-id* is transformed into a
795
- *qualified-id* ([[expr.prim]]) in which the *nested-name-specifier*
796
- names the class of the member function. These transformations do not
797
- apply in the template definition context ([[temp.dep.type]]).
798
 
799
  [*Example 1*:
800
 
801
  ``` cpp
802
  struct tnode {
@@ -825,55 +865,52 @@ void f(tnode n1, tnode n2) {
825
  In the body of the member function `tnode::set`, the member names
826
  `tword`, `count`, `left`, and `right` refer to members of the object for
827
  which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
828
  `tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
829
  refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
830
- not members of the class `tnode` and should be declared elsewhere.[^4]
831
 
832
  — *end example*]
833
 
834
  A non-static member function may be declared `const`, `volatile`, or
835
  `const` `volatile`. These *cv-qualifier*s affect the type of the `this`
836
- pointer ([[class.this]]). They also affect the function type (
837
- [[dcl.fct]]) of the member function; a member function declared `const`
838
- is a *const* member function, a member function declared `volatile` is a
839
- *volatile* member function and a member function declared `const`
840
- `volatile` is a *const volatile* member function.
841
 
842
  [*Example 2*:
843
 
844
  ``` cpp
845
  struct X {
846
  void g() const;
847
  void h() const volatile;
848
  };
849
  ```
850
 
851
- `X::g` is a `const` member function and `X::h` is a `const` `volatile`
852
- member function.
853
 
854
  — *end example*]
855
 
856
- A non-static member function may be declared with a *ref-qualifier* (
857
- [[dcl.fct]]); see  [[over.match.funcs]].
858
 
859
- A non-static member function may be declared *virtual* (
860
- [[class.virtual]]) or *pure virtual* ([[class.abstract]]).
861
 
862
  #### The `this` pointer <a id="class.this">[[class.this]]</a>
863
 
864
- In the body of a non-static ([[class.mfct]]) member function, the
865
- keyword `this` is a prvalue expression whose value is the address of the
866
- object for which the function is called. The type of `this` in a member
867
- function of a class `X` is `X*`. If the member function is declared
868
- `const`, the type of `this` is `const` `X*`, if the member function is
869
- declared `volatile`, the type of `this` is `volatile` `X*`, and if the
870
- member function is declared `const` `volatile`, the type of `this` is
871
- `const` `volatile` `X*`.
872
 
873
- [*Note 1*: Thus in a `const` member function, the object for which the
874
- function is called is accessed through a `const` access
875
  path. — *end note*]
876
 
877
  [*Example 1*:
878
 
879
  ``` cpp
@@ -887,22 +924,23 @@ struct s {
887
  int s::f() const { return a; }
888
  ```
889
 
890
  The `a++` in the body of `s::h` is ill-formed because it tries to modify
891
  (a part of) the object for which `s::h()` is called. This is not allowed
892
- in a `const` member function because `this` is a pointer to `const`;
893
- that is, `*this` has `const` type.
894
 
895
  — *end example*]
896
 
897
- Similarly, `volatile` semantics ([[dcl.type.cv]]) apply in `volatile`
898
- member functions when accessing the object and its non-static data
899
- members.
900
 
901
- A cv-qualified member function can be called on an object-expression (
902
- [[expr.ref]]) only if the object-expression is as cv-qualified or
903
- less-cv-qualified than the member function.
 
904
 
905
  [*Example 2*:
906
 
907
  ``` cpp
908
  void k(s& x, const s& y) {
@@ -912,29 +950,1253 @@ void k(s& x, const s& y) {
912
  y.g(); // error
913
  }
914
  ```
915
 
916
  The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
917
- non-`const` member function, that is, `s::g()` is less-qualified than
918
- the object-expression `y`.
919
 
920
  — *end example*]
921
 
922
- Constructors ([[class.ctor]]) and destructors ([[class.dtor]]) shall
923
- not be declared `const`, `volatile` or `const` `volatile`.
 
 
924
 
925
- [*Note 2*: However, these functions can be invoked to create and
926
- destroy objects with cv-qualified types, see  [[class.ctor]] and 
927
- [[class.dtor]]. *end note*]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
928
 
929
  ### Static members <a id="class.static">[[class.static]]</a>
930
 
931
  A static member `s` of class `X` may be referred to using the
932
  *qualified-id* expression `X::s`; it is not necessary to use the class
933
- member access syntax ([[expr.ref]]) to refer to a static member. A
934
- static member may be referred to using the class member access syntax,
935
- in which case the object expression is evaluated.
936
 
937
  [*Example 1*:
938
 
939
  ``` cpp
940
  struct process {
@@ -949,15 +2211,15 @@ void f() {
949
  ```
950
 
951
  — *end example*]
952
 
953
  A static member may be referred to directly in the scope of its class or
954
- in the scope of a class derived (Clause  [[class.derived]]) from its
955
- class; in this case, the static member is referred to as if a
956
- *qualified-id* expression was used, with the *nested-name-specifier* of
957
- the *qualified-id* naming the class scope from which the static member
958
- is referenced.
959
 
960
  [*Example 2*:
961
 
962
  ``` cpp
963
  int g();
@@ -970,59 +2232,53 @@ struct Y : X {
970
  int Y::i = g(); // equivalent to Y::g();
971
  ```
972
 
973
  — *end example*]
974
 
975
- If an *unqualified-id* ([[expr.prim]]) is used in the definition of a
976
- static member following the member’s *declarator-id*, and name lookup (
977
- [[basic.lookup.unqual]]) finds that the *unqualified-id* refers to a
978
- static member, enumerator, or nested type of the member’s class (or of a
979
- base class of the member’s class), the *unqualified-id* is transformed
980
- into a *qualified-id* expression in which the *nested-name-specifier*
981
- names the class scope from which the member is referenced.
982
-
983
- [*Note 1*: See  [[expr.prim]] for restrictions on the use of non-static
984
- data members and non-static member functions. — *end note*]
985
-
986
- Static members obey the usual class member access rules (Clause 
987
- [[class.access]]). When used in the declaration of a class member, the
988
  `static` specifier shall only be used in the member declarations that
989
  appear within the *member-specification* of the class definition.
990
 
991
- [*Note 2*: It cannot be specified in member declarations that appear in
992
  namespace scope. — *end note*]
993
 
994
  #### Static member functions <a id="class.static.mfct">[[class.static.mfct]]</a>
995
 
996
  [*Note 1*: The rules described in  [[class.mfct]] apply to static
997
  member functions. — *end note*]
998
 
999
- [*Note 2*: A static member function does not have a `this` pointer (
1000
- [[class.this]]). — *end note*]
1001
 
1002
  A static member function shall not be `virtual`. There shall not be a
1003
  static and a non-static member function with the same name and the same
1004
- parameter types ([[over.load]]). A static member function shall not be
1005
  declared `const`, `volatile`, or `const volatile`.
1006
 
1007
  #### Static data members <a id="class.static.data">[[class.static.data]]</a>
1008
 
1009
  A static data member is not part of the subobjects of a class. If a
1010
  static data member is declared `thread_local` there is one copy of the
1011
  member per thread. If a static data member is not declared
1012
  `thread_local` there is one copy of the data member that is shared by
1013
  all the objects of the class.
1014
 
 
 
 
 
 
1015
  The declaration of a non-inline static data member in its class
1016
  definition is not a definition and may be of an incomplete type other
1017
  than cv `void`. The definition for a static data member that is not
1018
  defined inline in the class definition shall appear in a namespace scope
1019
  enclosing the member’s class definition. In the definition at namespace
1020
  scope, the name of the static data member shall be qualified by its
1021
  class name using the `::` operator. The *initializer* expression in the
1022
- definition of a static data member is in the scope of its class (
1023
- [[basic.scope.class]]).
1024
 
1025
  [*Example 1*:
1026
 
1027
  ``` cpp
1028
  class process {
@@ -1058,92 +2314,89 @@ of class `process` are created by the program.
1058
  — *end note*]
1059
 
1060
  If a non-volatile non-inline `const` static data member is of integral
1061
  or enumeration type, its declaration in the class definition can specify
1062
  a *brace-or-equal-initializer* in which every *initializer-clause* that
1063
- is an *assignment-expression* is a constant expression (
1064
- [[expr.const]]). The member shall still be defined in a namespace scope
1065
- if it is odr-used ([[basic.def.odr]]) in the program and the namespace
1066
- scope definition shall not contain an *initializer*. An inline static
1067
- data member may be defined in the class definition and may specify a
1068
  *brace-or-equal-initializer*. If the member is declared with the
1069
  `constexpr` specifier, it may be redeclared in namespace scope with no
1070
- initializer (this usage is deprecated; see [[depr.static_constexpr]]).
1071
  Declarations of other static data members shall not specify a
1072
  *brace-or-equal-initializer*.
1073
 
1074
- [*Note 2*: There shall be exactly one definition of a static data
1075
- member that is odr-used ([[basic.def.odr]]) in a program; no diagnostic
1076
- is required. — *end note*]
1077
-
1078
- Unnamed classes and classes contained directly or indirectly within
1079
- unnamed classes shall not contain static data members.
1080
 
1081
  [*Note 3*: Static data members of a class in namespace scope have the
1082
- linkage of that class ([[basic.link]]). A local class cannot have
1083
- static data members ([[class.local]]). — *end note*]
1084
 
1085
  Static data members are initialized and destroyed exactly like non-local
1086
  variables ([[basic.start.static]], [[basic.start.dynamic]],
1087
  [[basic.start.term]]).
1088
 
1089
- A static data member shall not be `mutable` ([[dcl.stc]]).
1090
-
1091
  ### Bit-fields <a id="class.bit">[[class.bit]]</a>
1092
 
1093
  A *member-declarator* of the form
1094
 
1095
- specifies a bit-field; its length is set off from the bit-field name by
1096
- a colon. The optional *attribute-specifier-seq* appertains to the entity
1097
- being declared. The bit-field attribute is not part of the type of the
1098
- class member. The *constant-expression* shall be an integral constant
1099
- expression with a value greater than or equal to zero. The value of the
1100
- integral constant expression may be larger than the number of bits in
1101
- the object representation ([[basic.types]]) of the bit-field’s type; in
1102
- such cases the extra bits are used as padding bits and do not
1103
- participate in the value representation ([[basic.types]]) of the
1104
- bit-field. Allocation of bit-fields within a class object is
1105
- *implementation-defined*. Alignment of bit-fields is
1106
- *implementation-defined*. Bit-fields are packed into some addressable
1107
- allocation unit.
 
 
 
1108
 
1109
  [*Note 1*: Bit-fields straddle allocation units on some machines and
1110
  not on others. Bit-fields are assigned right-to-left on some machines,
1111
  left-to-right on others. — *end note*]
1112
 
1113
  A declaration for a bit-field that omits the *identifier* declares an
1114
  *unnamed bit-field*. Unnamed bit-fields are not members and cannot be
1115
- initialized.
 
1116
 
1117
  [*Note 2*: An unnamed bit-field is useful for padding to conform to
1118
  externally-imposed layouts. — *end note*]
1119
 
1120
  As a special case, an unnamed bit-field with a width of zero specifies
1121
  alignment of the next bit-field at an allocation unit boundary. Only
1122
- when declaring an unnamed bit-field may the value of the
1123
- *constant-expression* be equal to zero.
1124
 
1125
- A bit-field shall not be a static member. A bit-field shall have
1126
- integral or enumeration type ([[basic.fundamental]]). A `bool` value
1127
- can successfully be stored in a bit-field of any nonzero size. The
1128
- address-of operator `&` shall not be applied to a bit-field, so there
1129
- are no pointers to bit-fields. A non-const reference shall not be bound
1130
- to a bit-field ([[dcl.init.ref]]).
1131
 
1132
  [*Note 3*: If the initializer for a reference of type `const` `T&` is
1133
  an lvalue that refers to a bit-field, the reference is bound to a
1134
  temporary initialized to hold the value of the bit-field; the reference
1135
  is not bound to the bit-field directly. See 
1136
  [[dcl.init.ref]]. — *end note*]
1137
 
1138
- If the value `true` or `false` is stored into a bit-field of type `bool`
1139
- of any size (including a one bit bit-field), the original `bool` value
1140
- and the value of the bit-field shall compare equal. If the value of an
1141
- enumerator is stored into a bit-field of the same enumeration type and
1142
- the number of bits in the bit-field is large enough to hold all the
1143
- values of that enumeration type ([[dcl.enum]]), the original enumerator
1144
- value and the value of the bit-field shall compare equal.
 
 
 
 
1145
 
1146
  [*Example 1*:
1147
 
1148
  ``` cpp
1149
  enum BOOL { FALSE=0, TRUE=1 };
@@ -1161,16 +2414,16 @@ void f() {
1161
  — *end example*]
1162
 
1163
  ### Nested class declarations <a id="class.nest">[[class.nest]]</a>
1164
 
1165
  A class can be declared within another class. A class declared within
1166
- another is called a *nested* class. The name of a nested class is local
1167
  to its enclosing class. The nested class is in the scope of its
1168
  enclosing class.
1169
 
1170
- [*Note 1*: See  [[expr.prim]] for restrictions on the use of non-static
1171
- data members and non-static member functions. — *end note*]
1172
 
1173
  [*Example 1*:
1174
 
1175
  ``` cpp
1176
  int x;
@@ -1235,15 +2488,15 @@ class E {
1235
  class E::I2 { }; // definition of nested class
1236
  ```
1237
 
1238
  — *end example*]
1239
 
1240
- Like a member function, a friend function ([[class.friend]]) defined
1241
  within a nested class is in the lexical scope of that class; it obeys
1242
  the same rules for name binding as a static member function of that
1243
- class ([[class.static]]), but it has no special access rights to
1244
- members of an enclosing class.
1245
 
1246
  ### Nested type names <a id="class.nested.type">[[class.nested.type]]</a>
1247
 
1248
  Type names obey exactly the same scope rules as other names. In
1249
  particular, type names defined within a class definition cannot be used
@@ -1266,47 +2519,48 @@ X::I e; // OK
1266
 
1267
  — *end example*]
1268
 
1269
  ## Unions <a id="class.union">[[class.union]]</a>
1270
 
 
 
1271
  In a union, a non-static data member is *active* if its name refers to
1272
- an object whose lifetime has begun and has not ended ([[basic.life]]).
1273
- At most one of the non-static data members of an object of union type
1274
- can be active at any time, that is, the value of at most one of the
1275
  non-static data members can be stored in a union at any time.
1276
 
1277
  [*Note 1*: One special guarantee is made in order to simplify the use
1278
  of unions: If a standard-layout union contains several standard-layout
1279
- structs that share a common initial sequence ([[class.mem]]), and if a
1280
  non-static data member of an object of this standard-layout union type
1281
  is active and is one of the standard-layout structs, it is permitted to
1282
  inspect the common initial sequence of any of the standard-layout struct
1283
  members; see  [[class.mem]]. — *end note*]
1284
 
1285
  The size of a union is sufficient to contain the largest of its
1286
  non-static data members. Each non-static data member is allocated as if
1287
- it were the sole member of a struct.
1288
 
1289
  [*Note 2*: A union object and its non-static data members are
1290
  pointer-interconvertible ([[basic.compound]], [[expr.static.cast]]). As
1291
  a consequence, all non-static data members of a union object have the
1292
  same address. — *end note*]
1293
 
1294
  A union can have member functions (including constructors and
1295
- destructors), but it shall not have virtual ([[class.virtual]])
1296
- functions. A union shall not have base classes. A union shall not be
1297
- used as a base class. If a union contains a non-static data member of
1298
- reference type the program is ill-formed.
1299
 
1300
- [*Note 3*: Absent default member initializers ([[class.mem]]), if any
1301
- non-static data member of a union has a non-trivial default
1302
- constructor ([[class.ctor]]), copy constructor ([[class.copy]]), move
1303
- constructor ([[class.copy]]), copy assignment operator (
1304
- [[class.copy]]), move assignment operator ([[class.copy]]), or
1305
- destructor ([[class.dtor]]), the corresponding member function of the
1306
- union must be user-provided or it will be implicitly deleted (
1307
- [[dcl.fct.def.delete]]) for the union. — *end note*]
1308
 
1309
  [*Example 1*:
1310
 
1311
  Consider the following union:
1312
 
@@ -1316,21 +2570,21 @@ union U {
1316
  float f;
1317
  std::string s;
1318
  };
1319
  ```
1320
 
1321
- Since `std::string` ([[string.classes]]) declares non-trivial versions
1322
- of all of the special member functions, `U` will have an implicitly
1323
- deleted default constructor, copy/move constructor, copy/move assignment
1324
  operator, and destructor. To use `U`, some or all of these member
1325
  functions must be user-provided.
1326
 
1327
  — *end example*]
1328
 
1329
  When the left operand of an assignment operator involves a member access
1330
- expression ([[expr.ref]]) that nominates a union member, it may begin
1331
- the lifetime of that union member, as described below. For an expression
1332
  `E`, define the set S(E) of subexpressions of `E` as follows:
1333
 
1334
  - If `E` is of the form `A.B`, S(E) contains the elements of S(A), and
1335
  also contains `A.B` if `B` names a union member of a non-class,
1336
  non-array type, or of a class type with a trivial default constructor
@@ -1339,20 +2593,20 @@ the lifetime of that union member, as described below. For an expression
1339
  subscripting operator, S(E) is S(A) if `A` is of array type, S(B) if
1340
  `B` is of array type, and empty otherwise.
1341
  - Otherwise, S(E) is empty.
1342
 
1343
  In an assignment expression of the form `E1 = E2` that uses either the
1344
- built-in assignment operator ([[expr.ass]]) or a trivial assignment
1345
- operator ([[class.copy]]), for each element `X` of S(`E1`), if
1346
  modification of `X` would have undefined behavior under  [[basic.life]],
1347
  an object of the type of `X` is implicitly created in the nominated
1348
  storage; no initialization is performed and the beginning of its
1349
  lifetime is sequenced after the value computation of the left and right
1350
  operands and before the assignment.
1351
 
1352
  [*Note 4*: This ends the lifetime of the previously-active member of
1353
- the union, if any ([[basic.life]]). — *end note*]
1354
 
1355
  [*Example 2*:
1356
 
1357
  ``` cpp
1358
  union A { int x; int y[4]; };
@@ -1366,11 +2620,11 @@ int f() {
1366
  }
1367
 
1368
  struct X { const int a; int b; };
1369
  union Y { X x; int k; };
1370
  void g() {
1371
- Y y = { { 1, 2 } }; // OK, y.x is active union member ([class.mem])
1372
  int n = y.x.a;
1373
  y.k = 4; // OK: ends lifetime of y.x, y.k is active member of union
1374
  y.x.b = n; // undefined behavior: y.x.b modified outside its lifetime,
1375
  // S(y.x.b) is empty because X's default constructor is deleted,
1376
  // so union member y.x's lifetime does not implicitly start
@@ -1401,24 +2655,25 @@ new (&u.n) N;
1401
 
1402
  ### Anonymous unions <a id="class.union.anon">[[class.union.anon]]</a>
1403
 
1404
  A union of the form
1405
 
 
 
 
 
1406
  is called an *anonymous union*; it defines an unnamed type and an
1407
  unnamed object of that type called an *anonymous union object*. Each
1408
  *member-declaration* in the *member-specification* of an anonymous union
1409
  shall either define a non-static data member or be a
1410
- *static_assert-declaration*.
1411
-
1412
- [*Note 1*: Nested types, anonymous unions, and functions cannot be
1413
- declared within an anonymous union. *end note*]
1414
-
1415
- The names of the members of an anonymous union shall be distinct from
1416
- the names of any other entity in the scope in which the anonymous union
1417
- is declared. For the purpose of name lookup, after the anonymous union
1418
- definition, the members of the anonymous union are considered to have
1419
- been defined in the scope in which the anonymous union is declared.
1420
 
1421
  [*Example 1*:
1422
 
1423
  ``` cpp
1424
  void f() {
@@ -1436,12 +2691,12 @@ since they are union members they have the same address.
1436
  Anonymous unions declared in a named namespace or in the global
1437
  namespace shall be declared `static`. Anonymous unions declared at block
1438
  scope shall be declared with any storage class allowed for a block-scope
1439
  variable, or with no storage class. A storage class is not allowed in a
1440
  declaration of an anonymous union in a class scope. An anonymous union
1441
- shall not have `private` or `protected` members (Clause 
1442
- [[class.access]]). An anonymous union shall not have member functions.
1443
 
1444
  A union for which objects, pointers, or references are declared is not
1445
  an anonymous union.
1446
 
1447
  [*Example 2*:
@@ -1458,11 +2713,11 @@ The assignment to plain `aa` is ill-formed since the member name is not
1458
  visible outside the union, and even if it were visible, it is not
1459
  associated with any particular object.
1460
 
1461
  — *end example*]
1462
 
1463
- [*Note 2*: Initialization of unions with no user-declared constructors
1464
  is described in  [[dcl.init.aggr]]. — *end note*]
1465
 
1466
  A *union-like class* is a union or a class that has an anonymous union
1467
  as a direct member. A union-like class `X` has a set of *variant
1468
  members*. If `X` is a union, a non-static data member of `X` that is not
@@ -1489,49 +2744,3412 @@ union U {
1489
  — *end example*]
1490
 
1491
  ## Local class declarations <a id="class.local">[[class.local]]</a>
1492
 
1493
  A class can be declared within a function definition; such a class is
1494
- called a *local* class. The name of a local class is local to its
1495
  enclosing scope. The local class is in the scope of the enclosing scope,
1496
  and has the same access to names outside the function as does the
1497
- enclosing function. Declarations in a local class shall not odr-use (
1498
- [[basic.def.odr]]) a variable with automatic storage duration from an
1499
- enclosing scope.
 
1500
 
1501
  [*Example 1*:
1502
 
1503
  ``` cpp
1504
  int x;
1505
  void f() {
1506
  static int s;
1507
  int x;
1508
  const int N = 5;
1509
  extern int q();
 
 
1510
 
1511
  struct local {
1512
- int g() { return x; } // error: odr-use of automatic variable x
1513
  int h() { return s; } // OK
1514
  int k() { return ::x; } // OK
1515
  int l() { return q(); } // OK
1516
  int m() { return N; } // OK: not an odr-use
1517
- int* n() { return &N; } // error: odr-use of automatic variable N
 
1518
  };
1519
  }
1520
 
1521
  local* p = 0; // error: local not in scope
1522
  ```
1523
 
1524
  — *end example*]
1525
 
1526
  An enclosing function has no special access to members of the local
1527
- class; it obeys the usual access rules (Clause  [[class.access]]).
1528
- Member functions of a local class shall be defined within their class
1529
  definition, if they are defined at all.
1530
 
1531
  If class `X` is a local class a nested class `Y` may be declared in
1532
  class `X` and later defined in the definition of class `X` or be later
1533
  defined in the same scope as the definition of class `X`. A class nested
1534
  within a local class is a local class.
1535
 
1536
- A local class shall not have static data members.
 
1537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Classes <a id="class">[[class]]</a>
2
 
3
+ ## Preamble <a id="class.pre">[[class.pre]]</a>
4
+
5
+ A class is a type. Its name becomes a *class-name* [[class.name]] within
6
+ its scope.
7
 
8
  ``` bnf
9
  class-name:
10
  identifier
11
  simple-template-id
12
  ```
13
 
14
+ A *class-specifier* or an *elaborated-type-specifier* [[dcl.type.elab]]
15
+ is used to make a *class-name*. An object of a class consists of a
16
  (possibly empty) sequence of members and base class objects.
17
 
18
  ``` bnf
19
  class-specifier:
20
  class-head '{' member-specificationₒₚₜ '}'
 
31
  nested-name-specifierₒₚₜ class-name
32
  ```
33
 
34
  ``` bnf
35
  class-virt-specifier:
36
+ final
37
  ```
38
 
39
  ``` bnf
40
  class-key:
41
+ class
42
+ struct
43
+ union
44
  ```
45
 
46
+ A class declaration where the *class-name* in the *class-head-name* is a
47
+ *simple-template-id* shall be an explicit specialization
48
+ [[temp.expl.spec]] or a partial specialization [[temp.class.spec]]. A
49
+ *class-specifier* whose *class-head* omits the *class-head-name* defines
50
+ an unnamed class.
51
 
52
  [*Note 1*: An unnamed class thus can’t be `final`. — *end note*]
53
 
54
  A *class-name* is inserted into the scope in which it is declared
55
  immediately after the *class-name* is seen. The *class-name* is also
56
  inserted into the scope of the class itself; this is known as the
57
  *injected-class-name*. For purposes of access checking, the
58
  injected-class-name is treated as if it were a public member name. A
59
+ *class-specifier* is commonly referred to as a *class definition*. A
60
+ class is considered defined after the closing brace of its
61
+ *class-specifier* has been seen even though its member functions are in
62
+ general not yet defined. The optional *attribute-specifier-seq*
63
+ appertains to the class; the attributes in the *attribute-specifier-seq*
64
+ are thereafter considered attributes of the class whenever it is named.
65
+
66
+ If a *class-head-name* contains a *nested-name-specifier*, the
67
+ *class-specifier* shall refer to a class that was previously declared
68
+ directly in the class or namespace to which the *nested-name-specifier*
69
+ refers, or in an element of the inline namespace set [[namespace.def]]
70
+ of that namespace (i.e., not merely inherited or introduced by a
71
+ *using-declaration*), and the *class-specifier* shall appear in a
72
+ namespace enclosing the previous declaration. In such cases, the
73
+ *nested-name-specifier* of the *class-head-name* of the definition shall
74
+ not begin with a *decltype-specifier*.
75
+
76
+ [*Note 2*: The *class-key* determines whether the class is a union
77
+ [[class.union]] and whether access is public or private by default
78
+ [[class.access]]. A union holds the value of at most one data member at
79
+ a time. — *end note*]
80
 
81
  If a class is marked with the *class-virt-specifier* `final` and it
82
+ appears as a *class-or-decltype* in a *base-clause* [[class.derived]],
83
+ the program is ill-formed. Whenever a *class-key* is followed by a
84
+ *class-head-name*, the *identifier* `final`, and a colon or left brace,
85
+ `final` is interpreted as a *class-virt-specifier*.
86
 
87
  [*Example 1*:
88
 
89
  ``` cpp
90
  struct A;
 
98
  };
99
  ```
100
 
101
  — *end example*]
102
 
103
+ [*Note 3*: Complete objects of class type have nonzero size. Base class
104
+ subobjects and members declared with the `no_unique_address` attribute
105
+ [[dcl.attr.nouniqueaddr]] are not so constrained. — *end note*]
106
 
107
+ [*Note 4*: Class objects can be assigned ([[over.ass]],
108
+ [[class.copy.assign]]), passed as arguments to functions ([[dcl.init]],
109
+ [[class.copy.ctor]]), and returned by functions (except objects of
110
+ classes for which copying or moving has been restricted; see 
111
+ [[dcl.fct.def.delete]] and [[class.access]]). Other plausible operators,
112
+ such as equality comparison, can be defined by the user; see 
113
+ [[over.oper]]. — *end note*]
114
 
115
+ ## Properties of classes <a id="class.prop">[[class.prop]]</a>
 
 
 
 
116
 
117
  A *trivially copyable class* is a class:
118
 
119
+ - that has at least one eligible copy constructor, move constructor,
120
+ copy assignment operator, or move assignment operator ([[special]],
121
+ [[class.copy.ctor]], [[class.copy.assign]]),
122
+ - where each eligible copy constructor, move constructor, copy
123
+ assignment operator, and move assignment operator is trivial, and
124
+ - that has a trivial, non-deleted destructor [[class.dtor]].
125
 
126
  A *trivial class* is a class that is trivially copyable and has one or
127
+ more eligible default constructors [[class.default.ctor]], all of which
128
+ are trivial.
129
 
130
+ [*Note 1*: In particular, a trivially copyable or trivial class does
131
  not have virtual functions or virtual base classes. — *end note*]
132
 
133
  A class `S` is a *standard-layout class* if it:
134
 
135
  - has no non-static data members of type non-standard-layout class (or
136
  array of such types) or reference,
137
+ - has no virtual functions [[class.virtual]] and no virtual base classes
138
+ [[class.mi]],
139
+ - has the same access control [[class.access]] for all non-static data
140
+ members,
141
  - has no non-standard-layout base classes,
142
  - has at most one base class subobject of any given type,
143
  - has all non-static data members and bit-fields in the class and its
144
  base classes first declared in the same class, and
145
+ - has no element of the set M(S) of types as a base class, where for any
146
+ type `X`, M(X) is defined as follows.[^1]
147
+ \[*Note 2*: M(X) is the set of the types of all non-base-class
148
+ subobjects that may be at a zero offset in `X`. — *end note*]
149
+ - If `X` is a non-union class type with no (possibly inherited
150
+ [[class.derived]]) non-static data members, the set M(X) is empty.
151
+ - If `X` is a non-union class type with a non-static data member of
152
+ type X that is either of zero size or is the first non-static data
153
+ member of `X` (where said member may be an anonymous union), the set
154
  M(X) consists of X₀ and the elements of M(X₀).
155
+ - If `X` is a union type, the set M(X) is the union of all M(Uᵢ) and
156
+ the set containing all Uᵢ, where each Uᵢ is the type of the iᵗʰ
157
+ non-static data member of `X`.
158
+ - If `X` is an array type with element type Xₑ, the set M(X) consists
159
+ of Xₑ and the elements of M(Xₑ).
160
  - If `X` is a non-class, non-array type, the set M(X) is empty.
161
 
162
+ [*Example 1*:
 
 
 
 
163
 
164
  ``` cpp
165
  struct B { int i; }; // standard-layout class
166
  struct C : B { }; // standard-layout class
167
  struct D : C { }; // standard-layout class
 
174
  ```
175
 
176
  — *end example*]
177
 
178
  A *standard-layout struct* is a standard-layout class defined with the
179
+ *class-key* `struct` or the *class-key* `class`. A
180
+ *standard-layout union* is a standard-layout class defined with the
181
+ *class-key* `union`.
182
 
183
+ [*Note 3*: Standard-layout classes are useful for communicating with
184
  code written in other programming languages. Their layout is specified
185
  in  [[class.mem]]. — *end note*]
186
 
187
+ [*Example 2*:
 
 
 
 
 
 
 
 
188
 
189
  ``` cpp
190
  struct N { // neither trivial nor standard-layout
191
  int i;
192
  int j;
 
211
  };
212
  ```
213
 
214
  — *end example*]
215
 
216
+ [*Note 4*: Aggregates of class type are described in 
217
+ [[dcl.init.aggr]]. *end note*]
218
+
219
+ A class `S` is an *implicit-lifetime class* if it is an aggregate or has
220
+ at least one trivial eligible constructor and a trivial, non-deleted
221
+ destructor.
 
 
 
222
 
223
  ## Class names <a id="class.name">[[class.name]]</a>
224
 
225
  A class definition introduces a new type.
226
 
 
246
  ``` cpp
247
  int f(X);
248
  int f(Y);
249
  ```
250
 
251
+ declare an overloaded [[over]] function `f()` and not simply a single
252
+ function `f()` twice. For the same reason,
253
 
254
  ``` cpp
255
  struct S { int a; };
256
+ struct S { int a; }; // error: double definition
257
  ```
258
 
259
  is ill-formed because it defines `S` twice.
260
 
261
  — *end example*]
262
 
263
  A class declaration introduces the class name into the scope where it is
264
  declared and hides any class, variable, function, or other declaration
265
+ of that name in an enclosing scope [[basic.scope]]. If a class name is
266
+ declared in a scope where a variable, function, or enumerator of the
267
  same name is also declared, then when both declarations are in scope,
268
+ the class can be referred to only using an *elaborated-type-specifier*
269
+ [[basic.lookup.elab]].
270
 
271
  [*Example 2*:
272
 
273
  ``` cpp
274
  struct stat {
 
325
  // ...
326
  friend Vector operator*(const Matrix&, const Vector&);
327
  };
328
  ```
329
 
330
+ Declaration of friends is described in  [[class.friend]], operator
331
  functions in  [[over.oper]].
332
 
333
  — *end example*]
334
 
335
  — *end note*]
336
 
337
+ [*Note 2*: An *elaborated-type-specifier* [[dcl.type.elab]] can also be
338
+ used as a *type-specifier* as part of a declaration. It differs from a
339
+ class declaration in that if a class of the elaborated name is in scope
340
+ the elaborated name will refer to it. — *end note*]
341
 
342
  [*Example 5*:
343
 
344
  ``` cpp
345
  struct s { int a; };
 
367
  elaborated form `class` `A` must be used to refer to the class. Such
368
  artistry with names can be confusing and is best avoided.
369
 
370
  — *end note*]
371
 
372
+ A *simple-template-id* is only a *class-name* if its *template-name*
373
+ names a class template.
 
 
 
374
 
375
  ## Class members <a id="class.mem">[[class.mem]]</a>
376
 
377
  ``` bnf
378
  member-specification:
 
383
  ``` bnf
384
  member-declaration:
385
  attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ member-declarator-listₒₚₜ ';'
386
  function-definition
387
  using-declaration
388
+ using-enum-declaration
389
  static_assert-declaration
390
  template-declaration
391
+ explicit-specialization
392
  deduction-guide
393
  alias-declaration
394
+ opaque-enum-declaration
395
  empty-declaration
396
  ```
397
 
398
  ``` bnf
399
  member-declarator-list:
 
402
  ```
403
 
404
  ``` bnf
405
  member-declarator:
406
  declarator virt-specifier-seqₒₚₜ pure-specifierₒₚₜ
407
+ declarator requires-clause
408
  declarator brace-or-equal-initializerₒₚₜ
409
+ identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression brace-or-equal-initializerₒₚₜ
410
  ```
411
 
412
  ``` bnf
413
  virt-specifier-seq:
414
  virt-specifier
415
  virt-specifier-seq virt-specifier
416
  ```
417
 
418
  ``` bnf
419
  virt-specifier:
420
+ override
421
+ final
422
  ```
423
 
424
  ``` bnf
425
  pure-specifier:
426
+ '=' '0'
427
  ```
428
 
429
  The *member-specification* in a class definition declares the full set
430
  of members of the class; no member can be added elsewhere. A *direct
431
  member* of a class `X` is a member of `X` that was first declared within
432
+ the *member-specification* of `X`, including anonymous union objects
433
+ [[class.union.anon]] and direct members thereof. Members of a class are
434
+ data members, member functions [[class.mfct]], nested types,
435
+ enumerators, and member templates [[temp.mem]] and specializations
436
  thereof.
437
 
438
  [*Note 1*: A specialization of a static data member template is a
439
  static data member. A specialization of a member function template is a
440
  member function. A specialization of a member class template is a nested
441
  class. — *end note*]
442
 
443
  A *member-declaration* does not declare new members of the class if it
444
  is
445
 
446
+ - a friend declaration [[class.friend]],
447
  - a *static_assert-declaration*,
448
+ - a *using-declaration* [[namespace.udecl]], or
449
  - an *empty-declaration*.
450
 
451
  For any other *member-declaration*, each declared entity that is not an
452
+ unnamed bit-field [[class.bit]] is a member of the class, and each such
453
+ *member-declaration* shall either declare at least one member name of
454
+ the class or declare at least one unnamed bit-field.
455
 
456
  A *data member* is a non-function member introduced by a
457
  *member-declarator*. A *member function* is a member that is a function.
458
+ Nested types are classes ([[class.name]], [[class.nest]]) and
459
+ enumerations [[dcl.enum]] declared in the class and arbitrary types
460
+ declared as members by use of a typedef declaration [[dcl.typedef]] or
461
+ *alias-declaration*. The enumerators of an unscoped enumeration
462
+ [[dcl.enum]] defined in the class are members of the class.
463
 
464
  A data member or member function may be declared `static` in its
465
  *member-declaration*, in which case it is a *static member* (see 
466
+ [[class.static]]) (a *static data member* [[class.static.data]] or
467
+ *static member function* [[class.static.mfct]], respectively) of the
468
  class. Any other data member or member function is a *non-static member*
469
  (a *non-static data member* or *non-static member function* (
470
  [[class.mfct.non-static]]), respectively).
471
 
472
  [*Note 2*: A non-static data member of non-reference type is a member
473
+ subobject of a class object [[intro.object]]. — *end note*]
474
 
475
  A member shall not be declared twice in the *member-specification*,
476
  except that
477
 
478
  - a nested class or member class template can be declared and then later
479
  defined, and
480
  - an enumeration can be introduced with an *opaque-enum-declaration* and
481
  later redeclared with an *enum-specifier*.
482
 
483
  [*Note 3*: A single name can denote several member functions provided
484
+ their types are sufficiently different [[over.load]]. — *end note*]
485
+
486
+ A *complete-class context* of a class is a
487
+
488
+ - function body [[dcl.fct.def.general]],
489
+ - default argument [[dcl.fct.default]],
490
+ - *noexcept-specifier* [[except.spec]], or
491
+ - default member initializer
492
+
493
+ within the *member-specification* of the class.
494
+
495
+ [*Note 4*: A complete-class context of a nested class is also a
496
+ complete-class context of any enclosing class, if the nested class is
497
+ defined within the *member-specification* of the enclosing
498
+ class. — *end note*]
499
+
500
+ A class is considered a completely-defined object type [[basic.types]]
501
+ (or complete type) at the closing `}` of the *class-specifier*. The
502
+ class is regarded as complete within its complete-class contexts;
503
+ otherwise it is regarded as incomplete within its own class
504
+ *member-specification*.
505
 
506
  In a *member-declarator*, an `=` immediately following the *declarator*
507
  is interpreted as introducing a *pure-specifier* if the *declarator-id*
508
  has function type, otherwise it is interpreted as introducing a
509
  *brace-or-equal-initializer*.
 
518
  };
519
  ```
520
 
521
  — *end example*]
522
 
523
+ In a *member-declarator* for a bit-field, the *constant-expression* is
524
+ parsed as the longest sequence of tokens that could syntactically form a
525
+ *constant-expression*.
526
+
527
+ [*Example 2*:
528
+
529
+ ``` cpp
530
+ int a;
531
+ const int b = 0;
532
+ struct S {
533
+ int x1 : 8 = 42; // OK, "= 42" is brace-or-equal-initializer
534
+ int x2 : 8 { 42 }; // OK, "{ 42 \"} is brace-or-equal-initializer
535
+ int y1 : true ? 8 : a = 42; // OK, brace-or-equal-initializer is absent
536
+ int y2 : true ? 8 : b = 42; // error: cannot assign to const int
537
+ int y3 : (true ? 8 : b) = 42; // OK, "= 42" is brace-or-equal-initializer
538
+ int z : 1 || new int { 0 }; // OK, brace-or-equal-initializer is absent
539
+ };
540
+ ```
541
+
542
+ — *end example*]
543
+
544
  A *brace-or-equal-initializer* shall appear only in the declaration of a
545
  data member. (For static data members, see  [[class.static.data]]; for
546
  non-static data members, see  [[class.base.init]] and 
547
  [[dcl.init.aggr]]). A *brace-or-equal-initializer* for a non-static data
548
  member specifies a *default member initializer* for the member, and
 
557
 
558
  The *decl-specifier-seq* may be omitted in constructor, destructor, and
559
  conversion function declarations only; when declaring another kind of
560
  member the *decl-specifier-seq* shall contain a *type-specifier* that is
561
  not a *cv-qualifier*. The *member-declarator-list* can be omitted only
562
+ after a *class-specifier* or an *enum-specifier* or in a friend
563
+ declaration [[class.friend]]. A *pure-specifier* shall be used only in
564
+ the declaration of a virtual function [[class.virtual]] that is not a
565
+ friend declaration.
566
 
567
  The optional *attribute-specifier-seq* in a *member-declaration*
568
  appertains to each of the entities declared by the *member-declarator*s;
569
  it shall not appear if the optional *member-declarator-list* is omitted.
570
 
571
  A *virt-specifier-seq* shall contain at most one of each
572
+ *virt-specifier*. A *virt-specifier-seq* shall appear only in the first
573
+ declaration of a virtual member function [[class.virtual]].
574
 
575
+ The type of a non-static data member shall not be an incomplete type
576
+ [[basic.types]], an abstract class type [[class.abstract]], or a
577
+ (possibly multi-dimensional) array thereof.
578
 
579
+ [*Note 5*: In particular, a class `C` cannot contain a non-static
580
+ member of class `C`, but it can contain a pointer or reference to an
581
+ object of class `C`. — *end note*]
582
 
583
+ [*Note 6*: See  [[expr.prim.id]] for restrictions on the use of
584
+ non-static data members and non-static member functions. — *end note*]
585
+
586
+ [*Note 7*: The type of a non-static member function is an ordinary
587
  function type, and the type of a non-static data member is an ordinary
588
  object type. There are no special member function types or data member
589
  types. — *end note*]
590
 
591
+ [*Example 3*:
592
 
593
  A simple example of a class definition is
594
 
595
  ``` cpp
596
  struct tnode {
 
615
  pointer of the object `s`; and `s.right->tword[0]` refers to the initial
616
  character of the `tword` member of the `right` subtree of `s`.
617
 
618
  — *end example*]
619
 
620
+ [*Note 8*: Non-static data members of a (non-union) class with the
621
+ same access control [[class.access]] and non-zero size [[intro.object]]
622
+ are allocated so that later members have higher addresses within a class
623
+ object. The order of allocation of non-static data members with
624
+ different access control is unspecified. Implementation alignment
625
+ requirements might cause two adjacent members not to be allocated
626
+ immediately after each other; so might requirements for space for
627
+ managing virtual functions [[class.virtual]] and virtual base classes
628
+ [[class.mi]]. — *end note*]
629
 
630
  If `T` is the name of a class, then each of the following shall have a
631
  name different from `T`:
632
 
633
  - every static data member of class `T`;
634
+ - every member function of class `T` \[*Note 9*: This restriction does
635
+ not apply to constructors, which do not have names
636
+ [[class.ctor]] — *end note*] ;
637
  - every member of class `T` that is itself a type;
638
  - every member template of class `T`;
639
  - every enumerator of every member of class `T` that is an unscoped
640
  enumerated type; and
641
  - every member of every anonymous union that is a member of class `T`.
642
 
643
+ In addition, if class `T` has a user-declared constructor
644
+ [[class.ctor]], every non-static data member of class `T` shall have a
645
  name different from `T`.
646
 
647
+ The *common initial sequence* of two standard-layout struct
648
+ [[class.prop]] types is the longest sequence of non-static data members
649
+ and bit-fields in declaration order, starting with the first such entity
650
+ in each of the structs, such that corresponding entities have
651
+ layout-compatible types, either both entities are declared with the
652
+ `no_unique_address` attribute [[dcl.attr.nouniqueaddr]] or neither is,
653
+ and either both entities are bit-fields with the same width or neither
654
+ is a bit-field.
655
 
656
+ [*Example 4*:
657
 
658
  ``` cpp
659
  struct A { int a; char b; };
660
  struct B { const int b1; volatile char b2; };
661
  struct C { int c; unsigned : 0; char b; };
 
668
  `D` comprises the first member in each case. The common initial sequence
669
  of `A` and `E` is empty.
670
 
671
  — *end example*]
672
 
673
+ Two standard-layout struct [[class.prop]] types are *layout-compatible
674
+ classes* if their common initial sequence comprises all members and
675
+ bit-fields of both classes [[basic.types]].
676
 
677
  Two standard-layout unions are layout-compatible if they have the same
678
  number of non-static data members and corresponding non-static data
679
+ members (in any order) have layout-compatible types [[basic.types]].
680
 
681
+ In a standard-layout union with an active member [[class.union]] of
682
  struct type `T1`, it is permitted to read a non-static data member `m`
683
  of another union member of struct type `T2` provided `m` is part of the
684
  common initial sequence of `T1` and `T2`; the behavior is as if the
685
  corresponding member of `T1` were nominated.
686
 
687
+ [*Example 5*:
688
 
689
  ``` cpp
690
  struct T1 { int a, b; };
691
  struct T2 { int c; double d; };
692
  union U { T1 t1; T2 t2; };
 
696
  }
697
  ```
698
 
699
  — *end example*]
700
 
701
+ [*Note 10*: Reading a volatile object through a glvalue of non-volatile
702
+ type has undefined behavior [[dcl.type.cv]]. — *end note*]
703
 
704
  If a standard-layout class object has any non-static data members, its
705
+ address is the same as the address of its first non-static data member
706
+ if that member is not a bit-field. Its address is also the same as the
707
+ address of each of its base class subobjects.
708
 
709
+ [*Note 11*: There might therefore be unnamed padding within a
710
+ standard-layout struct object inserted by an implementation, but not at
711
+ its beginning, as necessary to achieve appropriate
712
+ alignment. — *end note*]
713
 
714
+ [*Note 12*: The object and its first subobject are
715
  pointer-interconvertible ([[basic.compound]],
716
  [[expr.static.cast]]). — *end note*]
717
 
718
  ### Member functions <a id="class.mfct">[[class.mfct]]</a>
719
 
720
+ A member function may be defined [[dcl.fct.def]] in its class
721
+ definition, in which case it is an inline [[dcl.inline]] member function
722
+ if it is attached to the global module, or it may be defined outside of
723
+ its class definition if it has already been declared but not defined in
724
+ its class definition.
725
+
726
+ [*Note 1*: A member function is also inline if it is declared `inline`,
727
+ `constexpr`, or `consteval`. *end note*]
728
+
729
+ A member function definition that appears outside of the class
730
+ definition shall appear in a namespace scope enclosing the class
731
+ definition. Except for member function definitions that appear outside
732
+ of a class definition, and except for explicit specializations of member
733
+ functions of class templates and member function templates [[temp.spec]]
734
  appearing outside of the class definition, a member function shall not
735
  be redeclared.
736
 
 
 
 
 
 
 
 
 
 
737
  [*Note 2*: There can be at most one definition of a non-inline member
738
+ function in a program. There may be more than one inline member function
739
+ definition in a program. See  [[basic.def.odr]] and 
740
  [[dcl.inline]]. — *end note*]
741
 
742
+ [*Note 3*: Member functions of a class have the linkage of the name of
743
+ the class. See  [[basic.link]]. — *end note*]
744
+
745
  If the definition of a member function is lexically outside its class
746
  definition, the member function name shall be qualified by its class
747
  name using the `::` operator.
748
 
749
+ [*Note 4*: A name used in a member function definition (that is, in the
750
+ *parameter-declaration-clause* including the default arguments
751
+ [[dcl.fct.default]] or in the member function body) is looked up as
752
  described in  [[basic.lookup]]. — *end note*]
753
 
754
  [*Example 1*:
755
 
756
  ``` cpp
 
769
  default argument `count` refers to the static data member `count`
770
  declared in class `X`.
771
 
772
  — *end example*]
773
 
774
+ [*Note 5*: A `static` local variable or local type in a member function
775
  always refers to the same entity, whether or not the member function is
776
+ inline. — *end note*]
777
 
778
+ Previously declared member functions may be mentioned in friend
779
  declarations.
780
 
781
  Member functions of a local class shall be defined inline in their class
782
  definition, if they are defined at all.
783
 
784
+ [*Note 6*:
785
 
786
  A member function can be declared (but not defined) using a typedef for
787
  a function type. The resulting member function has exactly the same type
788
  as it would have if the function declarator were provided explicitly,
789
  see  [[dcl.fct]]. For example,
 
806
  — *end note*]
807
 
808
  ### Non-static member functions <a id="class.mfct.non-static">[[class.mfct.non-static]]</a>
809
 
810
  A non-static member function may be called for an object of its class
811
+ type, or for an object of a class derived [[class.derived]] from its
812
+ class type, using the class member access syntax ([[expr.ref]],
813
+ [[over.match.call]]). A non-static member function may also be called
814
+ directly using the function call syntax ([[expr.call]],
815
+ [[over.match.call]]) from within its class or a class derived from its
816
+ class, or a member thereof, as described below.
817
 
818
  If a non-static member function of a class `X` is called for an object
819
  that is not of type `X`, or of a type derived from `X`, the behavior is
820
  undefined.
821
 
822
+ When an *id-expression* [[expr.prim.id]] that is not part of a class
823
+ member access syntax [[expr.ref]] and not used to form a pointer to
824
+ member [[expr.unary.op]] is used in a member of class `X` in a context
825
+ where `this` can be used [[expr.prim.this]], if name lookup
826
+ [[basic.lookup]] resolves the name in the *id-expression* to a
827
  non-static non-type member of some class `C`, and if either the
828
  *id-expression* is potentially evaluated or `C` is `X` or a base class
829
  of `X`, the *id-expression* is transformed into a class member access
830
+ expression [[expr.ref]] using `(*this)` [[class.this]] as the
831
  *postfix-expression* to the left of the `.` operator.
832
 
833
  [*Note 1*: If `C` is not `X` or a base class of `X`, the class member
834
  access expression is ill-formed. — *end note*]
835
 
836
+ This transformation does not apply in the template definition context
837
+ [[temp.dep.type]].
 
 
 
 
 
838
 
839
  [*Example 1*:
840
 
841
  ``` cpp
842
  struct tnode {
 
865
  In the body of the member function `tnode::set`, the member names
866
  `tword`, `count`, `left`, and `right` refer to members of the object for
867
  which the function is called. Thus, in the call `n1.set("abc",&n2,0)`,
868
  `tword` refers to `n1.tword`, and in the call `n2.set("def",0,0)`, it
869
  refers to `n2.tword`. The functions `strlen`, `perror`, and `strcpy` are
870
+ not members of the class `tnode` and should be declared elsewhere.[^2]
871
 
872
  — *end example*]
873
 
874
  A non-static member function may be declared `const`, `volatile`, or
875
  `const` `volatile`. These *cv-qualifier*s affect the type of the `this`
876
+ pointer [[class.this]]. They also affect the function type [[dcl.fct]]
877
+ of the member function; a member function declared `const` is a *const
878
+ member function*, a member function declared `volatile` is a *volatile
879
+ member function* and a member function declared `const` `volatile` is a
880
+ *const volatile member function*.
881
 
882
  [*Example 2*:
883
 
884
  ``` cpp
885
  struct X {
886
  void g() const;
887
  void h() const volatile;
888
  };
889
  ```
890
 
891
+ `X::g` is a const member function and `X::h` is a const volatile member
892
+ function.
893
 
894
  — *end example*]
895
 
896
+ A non-static member function may be declared with a *ref-qualifier*
897
+ [[dcl.fct]]; see  [[over.match.funcs]].
898
 
899
+ A non-static member function may be declared virtual [[class.virtual]]
900
+ or pure virtual [[class.abstract]].
901
 
902
  #### The `this` pointer <a id="class.this">[[class.this]]</a>
903
 
904
+ In the body of a non-static [[class.mfct]] member function, the keyword
905
+ `this` is a prvalue whose value is a pointer to the object for which the
906
+ function is called. The type of `this` in a member function whose type
907
+ has a *cv-qualifier-seq* cv and whose class is `X` is “pointer to cv
908
+ `X`”.
 
 
 
909
 
910
+ [*Note 1*: Thus in a const member function, the object for which the
911
+ function is called is accessed through a const access
912
  path. — *end note*]
913
 
914
  [*Example 1*:
915
 
916
  ``` cpp
 
924
  int s::f() const { return a; }
925
  ```
926
 
927
  The `a++` in the body of `s::h` is ill-formed because it tries to modify
928
  (a part of) the object for which `s::h()` is called. This is not allowed
929
+ in a const member function because `this` is a pointer to `const`; that
930
+ is, `*this` has `const` type.
931
 
932
  — *end example*]
933
 
934
+ [*Note 2*: Similarly, `volatile` semantics [[dcl.type.cv]] apply in
935
+ volatile member functions when accessing the object and its non-static
936
+ data members. — *end note*]
937
 
938
+ A member function whose type has a *cv-qualifier-seq* *cv1* can be
939
+ called on an object expression [[expr.ref]] of type *cv2* `T` only if
940
+ *cv1* is the same as or more cv-qualified than *cv2*
941
+ [[basic.type.qualifier]].
942
 
943
  [*Example 2*:
944
 
945
  ``` cpp
946
  void k(s& x, const s& y) {
 
950
  y.g(); // error
951
  }
952
  ```
953
 
954
  The call `y.g()` is ill-formed because `y` is `const` and `s::g()` is a
955
+ non-const member function, that is, `s::g()` is less-qualified than the
956
+ object expression `y`.
957
 
958
  — *end example*]
959
 
960
+ [*Note 3*: Constructors and destructors cannot be declared `const`,
961
+ `volatile`, or `const` `volatile`. However, these functions can be
962
+ invoked to create and destroy objects with cv-qualified types; see 
963
+ [[class.ctor]] and  [[class.dtor]]. — *end note*]
964
 
965
+ ### Special member functions <a id="special">[[special]]</a>
966
+
967
+ Default constructors [[class.default.ctor]], copy constructors, move
968
+ constructors [[class.copy.ctor]], copy assignment operators, move
969
+ assignment operators [[class.copy.assign]], and prospective destructors
970
+ [[class.dtor]] are *special member functions*.
971
+
972
+ [*Note 1*: The implementation will implicitly declare these member
973
+ functions for some class types when the program does not explicitly
974
+ declare them. The implementation will implicitly define them if they are
975
+ odr-used [[basic.def.odr]] or needed for constant evaluation
976
+ [[expr.const]]. — *end note*]
977
+
978
+ An implicitly-declared special member function is declared at the
979
+ closing `}` of the *class-specifier*. Programs shall not define
980
+ implicitly-declared special member functions.
981
+
982
+ Programs may explicitly refer to implicitly-declared special member
983
+ functions.
984
+
985
+ [*Example 1*:
986
+
987
+ A program may explicitly call or form a pointer to member to an
988
+ implicitly-declared special member function.
989
+
990
+ ``` cpp
991
+ struct A { }; // implicitly declared A::operator=
992
+ struct B : A {
993
+ B& operator=(const B &);
994
+ };
995
+ B& B::operator=(const B& s) {
996
+ this->A::operator=(s); // well-formed
997
+ return *this;
998
+ }
999
+ ```
1000
+
1001
+ — *end example*]
1002
+
1003
+ [*Note 2*: The special member functions affect the way objects of class
1004
+ type are created, copied, moved, and destroyed, and how values can be
1005
+ converted to values of other types. Often such special member functions
1006
+ are called implicitly. — *end note*]
1007
+
1008
+ Special member functions obey the usual access rules [[class.access]].
1009
+
1010
+ [*Example 2*: Declaring a constructor protected ensures that only
1011
+ derived classes and friends can create objects using
1012
+ it. — *end example*]
1013
+
1014
+ Two special member functions are of the same kind if:
1015
+
1016
+ - they are both default constructors,
1017
+ - they are both copy or move constructors with the same first parameter
1018
+ type, or
1019
+ - they are both copy or move assignment operators with the same first
1020
+ parameter type and the same *cv-qualifier*s and *ref-qualifier*, if
1021
+ any.
1022
+
1023
+ An *eligible special member function* is a special member function for
1024
+ which:
1025
+
1026
+ - the function is not deleted,
1027
+ - the associated constraints [[temp.constr]], if any, are satisfied, and
1028
+ - no special member function of the same kind is more constrained
1029
+ [[temp.constr.order]].
1030
+
1031
+ For a class, its non-static data members, its non-virtual direct base
1032
+ classes, and, if the class is not abstract [[class.abstract]], its
1033
+ virtual base classes are called its *potentially constructed
1034
+ subobjects*.
1035
+
1036
+ A defaulted special member function is *constexpr-compatible* if the
1037
+ corresponding implicitly-declared special member function would be a
1038
+ constexpr function.
1039
+
1040
+ ### Constructors <a id="class.ctor">[[class.ctor]]</a>
1041
+
1042
+ A *constructor* is introduced by a declaration whose *declarator* is a
1043
+ function declarator [[dcl.fct]] of the form
1044
+
1045
+ ``` bnf
1046
+ ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
1047
+ ```
1048
+
1049
+ where the *ptr-declarator* consists solely of an *id-expression*, an
1050
+ optional *attribute-specifier-seq*, and optional surrounding
1051
+ parentheses, and the *id-expression* has one of the following forms:
1052
+
1053
+ - in a *member-declaration* that belongs to the *member-specification*
1054
+ of a class or class template but is not a friend declaration
1055
+ [[class.friend]], the *id-expression* is the injected-class-name
1056
+ [[class.pre]] of the immediately-enclosing entity or
1057
+ - in a declaration at namespace scope or in a friend declaration, the
1058
+ *id-expression* is a *qualified-id* that names a constructor
1059
+ [[class.qual]].
1060
+
1061
+ Constructors do not have names. In a constructor declaration, each
1062
+ *decl-specifier* in the optional *decl-specifier-seq* shall be `friend`,
1063
+ `inline`, `constexpr`, or an *explicit-specifier*.
1064
+
1065
+ [*Example 1*:
1066
+
1067
+ ``` cpp
1068
+ struct S {
1069
+ S(); // declares the constructor
1070
+ };
1071
+
1072
+ S::S() { } // defines the constructor
1073
+ ```
1074
+
1075
+ — *end example*]
1076
+
1077
+ A constructor is used to initialize objects of its class type. Because
1078
+ constructors do not have names, they are never found during name lookup;
1079
+ however an explicit type conversion using the functional notation
1080
+ [[expr.type.conv]] will cause a constructor to be called to initialize
1081
+ an object.
1082
+
1083
+ [*Note 1*: The syntax looks like an explicit call of the
1084
+ constructor. — *end note*]
1085
+
1086
+ [*Example 2*:
1087
+
1088
+ ``` cpp
1089
+ complex zz = complex(1,2.3);
1090
+ cprint( complex(7.8,1.2) );
1091
+ ```
1092
+
1093
+ — *end example*]
1094
+
1095
+ [*Note 2*: For initialization of objects of class type see 
1096
+ [[class.init]]. — *end note*]
1097
+
1098
+ An object created in this way is unnamed.
1099
+
1100
+ [*Note 3*: [[class.temporary]] describes the lifetime of temporary
1101
+ objects. — *end note*]
1102
+
1103
+ [*Note 4*: Explicit constructor calls do not yield lvalues, see 
1104
+ [[basic.lval]]. — *end note*]
1105
+
1106
+ [*Note 5*: Some language constructs have special semantics when used
1107
+ during construction; see  [[class.base.init]] and 
1108
+ [[class.cdtor]]. — *end note*]
1109
+
1110
+ A constructor can be invoked for a `const`, `volatile` or `const`
1111
+ `volatile` object. `const` and `volatile` semantics [[dcl.type.cv]] are
1112
+ not applied on an object under construction. They come into effect when
1113
+ the constructor for the most derived object [[intro.object]] ends.
1114
+
1115
+ A `return` statement in the body of a constructor shall not specify a
1116
+ return value. The address of a constructor shall not be taken.
1117
+
1118
+ A constructor shall not be a coroutine.
1119
+
1120
+ #### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
1121
+
1122
+ A *default constructor* for a class `X` is a constructor of class `X`
1123
+ for which each parameter that is not a function parameter pack has a
1124
+ default argument (including the case of a constructor with no
1125
+ parameters). If there is no user-declared constructor for class `X`, a
1126
+ non-explicit constructor having no parameters is implicitly declared as
1127
+ defaulted [[dcl.fct.def]]. An implicitly-declared default constructor is
1128
+ an inline public member of its class.
1129
+
1130
+ A defaulted default constructor for class `X` is defined as deleted if:
1131
+
1132
+ - `X` is a union that has a variant member with a non-trivial default
1133
+ constructor and no variant member of `X` has a default member
1134
+ initializer,
1135
+ - `X` is a non-union class that has a variant member `M` with a
1136
+ non-trivial default constructor and no variant member of the anonymous
1137
+ union containing `M` has a default member initializer,
1138
+ - any non-static data member with no default member initializer
1139
+ [[class.mem]] is of reference type,
1140
+ - any non-variant non-static data member of const-qualified type (or
1141
+ array thereof) with no *brace-or-equal-initializer* is not
1142
+ const-default-constructible [[dcl.init]],
1143
+ - `X` is a union and all of its variant members are of const-qualified
1144
+ type (or array thereof),
1145
+ - `X` is a non-union class and all members of any anonymous union member
1146
+ are of const-qualified type (or array thereof),
1147
+ - any potentially constructed subobject, except for a non-static data
1148
+ member with a *brace-or-equal-initializer*, has class type `M` (or
1149
+ array thereof) and either `M` has no default constructor or overload
1150
+ resolution [[over.match]] as applied to find `M`’s corresponding
1151
+ constructor results in an ambiguity or in a function that is deleted
1152
+ or inaccessible from the defaulted default constructor, or
1153
+ - any potentially constructed subobject has a type with a destructor
1154
+ that is deleted or inaccessible from the defaulted default
1155
+ constructor.
1156
+
1157
+ A default constructor is *trivial* if it is not user-provided and if:
1158
+
1159
+ - its class has no virtual functions [[class.virtual]] and no virtual
1160
+ base classes [[class.mi]], and
1161
+ - no non-static data member of its class has a default member
1162
+ initializer [[class.mem]], and
1163
+ - all the direct base classes of its class have trivial default
1164
+ constructors, and
1165
+ - for all the non-static data members of its class that are of class
1166
+ type (or array thereof), each such class has a trivial default
1167
+ constructor.
1168
+
1169
+ Otherwise, the default constructor is *non-trivial*.
1170
+
1171
+ A default constructor that is defaulted and not defined as deleted is
1172
+ *implicitly defined* when it is odr-used [[basic.def.odr]] to create an
1173
+ object of its class type [[intro.object]], when it is needed for
1174
+ constant evaluation [[expr.const]], or when it is explicitly defaulted
1175
+ after its first declaration. The implicitly-defined default constructor
1176
+ performs the set of initializations of the class that would be performed
1177
+ by a user-written default constructor for that class with no
1178
+ *ctor-initializer* [[class.base.init]] and an empty
1179
+ *compound-statement*. If that user-written default constructor would be
1180
+ ill-formed, the program is ill-formed. If that user-written default
1181
+ constructor would satisfy the requirements of a constexpr constructor
1182
+ [[dcl.constexpr]], the implicitly-defined default constructor is
1183
+ `constexpr`. Before the defaulted default constructor for a class is
1184
+ implicitly defined, all the non-user-provided default constructors for
1185
+ its base classes and its non-static data members are implicitly defined.
1186
+
1187
+ [*Note 1*: An implicitly-declared default constructor has an exception
1188
+ specification [[except.spec]]. An explicitly-defaulted definition might
1189
+ have an implicit exception specification, see 
1190
+ [[dcl.fct.def]]. — *end note*]
1191
+
1192
+ Default constructors are called implicitly to create class objects of
1193
+ static, thread, or automatic storage duration ([[basic.stc.static]],
1194
+ [[basic.stc.thread]], [[basic.stc.auto]]) defined without an initializer
1195
+ [[dcl.init]], are called to create class objects of dynamic storage
1196
+ duration [[basic.stc.dynamic]] created by a *new-expression* in which
1197
+ the *new-initializer* is omitted [[expr.new]], or are called when the
1198
+ explicit type conversion syntax [[expr.type.conv]] is used. A program is
1199
+ ill-formed if the default constructor for an object is implicitly used
1200
+ and the constructor is not accessible [[class.access]].
1201
+
1202
+ [*Note 2*: [[class.base.init]] describes the order in which
1203
+ constructors for base classes and non-static data members are called and
1204
+ describes how arguments can be specified for the calls to these
1205
+ constructors. — *end note*]
1206
+
1207
+ #### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
1208
+
1209
+ A non-template constructor for class `X` is a copy constructor if its
1210
+ first parameter is of type `X&`, `const X&`, `volatile X&` or
1211
+ `const volatile X&`, and either there are no other parameters or else
1212
+ all other parameters have default arguments [[dcl.fct.default]].
1213
+
1214
+ [*Example 1*:
1215
+
1216
+ `X::X(const X&)`
1217
+
1218
+ and `X::X(X&,int=1)` are copy constructors.
1219
+
1220
+ ``` cpp
1221
+ struct X {
1222
+ X(int);
1223
+ X(const X&, int = 1);
1224
+ };
1225
+ X a(1); // calls X(int);
1226
+ X b(a, 0); // calls X(const X&, int);
1227
+ X c = b; // calls X(const X&, int);
1228
+ ```
1229
+
1230
+ — *end example*]
1231
+
1232
+ A non-template constructor for class `X` is a move constructor if its
1233
+ first parameter is of type `X&&`, `const X&&`, `volatile X&&`, or
1234
+ `const volatile X&&`, and either there are no other parameters or else
1235
+ all other parameters have default arguments [[dcl.fct.default]].
1236
+
1237
+ [*Example 2*:
1238
+
1239
+ `Y::Y(Y&&)` is a move constructor.
1240
+
1241
+ ``` cpp
1242
+ struct Y {
1243
+ Y(const Y&);
1244
+ Y(Y&&);
1245
+ };
1246
+ extern Y f(int);
1247
+ Y d(f(1)); // calls Y(Y&&)
1248
+ Y e = d; // calls Y(const Y&)
1249
+ ```
1250
+
1251
+ — *end example*]
1252
+
1253
+ [*Note 1*:
1254
+
1255
+ All forms of copy/move constructor may be declared for a class.
1256
+
1257
+ [*Example 3*:
1258
+
1259
+ ``` cpp
1260
+ struct X {
1261
+ X(const X&);
1262
+ X(X&); // OK
1263
+ X(X&&);
1264
+ X(const X&&); // OK, but possibly not sensible
1265
+ };
1266
+ ```
1267
+
1268
+ — *end example*]
1269
+
1270
+ — *end note*]
1271
+
1272
+ [*Note 2*:
1273
+
1274
+ If a class `X` only has a copy constructor with a parameter of type
1275
+ `X&`, an initializer of type `const` `X` or `volatile` `X` cannot
1276
+ initialize an object of type cv `X`.
1277
+
1278
+ [*Example 4*:
1279
+
1280
+ ``` cpp
1281
+ struct X {
1282
+ X(); // default constructor
1283
+ X(X&); // copy constructor with a non-const parameter
1284
+ };
1285
+ const X cx;
1286
+ X x = cx; // error: X::X(X&) cannot copy cx into x
1287
+ ```
1288
+
1289
+ — *end example*]
1290
+
1291
+ — *end note*]
1292
+
1293
+ A declaration of a constructor for a class `X` is ill-formed if its
1294
+ first parameter is of type cv `X` and either there are no other
1295
+ parameters or else all other parameters have default arguments. A member
1296
+ function template is never instantiated to produce such a constructor
1297
+ signature.
1298
+
1299
+ [*Example 5*:
1300
+
1301
+ ``` cpp
1302
+ struct S {
1303
+ template<typename T> S(T);
1304
+ S();
1305
+ };
1306
+
1307
+ S g;
1308
+
1309
+ void h() {
1310
+ S a(g); // does not instantiate the member template to produce S::S<S>(S);
1311
+ // uses the implicitly declared copy constructor
1312
+ }
1313
+ ```
1314
+
1315
+ — *end example*]
1316
+
1317
+ If the class definition does not explicitly declare a copy constructor,
1318
+ a non-explicit one is declared *implicitly*. If the class definition
1319
+ declares a move constructor or move assignment operator, the implicitly
1320
+ declared copy constructor is defined as deleted; otherwise, it is
1321
+ defined as defaulted [[dcl.fct.def]]. The latter case is deprecated if
1322
+ the class has a user-declared copy assignment operator or a
1323
+ user-declared destructor [[depr.impldec]].
1324
+
1325
+ The implicitly-declared copy constructor for a class `X` will have the
1326
+ form
1327
+
1328
+ ``` cpp
1329
+ X::X(const X&)
1330
+ ```
1331
+
1332
+ if each potentially constructed subobject of a class type `M` (or array
1333
+ thereof) has a copy constructor whose first parameter is of type `const`
1334
+ `M&` or `const` `volatile` `M&`.[^3] Otherwise, the implicitly-declared
1335
+ copy constructor will have the form
1336
+
1337
+ ``` cpp
1338
+ X::X(X&)
1339
+ ```
1340
+
1341
+ If the definition of a class `X` does not explicitly declare a move
1342
+ constructor, a non-explicit one will be implicitly declared as defaulted
1343
+ if and only if
1344
+
1345
+ - `X` does not have a user-declared copy constructor,
1346
+ - `X` does not have a user-declared copy assignment operator,
1347
+ - `X` does not have a user-declared move assignment operator, and
1348
+ - `X` does not have a user-declared destructor.
1349
+
1350
+ [*Note 3*: When the move constructor is not implicitly declared or
1351
+ explicitly supplied, expressions that otherwise would have invoked the
1352
+ move constructor may instead invoke a copy constructor. — *end note*]
1353
+
1354
+ The implicitly-declared move constructor for class `X` will have the
1355
+ form
1356
+
1357
+ ``` cpp
1358
+ X::X(X&&)
1359
+ ```
1360
+
1361
+ An implicitly-declared copy/move constructor is an inline public member
1362
+ of its class. A defaulted copy/move constructor for a class `X` is
1363
+ defined as deleted [[dcl.fct.def.delete]] if `X` has:
1364
+
1365
+ - a potentially constructed subobject type `M` (or array thereof) that
1366
+ cannot be copied/moved because overload resolution [[over.match]], as
1367
+ applied to find `M`’s corresponding constructor, results in an
1368
+ ambiguity or a function that is deleted or inaccessible from the
1369
+ defaulted constructor,
1370
+ - a variant member whose corresponding constructor as selected by
1371
+ overload resolution is non-trivial,
1372
+ - any potentially constructed subobject of a type with a destructor that
1373
+ is deleted or inaccessible from the defaulted constructor, or,
1374
+ - for the copy constructor, a non-static data member of rvalue reference
1375
+ type.
1376
+
1377
+ [*Note 4*: A defaulted move constructor that is defined as deleted is
1378
+ ignored by overload resolution ([[over.match]], [[over.over]]). Such a
1379
+ constructor would otherwise interfere with initialization from an rvalue
1380
+ which can use the copy constructor instead. — *end note*]
1381
+
1382
+ A copy/move constructor for class `X` is trivial if it is not
1383
+ user-provided and if:
1384
+
1385
+ - class `X` has no virtual functions [[class.virtual]] and no virtual
1386
+ base classes [[class.mi]], and
1387
+ - the constructor selected to copy/move each direct base class subobject
1388
+ is trivial, and
1389
+ - for each non-static data member of `X` that is of class type (or array
1390
+ thereof), the constructor selected to copy/move that member is
1391
+ trivial;
1392
+
1393
+ otherwise the copy/move constructor is *non-trivial*.
1394
+
1395
+ A copy/move constructor that is defaulted and not defined as deleted is
1396
+ *implicitly defined* when it is odr-used [[basic.def.odr]], when it is
1397
+ needed for constant evaluation [[expr.const]], or when it is explicitly
1398
+ defaulted after its first declaration.
1399
+
1400
+ [*Note 5*: The copy/move constructor is implicitly defined even if the
1401
+ implementation elided its odr-use ([[basic.def.odr]],
1402
+ [[class.temporary]]). — *end note*]
1403
+
1404
+ If the implicitly-defined constructor would satisfy the requirements of
1405
+ a constexpr constructor [[dcl.constexpr]], the implicitly-defined
1406
+ constructor is `constexpr`.
1407
+
1408
+ Before the defaulted copy/move constructor for a class is implicitly
1409
+ defined, all non-user-provided copy/move constructors for its
1410
+ potentially constructed subobjects are implicitly defined.
1411
+
1412
+ [*Note 6*: An implicitly-declared copy/move constructor has an implied
1413
+ exception specification [[except.spec]]. — *end note*]
1414
+
1415
+ The implicitly-defined copy/move constructor for a non-union class `X`
1416
+ performs a memberwise copy/move of its bases and members.
1417
+
1418
+ [*Note 7*: Default member initializers of non-static data members are
1419
+ ignored. See also the example in  [[class.base.init]]. — *end note*]
1420
+
1421
+ The order of initialization is the same as the order of initialization
1422
+ of bases and members in a user-defined constructor (see 
1423
+ [[class.base.init]]). Let `x` be either the parameter of the constructor
1424
+ or, for the move constructor, an xvalue referring to the parameter. Each
1425
+ base or non-static data member is copied/moved in the manner appropriate
1426
+ to its type:
1427
+
1428
+ - if the member is an array, each element is direct-initialized with the
1429
+ corresponding subobject of `x`;
1430
+ - if a member `m` has rvalue reference type `T&&`, it is
1431
+ direct-initialized with `static_cast<T&&>(x.m)`;
1432
+ - otherwise, the base or member is direct-initialized with the
1433
+ corresponding base or member of `x`.
1434
+
1435
+ Virtual base class subobjects shall be initialized only once by the
1436
+ implicitly-defined copy/move constructor (see  [[class.base.init]]).
1437
+
1438
+ The implicitly-defined copy/move constructor for a union `X` copies the
1439
+ object representation [[basic.types]] of `X`. For each object nested
1440
+ within [[intro.object]] the object that is the source of the copy, a
1441
+ corresponding object o nested within the destination is identified (if
1442
+ the object is a subobject) or created (otherwise), and the lifetime of o
1443
+ begins before the copy is performed.
1444
+
1445
+ ### Copy/move assignment operator <a id="class.copy.assign">[[class.copy.assign]]</a>
1446
+
1447
+ A user-declared *copy* assignment operator `X::operator=` is a
1448
+ non-static non-template member function of class `X` with exactly one
1449
+ parameter of type `X`, `X&`, `const X&`, `volatile X&`, or
1450
+ `const volatile X&`.[^4]
1451
+
1452
+ [*Note 1*: An overloaded assignment operator must be declared to have
1453
+ only one parameter; see  [[over.ass]]. — *end note*]
1454
+
1455
+ [*Note 2*: More than one form of copy assignment operator may be
1456
+ declared for a class. — *end note*]
1457
+
1458
+ [*Note 3*:
1459
+
1460
+ If a class `X` only has a copy assignment operator with a parameter of
1461
+ type `X&`, an expression of type const `X` cannot be assigned to an
1462
+ object of type `X`.
1463
+
1464
+ [*Example 1*:
1465
+
1466
+ ``` cpp
1467
+ struct X {
1468
+ X();
1469
+ X& operator=(X&);
1470
+ };
1471
+ const X cx;
1472
+ X x;
1473
+ void f() {
1474
+ x = cx; // error: X::operator=(X&) cannot assign cx into x
1475
+ }
1476
+ ```
1477
+
1478
+ — *end example*]
1479
+
1480
+ — *end note*]
1481
+
1482
+ If the class definition does not explicitly declare a copy assignment
1483
+ operator, one is declared *implicitly*. If the class definition declares
1484
+ a move constructor or move assignment operator, the implicitly declared
1485
+ copy assignment operator is defined as deleted; otherwise, it is defined
1486
+ as defaulted [[dcl.fct.def]]. The latter case is deprecated if the class
1487
+ has a user-declared copy constructor or a user-declared destructor
1488
+ [[depr.impldec]]. The implicitly-declared copy assignment operator for a
1489
+ class `X` will have the form
1490
+
1491
+ ``` cpp
1492
+ X& X::operator=(const X&)
1493
+ ```
1494
+
1495
+ if
1496
+
1497
+ - each direct base class `B` of `X` has a copy assignment operator whose
1498
+ parameter is of type `const B&`, `const volatile B&`, or `B`, and
1499
+ - for all the non-static data members of `X` that are of a class type
1500
+ `M` (or array thereof), each such class type has a copy assignment
1501
+ operator whose parameter is of type `const M&`, `const volatile M&`,
1502
+ or `M`.[^5]
1503
+
1504
+ Otherwise, the implicitly-declared copy assignment operator will have
1505
+ the form
1506
+
1507
+ ``` cpp
1508
+ X& X::operator=(X&)
1509
+ ```
1510
+
1511
+ A user-declared move assignment operator `X::operator=` is a non-static
1512
+ non-template member function of class `X` with exactly one parameter of
1513
+ type `X&&`, `const X&&`, `volatile X&&`, or `const volatile X&&`.
1514
+
1515
+ [*Note 4*: An overloaded assignment operator must be declared to have
1516
+ only one parameter; see  [[over.ass]]. — *end note*]
1517
+
1518
+ [*Note 5*: More than one form of move assignment operator may be
1519
+ declared for a class. — *end note*]
1520
+
1521
+ If the definition of a class `X` does not explicitly declare a move
1522
+ assignment operator, one will be implicitly declared as defaulted if and
1523
+ only if
1524
+
1525
+ - `X` does not have a user-declared copy constructor,
1526
+ - `X` does not have a user-declared move constructor,
1527
+ - `X` does not have a user-declared copy assignment operator, and
1528
+ - `X` does not have a user-declared destructor.
1529
+
1530
+ [*Example 2*:
1531
+
1532
+ The class definition
1533
+
1534
+ ``` cpp
1535
+ struct S {
1536
+ int a;
1537
+ S& operator=(const S&) = default;
1538
+ };
1539
+ ```
1540
+
1541
+ will not have a default move assignment operator implicitly declared
1542
+ because the copy assignment operator has been user-declared. The move
1543
+ assignment operator may be explicitly defaulted.
1544
+
1545
+ ``` cpp
1546
+ struct S {
1547
+ int a;
1548
+ S& operator=(const S&) = default;
1549
+ S& operator=(S&&) = default;
1550
+ };
1551
+ ```
1552
+
1553
+ — *end example*]
1554
+
1555
+ The implicitly-declared move assignment operator for a class `X` will
1556
+ have the form
1557
+
1558
+ ``` cpp
1559
+ X& X::operator=(X&&)
1560
+ ```
1561
+
1562
+ The implicitly-declared copy/move assignment operator for class `X` has
1563
+ the return type `X&`; it returns the object for which the assignment
1564
+ operator is invoked, that is, the object assigned to. An
1565
+ implicitly-declared copy/move assignment operator is an inline public
1566
+ member of its class.
1567
+
1568
+ A defaulted copy/move assignment operator for class `X` is defined as
1569
+ deleted if `X` has:
1570
+
1571
+ - a variant member with a non-trivial corresponding assignment operator
1572
+ and `X` is a union-like class, or
1573
+ - a non-static data member of `const` non-class type (or array thereof),
1574
+ or
1575
+ - a non-static data member of reference type, or
1576
+ - a direct non-static data member of class type `M` (or array thereof)
1577
+ or a direct base class `M` that cannot be copied/moved because
1578
+ overload resolution [[over.match]], as applied to find `M`’s
1579
+ corresponding assignment operator, results in an ambiguity or a
1580
+ function that is deleted or inaccessible from the defaulted assignment
1581
+ operator.
1582
+
1583
+ [*Note 6*: A defaulted move assignment operator that is defined as
1584
+ deleted is ignored by overload resolution ([[over.match]],
1585
+ [[over.over]]). — *end note*]
1586
+
1587
+ Because a copy/move assignment operator is implicitly declared for a
1588
+ class if not declared by the user, a base class copy/move assignment
1589
+ operator is always hidden by the corresponding assignment operator of a
1590
+ derived class [[over.ass]]. A *using-declaration* [[namespace.udecl]]
1591
+ that brings in from a base class an assignment operator with a parameter
1592
+ type that could be that of a copy/move assignment operator for the
1593
+ derived class is not considered an explicit declaration of such an
1594
+ operator and does not suppress the implicit declaration of the derived
1595
+ class operator; the operator introduced by the *using-declaration* is
1596
+ hidden by the implicitly-declared operator in the derived class.
1597
+
1598
+ A copy/move assignment operator for class `X` is trivial if it is not
1599
+ user-provided and if:
1600
+
1601
+ - class `X` has no virtual functions [[class.virtual]] and no virtual
1602
+ base classes [[class.mi]], and
1603
+ - the assignment operator selected to copy/move each direct base class
1604
+ subobject is trivial, and
1605
+ - for each non-static data member of `X` that is of class type (or array
1606
+ thereof), the assignment operator selected to copy/move that member is
1607
+ trivial;
1608
+
1609
+ otherwise the copy/move assignment operator is *non-trivial*.
1610
+
1611
+ A copy/move assignment operator for a class `X` that is defaulted and
1612
+ not defined as deleted is *implicitly defined* when it is odr-used
1613
+ [[basic.def.odr]] (e.g., when it is selected by overload resolution to
1614
+ assign to an object of its class type), when it is needed for constant
1615
+ evaluation [[expr.const]], or when it is explicitly defaulted after its
1616
+ first declaration. The implicitly-defined copy/move assignment operator
1617
+ is `constexpr` if
1618
+
1619
+ - `X` is a literal type, and
1620
+ - the assignment operator selected to copy/move each direct base class
1621
+ subobject is a constexpr function, and
1622
+ - for each non-static data member of `X` that is of class type (or array
1623
+ thereof), the assignment operator selected to copy/move that member is
1624
+ a constexpr function.
1625
+
1626
+ Before the defaulted copy/move assignment operator for a class is
1627
+ implicitly defined, all non-user-provided copy/move assignment operators
1628
+ for its direct base classes and its non-static data members are
1629
+ implicitly defined.
1630
+
1631
+ [*Note 7*: An implicitly-declared copy/move assignment operator has an
1632
+ implied exception specification [[except.spec]]. — *end note*]
1633
+
1634
+ The implicitly-defined copy/move assignment operator for a non-union
1635
+ class `X` performs memberwise copy/move assignment of its subobjects.
1636
+ The direct base classes of `X` are assigned first, in the order of their
1637
+ declaration in the *base-specifier-list*, and then the immediate
1638
+ non-static data members of `X` are assigned, in the order in which they
1639
+ were declared in the class definition. Let `x` be either the parameter
1640
+ of the function or, for the move operator, an xvalue referring to the
1641
+ parameter. Each subobject is assigned in the manner appropriate to its
1642
+ type:
1643
+
1644
+ - if the subobject is of class type, as if by a call to `operator=` with
1645
+ the subobject as the object expression and the corresponding subobject
1646
+ of `x` as a single function argument (as if by explicit qualification;
1647
+ that is, ignoring any possible virtual overriding functions in more
1648
+ derived classes);
1649
+ - if the subobject is an array, each element is assigned, in the manner
1650
+ appropriate to the element type;
1651
+ - if the subobject is of scalar type, the built-in assignment operator
1652
+ is used.
1653
+
1654
+ It is unspecified whether subobjects representing virtual base classes
1655
+ are assigned more than once by the implicitly-defined copy/move
1656
+ assignment operator.
1657
+
1658
+ [*Example 3*:
1659
+
1660
+ ``` cpp
1661
+ struct V { };
1662
+ struct A : virtual V { };
1663
+ struct B : virtual V { };
1664
+ struct C : B, A { };
1665
+ ```
1666
+
1667
+ It is unspecified whether the virtual base class subobject `V` is
1668
+ assigned twice by the implicitly-defined copy/move assignment operator
1669
+ for `C`.
1670
+
1671
+ — *end example*]
1672
+
1673
+ The implicitly-defined copy assignment operator for a union `X` copies
1674
+ the object representation [[basic.types]] of `X`. If the source and
1675
+ destination of the assignment are not the same object, then for each
1676
+ object nested within [[intro.object]] the object that is the source of
1677
+ the copy, a corresponding object o nested within the destination is
1678
+ created, and the lifetime of o begins before the copy is performed.
1679
+
1680
+ ### Destructors <a id="class.dtor">[[class.dtor]]</a>
1681
+
1682
+ A *prospective destructor* is introduced by a declaration whose
1683
+ *declarator* is a function declarator [[dcl.fct]] of the form
1684
+
1685
+ ``` bnf
1686
+ ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
1687
+ ```
1688
+
1689
+ where the *ptr-declarator* consists solely of an *id-expression*, an
1690
+ optional *attribute-specifier-seq*, and optional surrounding
1691
+ parentheses, and the *id-expression* has one of the following forms:
1692
+
1693
+ - in a *member-declaration* that belongs to the *member-specification*
1694
+ of a class or class template but is not a friend declaration
1695
+ [[class.friend]], the *id-expression* is `~`*class-name* and the
1696
+ *class-name* is the injected-class-name [[class.pre]] of the
1697
+ immediately-enclosing entity or
1698
+ - in a declaration at namespace scope or in a friend declaration, the
1699
+ *id-expression* is *nested-name-specifier* `~`*class-name* and the
1700
+ *class-name* names the same class as the *nested-name-specifier*.
1701
+
1702
+ A prospective destructor shall take no arguments [[dcl.fct]]. Each
1703
+ *decl-specifier* of the *decl-specifier-seq* of a prospective destructor
1704
+ declaration (if any) shall be `friend`, `inline`, `virtual`,
1705
+ `constexpr`, or `consteval`.
1706
+
1707
+ If a class has no user-declared prospective destructor, a prospective
1708
+ destructor is implicitly declared as defaulted [[dcl.fct.def]]. An
1709
+ implicitly-declared prospective destructor is an inline public member of
1710
+ its class.
1711
+
1712
+ An implicitly-declared prospective destructor for a class `X` will have
1713
+ the form
1714
+
1715
+ ``` cpp
1716
+ ~X()
1717
+ ```
1718
+
1719
+ At the end of the definition of a class, overload resolution is
1720
+ performed among the prospective destructors declared in that class with
1721
+ an empty argument list to select the *destructor* for the class, also
1722
+ known as the *selected destructor*. The program is ill-formed if
1723
+ overload resolution fails. Destructor selection does not constitute a
1724
+ reference to, or odr-use [[basic.def.odr]] of, the selected destructor,
1725
+ and in particular, the selected destructor may be deleted
1726
+ [[dcl.fct.def.delete]].
1727
+
1728
+ The address of a destructor shall not be taken. A destructor can be
1729
+ invoked for a `const`, `volatile` or `const` `volatile` object. `const`
1730
+ and `volatile` semantics [[dcl.type.cv]] are not applied on an object
1731
+ under destruction. They stop being in effect when the destructor for the
1732
+ most derived object [[intro.object]] starts.
1733
+
1734
+ [*Note 1*: A declaration of a destructor that does not have a
1735
+ *noexcept-specifier* has the same exception specification as if it had
1736
+ been implicitly declared [[except.spec]]. — *end note*]
1737
+
1738
+ A defaulted destructor for a class `X` is defined as deleted if:
1739
+
1740
+ - `X` is a union-like class that has a variant member with a non-trivial
1741
+ destructor,
1742
+ - any potentially constructed subobject has class type `M` (or array
1743
+ thereof) and `M` has a deleted destructor or a destructor that is
1744
+ inaccessible from the defaulted destructor,
1745
+ - or, for a virtual destructor, lookup of the non-array deallocation
1746
+ function results in an ambiguity or in a function that is deleted or
1747
+ inaccessible from the defaulted destructor.
1748
+
1749
+ A destructor is trivial if it is not user-provided and if:
1750
+
1751
+ - the destructor is not `virtual`,
1752
+ - all of the direct base classes of its class have trivial destructors,
1753
+ and
1754
+ - for all of the non-static data members of its class that are of class
1755
+ type (or array thereof), each such class has a trivial destructor.
1756
+
1757
+ Otherwise, the destructor is *non-trivial*.
1758
+
1759
+ A defaulted destructor is a constexpr destructor if it satisfies the
1760
+ requirements for a constexpr destructor [[dcl.constexpr]].
1761
+
1762
+ A destructor that is defaulted and not defined as deleted is *implicitly
1763
+ defined* when it is odr-used [[basic.def.odr]] or when it is explicitly
1764
+ defaulted after its first declaration.
1765
+
1766
+ Before a defaulted destructor for a class is implicitly defined, all the
1767
+ non-user-provided destructors for its base classes and its non-static
1768
+ data members are implicitly defined.
1769
+
1770
+ A prospective destructor can be declared `virtual` [[class.virtual]] or
1771
+ pure `virtual` [[class.abstract]]. If the destructor of a class is
1772
+ virtual and any objects of that class or any derived class are created
1773
+ in the program, the destructor shall be defined. If a class has a base
1774
+ class with a virtual destructor, its destructor (whether user- or
1775
+ implicitly-declared) is virtual.
1776
+
1777
+ [*Note 2*: Some language constructs have special semantics when used
1778
+ during destruction; see  [[class.cdtor]]. — *end note*]
1779
+
1780
+ After executing the body of the destructor and destroying any objects
1781
+ with automatic storage duration allocated within the body, a destructor
1782
+ for class `X` calls the destructors for `X`’s direct non-variant
1783
+ non-static data members, the destructors for `X`’s non-virtual direct
1784
+ base classes and, if `X` is the most derived class [[class.base.init]],
1785
+ its destructor calls the destructors for `X`’s virtual base classes. All
1786
+ destructors are called as if they were referenced with a qualified name,
1787
+ that is, ignoring any possible virtual overriding destructors in more
1788
+ derived classes. Bases and members are destroyed in the reverse order of
1789
+ the completion of their constructor (see  [[class.base.init]]). A
1790
+ `return` statement [[stmt.return]] in a destructor might not directly
1791
+ return to the caller; before transferring control to the caller, the
1792
+ destructors for the members and bases are called. Destructors for
1793
+ elements of an array are called in reverse order of their construction
1794
+ (see  [[class.init]]).
1795
+
1796
+ A destructor is invoked implicitly
1797
+
1798
+ - for a constructed object with static storage duration
1799
+ [[basic.stc.static]] at program termination [[basic.start.term]],
1800
+ - for a constructed object with thread storage duration
1801
+ [[basic.stc.thread]] at thread exit,
1802
+ - for a constructed object with automatic storage duration
1803
+ [[basic.stc.auto]] when the block in which an object is created exits
1804
+ [[stmt.dcl]],
1805
+ - for a constructed temporary object when its lifetime ends (
1806
+ [[conv.rval]], [[class.temporary]]).
1807
+
1808
+ In each case, the context of the invocation is the context of the
1809
+ construction of the object. A destructor may also be invoked implicitly
1810
+ through use of a *delete-expression* [[expr.delete]] for a constructed
1811
+ object allocated by a *new-expression* [[expr.new]]; the context of the
1812
+ invocation is the *delete-expression*.
1813
+
1814
+ [*Note 3*: An array of class type contains several subobjects for each
1815
+ of which the destructor is invoked. — *end note*]
1816
+
1817
+ A destructor can also be invoked explicitly. A destructor is
1818
+ *potentially invoked* if it is invoked or as specified in  [[expr.new]],
1819
+ [[stmt.return]], [[dcl.init.aggr]], [[class.base.init]], and 
1820
+ [[except.throw]]. A program is ill-formed if a destructor that is
1821
+ potentially invoked is deleted or not accessible from the context of the
1822
+ invocation.
1823
+
1824
+ At the point of definition of a virtual destructor (including an
1825
+ implicit definition [[class.dtor]]), the non-array deallocation function
1826
+ is determined as if for the expression `delete this` appearing in a
1827
+ non-virtual destructor of the destructor’s class (see  [[expr.delete]]).
1828
+ If the lookup fails or if the deallocation function has a deleted
1829
+ definition [[dcl.fct.def]], the program is ill-formed.
1830
+
1831
+ [*Note 4*: This assures that a deallocation function corresponding to
1832
+ the dynamic type of an object is available for the *delete-expression*
1833
+ [[class.free]]. — *end note*]
1834
+
1835
+ In an explicit destructor call, the destructor is specified by a `~`
1836
+ followed by a *type-name* or *decltype-specifier* that denotes the
1837
+ destructor’s class type. The invocation of a destructor is subject to
1838
+ the usual rules for member functions [[class.mfct]]; that is, if the
1839
+ object is not of the destructor’s class type and not of a class derived
1840
+ from the destructor’s class type (including when the destructor is
1841
+ invoked via a null pointer value), the program has undefined behavior.
1842
+
1843
+ [*Note 5*: Invoking `delete` on a null pointer does not call the
1844
+ destructor; see [[expr.delete]]. — *end note*]
1845
+
1846
+ [*Example 1*:
1847
+
1848
+ ``` cpp
1849
+ struct B {
1850
+ virtual ~B() { }
1851
+ };
1852
+ struct D : B {
1853
+ ~D() { }
1854
+ };
1855
+
1856
+ D D_object;
1857
+ typedef B B_alias;
1858
+ B* B_ptr = &D_object;
1859
+
1860
+ void f() {
1861
+ D_object.B::~B(); // calls B's destructor
1862
+ B_ptr->~B(); // calls D's destructor
1863
+ B_ptr->~B_alias(); // calls D's destructor
1864
+ B_ptr->B_alias::~B(); // calls B's destructor
1865
+ B_ptr->B_alias::~B_alias(); // calls B's destructor
1866
+ }
1867
+ ```
1868
+
1869
+ — *end example*]
1870
+
1871
+ [*Note 6*: An explicit destructor call must always be written using a
1872
+ member access operator [[expr.ref]] or a *qualified-id*
1873
+ [[expr.prim.id.qual]]; in particular, the *unary-expression* `~X()` in a
1874
+ member function is not an explicit destructor call
1875
+ [[expr.unary.op]]. — *end note*]
1876
+
1877
+ [*Note 7*:
1878
+
1879
+ Explicit calls of destructors are rarely needed. One use of such calls
1880
+ is for objects placed at specific addresses using a placement
1881
+ *new-expression*. Such use of explicit placement and destruction of
1882
+ objects can be necessary to cope with dedicated hardware resources and
1883
+ for writing memory management facilities. For example,
1884
+
1885
+ ``` cpp
1886
+ void* operator new(std::size_t, void* p) { return p; }
1887
+ struct X {
1888
+ X(int);
1889
+ ~X();
1890
+ };
1891
+ void f(X* p);
1892
+
1893
+ void g() { // rare, specialized use:
1894
+ char* buf = new char[sizeof(X)];
1895
+ X* p = new(buf) X(222); // use buf[] and initialize
1896
+ f(p);
1897
+ p->X::~X(); // cleanup
1898
+ }
1899
+ ```
1900
+
1901
+ — *end note*]
1902
+
1903
+ Once a destructor is invoked for an object, the object no longer exists;
1904
+ the behavior is undefined if the destructor is invoked for an object
1905
+ whose lifetime has ended [[basic.life]].
1906
+
1907
+ [*Example 2*: If the destructor for an object with automatic storage
1908
+ duration is explicitly invoked, and the block is subsequently left in a
1909
+ manner that would ordinarily invoke implicit destruction of the object,
1910
+ the behavior is undefined. — *end example*]
1911
+
1912
+ [*Note 8*:
1913
+
1914
+ The notation for explicit call of a destructor can be used for any
1915
+ scalar type name [[expr.prim.id.dtor]]. Allowing this makes it possible
1916
+ to write code without having to know if a destructor exists for a given
1917
+ type. For example:
1918
+
1919
+ ``` cpp
1920
+ typedef int I;
1921
+ I* p;
1922
+ p->I::~I();
1923
+ ```
1924
+
1925
+ — *end note*]
1926
+
1927
+ A destructor shall not be a coroutine.
1928
+
1929
+ ### Conversions <a id="class.conv">[[class.conv]]</a>
1930
+
1931
+ Type conversions of class objects can be specified by constructors and
1932
+ by conversion functions. These conversions are called *user-defined
1933
+ conversions* and are used for implicit type conversions [[conv]], for
1934
+ initialization [[dcl.init]], and for explicit type conversions (
1935
+ [[expr.type.conv]], [[expr.cast]], [[expr.static.cast]]).
1936
+
1937
+ User-defined conversions are applied only where they are unambiguous (
1938
+ [[class.member.lookup]], [[class.conv.fct]]). Conversions obey the
1939
+ access control rules [[class.access]]. Access control is applied after
1940
+ ambiguity resolution [[basic.lookup]].
1941
+
1942
+ [*Note 1*: See  [[over.match]] for a discussion of the use of
1943
+ conversions in function calls as well as examples below. — *end note*]
1944
+
1945
+ At most one user-defined conversion (constructor or conversion function)
1946
+ is implicitly applied to a single value.
1947
+
1948
+ [*Example 1*:
1949
+
1950
+ ``` cpp
1951
+ struct X {
1952
+ operator int();
1953
+ };
1954
+
1955
+ struct Y {
1956
+ operator X();
1957
+ };
1958
+
1959
+ Y a;
1960
+ int b = a; // error: no viable conversion (a.operator X().operator int() not considered)
1961
+ int c = X(a); // OK: a.operator X().operator int()
1962
+ ```
1963
+
1964
+ — *end example*]
1965
+
1966
+ User-defined conversions are used implicitly only if they are
1967
+ unambiguous. A conversion function in a derived class does not hide a
1968
+ conversion function in a base class unless the two functions convert to
1969
+ the same type. Function overload resolution [[over.match.best]] selects
1970
+ the best conversion function to perform the conversion.
1971
+
1972
+ [*Example 2*:
1973
+
1974
+ ``` cpp
1975
+ struct X {
1976
+ operator int();
1977
+ };
1978
+
1979
+ struct Y : X {
1980
+ operator char();
1981
+ };
1982
+
1983
+ void f(Y& a) {
1984
+ if (a) { // error: ambiguous between X::operator int() and Y::operator char()
1985
+ }
1986
+ }
1987
+ ```
1988
+
1989
+ — *end example*]
1990
+
1991
+ #### Conversion by constructor <a id="class.conv.ctor">[[class.conv.ctor]]</a>
1992
+
1993
+ A constructor that is not explicit [[dcl.fct.spec]] specifies a
1994
+ conversion from the types of its parameters (if any) to the type of its
1995
+ class. Such a constructor is called a *converting constructor*.
1996
+
1997
+ [*Example 1*:
1998
+
1999
+ ``` cpp
2000
+ struct X {
2001
+ X(int);
2002
+ X(const char*, int =0);
2003
+ X(int, int);
2004
+ };
2005
+
2006
+ void f(X arg) {
2007
+ X a = 1; // a = X(1)
2008
+ X b = "Jessie"; // b = X("Jessie",0)
2009
+ a = 2; // a = X(2)
2010
+ f(3); // f(X(3))
2011
+ f({1, 2}); // f(X(1,2))
2012
+ }
2013
+ ```
2014
+
2015
+ — *end example*]
2016
+
2017
+ [*Note 1*:
2018
+
2019
+ An explicit constructor constructs objects just like non-explicit
2020
+ constructors, but does so only where the direct-initialization syntax
2021
+ [[dcl.init]] or where casts ([[expr.static.cast]], [[expr.cast]]) are
2022
+ explicitly used; see also  [[over.match.copy]]. A default constructor
2023
+ may be an explicit constructor; such a constructor will be used to
2024
+ perform default-initialization or value-initialization [[dcl.init]].
2025
+
2026
+ [*Example 2*:
2027
+
2028
+ ``` cpp
2029
+ struct Z {
2030
+ explicit Z();
2031
+ explicit Z(int);
2032
+ explicit Z(int, int);
2033
+ };
2034
+
2035
+ Z a; // OK: default-initialization performed
2036
+ Z b{}; // OK: direct initialization syntax used
2037
+ Z c = {}; // error: copy-list-initialization
2038
+ Z a1 = 1; // error: no implicit conversion
2039
+ Z a3 = Z(1); // OK: direct initialization syntax used
2040
+ Z a2(1); // OK: direct initialization syntax used
2041
+ Z* p = new Z(1); // OK: direct initialization syntax used
2042
+ Z a4 = (Z)1; // OK: explicit cast used
2043
+ Z a5 = static_cast<Z>(1); // OK: explicit cast used
2044
+ Z a6 = { 3, 4 }; // error: no implicit conversion
2045
+ ```
2046
+
2047
+ — *end example*]
2048
+
2049
+ — *end note*]
2050
+
2051
+ A non-explicit copy/move constructor [[class.copy.ctor]] is a converting
2052
+ constructor.
2053
+
2054
+ [*Note 2*: An implicitly-declared copy/move constructor is not an
2055
+ explicit constructor; it may be called for implicit type
2056
+ conversions. — *end note*]
2057
+
2058
+ #### Conversion functions <a id="class.conv.fct">[[class.conv.fct]]</a>
2059
+
2060
+ A member function of a class `X` having no parameters with a name of the
2061
+ form
2062
+
2063
+ ``` bnf
2064
+ conversion-function-id:
2065
+ operator conversion-type-id
2066
+ ```
2067
+
2068
+ ``` bnf
2069
+ conversion-type-id:
2070
+ type-specifier-seq conversion-declaratorₒₚₜ
2071
+ ```
2072
+
2073
+ ``` bnf
2074
+ conversion-declarator:
2075
+ ptr-operator conversion-declaratorₒₚₜ
2076
+ ```
2077
+
2078
+ specifies a conversion from `X` to the type specified by the
2079
+ *conversion-type-id*. Such functions are called *conversion functions*.
2080
+ A *decl-specifier* in the *decl-specifier-seq* of a conversion function
2081
+ (if any) shall be neither a *defining-type-specifier* nor `static`. The
2082
+ type of the conversion function [[dcl.fct]] is “function taking no
2083
+ parameter returning *conversion-type-id*”. A conversion function is
2084
+ never used to convert a (possibly cv-qualified) object to the (possibly
2085
+ cv-qualified) same object type (or a reference to it), to a (possibly
2086
+ cv-qualified) base class of that type (or a reference to it), or to
2087
+ cv `void`.[^6]
2088
+
2089
+ [*Example 1*:
2090
+
2091
+ ``` cpp
2092
+ struct X {
2093
+ operator int();
2094
+ operator auto() -> short; // error: trailing return type
2095
+ };
2096
+
2097
+ void f(X a) {
2098
+ int i = int(a);
2099
+ i = (int)a;
2100
+ i = a;
2101
+ }
2102
+ ```
2103
+
2104
+ In all three cases the value assigned will be converted by
2105
+ `X::operator int()`.
2106
+
2107
+ — *end example*]
2108
+
2109
+ A conversion function may be explicit [[dcl.fct.spec]], in which case it
2110
+ is only considered as a user-defined conversion for
2111
+ direct-initialization [[dcl.init]]. Otherwise, user-defined conversions
2112
+ are not restricted to use in assignments and initializations.
2113
+
2114
+ [*Example 2*:
2115
+
2116
+ ``` cpp
2117
+ class Y { };
2118
+ struct Z {
2119
+ explicit operator Y() const;
2120
+ };
2121
+
2122
+ void h(Z z) {
2123
+ Y y1(z); // OK: direct-initialization
2124
+ Y y2 = z; // error: no conversion function candidate for copy-initialization
2125
+ Y y3 = (Y)z; // OK: cast notation
2126
+ }
2127
+
2128
+ void g(X a, X b) {
2129
+ int i = (a) ? 1+a : 0;
2130
+ int j = (a&&b) ? a+b : i;
2131
+ if (a) {
2132
+ }
2133
+ }
2134
+ ```
2135
+
2136
+ — *end example*]
2137
+
2138
+ The *conversion-type-id* shall not represent a function type nor an
2139
+ array type. The *conversion-type-id* in a *conversion-function-id* is
2140
+ the longest sequence of tokens that could possibly form a
2141
+ *conversion-type-id*.
2142
+
2143
+ [*Note 1*:
2144
+
2145
+ This prevents ambiguities between the declarator operator `*` and its
2146
+ expression counterparts.
2147
+
2148
+ [*Example 3*:
2149
+
2150
+ ``` cpp
2151
+ &ac.operator int*i; // syntax error:
2152
+ // parsed as: &(ac.operator int *)i
2153
+ // not as: &(ac.operator int)*i
2154
+ ```
2155
+
2156
+ The `*` is the pointer declarator and not the multiplication operator.
2157
+
2158
+ — *end example*]
2159
+
2160
+ This rule also prevents ambiguities for attributes.
2161
+
2162
+ [*Example 4*:
2163
+
2164
+ ``` cpp
2165
+ operator int [[noreturn]] (); // error: noreturn attribute applied to a type
2166
+ ```
2167
+
2168
+ — *end example*]
2169
+
2170
+ — *end note*]
2171
+
2172
+ Conversion functions are inherited.
2173
+
2174
+ Conversion functions can be virtual.
2175
+
2176
+ A conversion function template shall not have a deduced return type
2177
+ [[dcl.spec.auto]].
2178
+
2179
+ [*Example 5*:
2180
+
2181
+ ``` cpp
2182
+ struct S {
2183
+ operator auto() const { return 10; } // OK
2184
+ template<class T>
2185
+ operator auto() const { return 1.2; } // error: conversion function template
2186
+ };
2187
+ ```
2188
+
2189
+ — *end example*]
2190
 
2191
  ### Static members <a id="class.static">[[class.static]]</a>
2192
 
2193
  A static member `s` of class `X` may be referred to using the
2194
  *qualified-id* expression `X::s`; it is not necessary to use the class
2195
+ member access syntax [[expr.ref]] to refer to a static member. A static
2196
+ member may be referred to using the class member access syntax, in which
2197
+ case the object expression is evaluated.
2198
 
2199
  [*Example 1*:
2200
 
2201
  ``` cpp
2202
  struct process {
 
2211
  ```
2212
 
2213
  — *end example*]
2214
 
2215
  A static member may be referred to directly in the scope of its class or
2216
+ in the scope of a class derived [[class.derived]] from its class; in
2217
+ this case, the static member is referred to as if a *qualified-id*
2218
+ expression was used, with the *nested-name-specifier* of the
2219
+ *qualified-id* naming the class scope from which the static member is
2220
+ referenced.
2221
 
2222
  [*Example 2*:
2223
 
2224
  ``` cpp
2225
  int g();
 
2232
  int Y::i = g(); // equivalent to Y::g();
2233
  ```
2234
 
2235
  — *end example*]
2236
 
2237
+ Static members obey the usual class member access rules
2238
+ [[class.access]]. When used in the declaration of a class member, the
 
 
 
 
 
 
 
 
 
 
 
2239
  `static` specifier shall only be used in the member declarations that
2240
  appear within the *member-specification* of the class definition.
2241
 
2242
+ [*Note 1*: It cannot be specified in member declarations that appear in
2243
  namespace scope. — *end note*]
2244
 
2245
  #### Static member functions <a id="class.static.mfct">[[class.static.mfct]]</a>
2246
 
2247
  [*Note 1*: The rules described in  [[class.mfct]] apply to static
2248
  member functions. — *end note*]
2249
 
2250
+ [*Note 2*: A static member function does not have a `this` pointer
2251
+ [[class.this]]. — *end note*]
2252
 
2253
  A static member function shall not be `virtual`. There shall not be a
2254
  static and a non-static member function with the same name and the same
2255
+ parameter types [[over.load]]. A static member function shall not be
2256
  declared `const`, `volatile`, or `const volatile`.
2257
 
2258
  #### Static data members <a id="class.static.data">[[class.static.data]]</a>
2259
 
2260
  A static data member is not part of the subobjects of a class. If a
2261
  static data member is declared `thread_local` there is one copy of the
2262
  member per thread. If a static data member is not declared
2263
  `thread_local` there is one copy of the data member that is shared by
2264
  all the objects of the class.
2265
 
2266
+ A static data member shall not be `mutable` [[dcl.stc]]. A static data
2267
+ member shall not be a direct member [[class.mem]] of an unnamed
2268
+ [[class.pre]] or local [[class.local]] class or of a (possibly
2269
+ indirectly) nested class [[class.nest]] thereof.
2270
+
2271
  The declaration of a non-inline static data member in its class
2272
  definition is not a definition and may be of an incomplete type other
2273
  than cv `void`. The definition for a static data member that is not
2274
  defined inline in the class definition shall appear in a namespace scope
2275
  enclosing the member’s class definition. In the definition at namespace
2276
  scope, the name of the static data member shall be qualified by its
2277
  class name using the `::` operator. The *initializer* expression in the
2278
+ definition of a static data member is in the scope of its class
2279
+ [[basic.scope.class]].
2280
 
2281
  [*Example 1*:
2282
 
2283
  ``` cpp
2284
  class process {
 
2314
  — *end note*]
2315
 
2316
  If a non-volatile non-inline `const` static data member is of integral
2317
  or enumeration type, its declaration in the class definition can specify
2318
  a *brace-or-equal-initializer* in which every *initializer-clause* that
2319
+ is an *assignment-expression* is a constant expression [[expr.const]].
2320
+ The member shall still be defined in a namespace scope if it is odr-used
2321
+ [[basic.def.odr]] in the program and the namespace scope definition
2322
+ shall not contain an *initializer*. An inline static data member may be
2323
+ defined in the class definition and may specify a
2324
  *brace-or-equal-initializer*. If the member is declared with the
2325
  `constexpr` specifier, it may be redeclared in namespace scope with no
2326
+ initializer (this usage is deprecated; see [[depr.static.constexpr]]).
2327
  Declarations of other static data members shall not specify a
2328
  *brace-or-equal-initializer*.
2329
 
2330
+ [*Note 2*: There is exactly one definition of a static data member that
2331
+ is odr-used [[basic.def.odr]] in a valid program. *end note*]
 
 
 
 
2332
 
2333
  [*Note 3*: Static data members of a class in namespace scope have the
2334
+ linkage of the name of the class [[basic.link]]. *end note*]
 
2335
 
2336
  Static data members are initialized and destroyed exactly like non-local
2337
  variables ([[basic.start.static]], [[basic.start.dynamic]],
2338
  [[basic.start.term]]).
2339
 
 
 
2340
  ### Bit-fields <a id="class.bit">[[class.bit]]</a>
2341
 
2342
  A *member-declarator* of the form
2343
 
2344
+ ``` bnf
2345
+ identifierₒₚₜ attribute-specifier-seqₒₚₜ ':' constant-expression brace-or-equal-initializerₒₚₜ
2346
+ ```
2347
+
2348
+ specifies a bit-field. The optional *attribute-specifier-seq* appertains
2349
+ to the entity being declared. A bit-field shall not be a static member.
2350
+ A bit-field shall have integral or enumeration type; the bit-field
2351
+ semantic property is not part of the type of the class member. The
2352
+ *constant-expression* shall be an integral constant expression with a
2353
+ value greater than or equal to zero and is called the *width* of the
2354
+ bit-field. If the width of a bit-field is larger than the width of the
2355
+ bit-field’s type (or, in case of an enumeration type, of its underlying
2356
+ type), the extra bits are padding bits [[basic.types]]. Allocation of
2357
+ bit-fields within a class object is *implementation-defined*. Alignment
2358
+ of bit-fields is *implementation-defined*. Bit-fields are packed into
2359
+ some addressable allocation unit.
2360
 
2361
  [*Note 1*: Bit-fields straddle allocation units on some machines and
2362
  not on others. Bit-fields are assigned right-to-left on some machines,
2363
  left-to-right on others. — *end note*]
2364
 
2365
  A declaration for a bit-field that omits the *identifier* declares an
2366
  *unnamed bit-field*. Unnamed bit-fields are not members and cannot be
2367
+ initialized. An unnamed bit-field shall not be declared with a
2368
+ cv-qualified type.
2369
 
2370
  [*Note 2*: An unnamed bit-field is useful for padding to conform to
2371
  externally-imposed layouts. — *end note*]
2372
 
2373
  As a special case, an unnamed bit-field with a width of zero specifies
2374
  alignment of the next bit-field at an allocation unit boundary. Only
2375
+ when declaring an unnamed bit-field may the width be zero.
 
2376
 
2377
+ The address-of operator `&` shall not be applied to a bit-field, so
2378
+ there are no pointers to bit-fields. A non-const reference shall not be
2379
+ bound to a bit-field [[dcl.init.ref]].
 
 
 
2380
 
2381
  [*Note 3*: If the initializer for a reference of type `const` `T&` is
2382
  an lvalue that refers to a bit-field, the reference is bound to a
2383
  temporary initialized to hold the value of the bit-field; the reference
2384
  is not bound to the bit-field directly. See 
2385
  [[dcl.init.ref]]. — *end note*]
2386
 
2387
+ If a value of integral type (other than `bool`) is stored into a
2388
+ bit-field of width N and the value would be representable in a
2389
+ hypothetical signed or unsigned integer type with width N and the same
2390
+ signedness as the bit-field’s type, the original value and the value of
2391
+ the bit-field compare equal. If the value `true` or `false` is stored
2392
+ into a bit-field of type `bool` of any size (including a one bit
2393
+ bit-field), the original `bool` value and the value of the bit-field
2394
+ compare equal. If a value of an enumeration type is stored into a
2395
+ bit-field of the same type and the width is large enough to hold all the
2396
+ values of that enumeration type [[dcl.enum]], the original value and the
2397
+ value of the bit-field compare equal.
2398
 
2399
  [*Example 1*:
2400
 
2401
  ``` cpp
2402
  enum BOOL { FALSE=0, TRUE=1 };
 
2414
  — *end example*]
2415
 
2416
  ### Nested class declarations <a id="class.nest">[[class.nest]]</a>
2417
 
2418
  A class can be declared within another class. A class declared within
2419
+ another is called a *nested class*. The name of a nested class is local
2420
  to its enclosing class. The nested class is in the scope of its
2421
  enclosing class.
2422
 
2423
+ [*Note 1*: See  [[expr.prim.id]] for restrictions on the use of
2424
+ non-static data members and non-static member functions. — *end note*]
2425
 
2426
  [*Example 1*:
2427
 
2428
  ``` cpp
2429
  int x;
 
2488
  class E::I2 { }; // definition of nested class
2489
  ```
2490
 
2491
  — *end example*]
2492
 
2493
+ Like a member function, a friend function [[class.friend]] defined
2494
  within a nested class is in the lexical scope of that class; it obeys
2495
  the same rules for name binding as a static member function of that
2496
+ class [[class.static]], but it has no special access rights to members
2497
+ of an enclosing class.
2498
 
2499
  ### Nested type names <a id="class.nested.type">[[class.nested.type]]</a>
2500
 
2501
  Type names obey exactly the same scope rules as other names. In
2502
  particular, type names defined within a class definition cannot be used
 
2519
 
2520
  — *end example*]
2521
 
2522
  ## Unions <a id="class.union">[[class.union]]</a>
2523
 
2524
+ A *union* is a class defined with the *class-key* `union`.
2525
+
2526
  In a union, a non-static data member is *active* if its name refers to
2527
+ an object whose lifetime has begun and has not ended [[basic.life]]. At
2528
+ most one of the non-static data members of an object of union type can
2529
+ be active at any time, that is, the value of at most one of the
2530
  non-static data members can be stored in a union at any time.
2531
 
2532
  [*Note 1*: One special guarantee is made in order to simplify the use
2533
  of unions: If a standard-layout union contains several standard-layout
2534
+ structs that share a common initial sequence [[class.mem]], and if a
2535
  non-static data member of an object of this standard-layout union type
2536
  is active and is one of the standard-layout structs, it is permitted to
2537
  inspect the common initial sequence of any of the standard-layout struct
2538
  members; see  [[class.mem]]. — *end note*]
2539
 
2540
  The size of a union is sufficient to contain the largest of its
2541
  non-static data members. Each non-static data member is allocated as if
2542
+ it were the sole member of a non-union class.
2543
 
2544
  [*Note 2*: A union object and its non-static data members are
2545
  pointer-interconvertible ([[basic.compound]], [[expr.static.cast]]). As
2546
  a consequence, all non-static data members of a union object have the
2547
  same address. — *end note*]
2548
 
2549
  A union can have member functions (including constructors and
2550
+ destructors), but it shall not have virtual [[class.virtual]] functions.
2551
+ A union shall not have base classes. A union shall not be used as a base
2552
+ class. If a union contains a non-static data member of reference type
2553
+ the program is ill-formed.
2554
 
2555
+ [*Note 3*: Absent default member initializers [[class.mem]], if any
2556
+ non-static data member of a union has a non-trivial default constructor
2557
+ [[class.default.ctor]], copy constructor, move constructor
2558
+ [[class.copy.ctor]], copy assignment operator, move assignment operator
2559
+ [[class.copy.assign]], or destructor [[class.dtor]], the corresponding
2560
+ member function of the union must be user-provided or it will be
2561
+ implicitly deleted [[dcl.fct.def.delete]] for the union. *end note*]
 
2562
 
2563
  [*Example 1*:
2564
 
2565
  Consider the following union:
2566
 
 
2570
  float f;
2571
  std::string s;
2572
  };
2573
  ```
2574
 
2575
+ Since `std::string` [[string.classes]] declares non-trivial versions of
2576
+ all of the special member functions, `U` will have an implicitly deleted
2577
+ default constructor, copy/move constructor, copy/move assignment
2578
  operator, and destructor. To use `U`, some or all of these member
2579
  functions must be user-provided.
2580
 
2581
  — *end example*]
2582
 
2583
  When the left operand of an assignment operator involves a member access
2584
+ expression [[expr.ref]] that nominates a union member, it may begin the
2585
+ lifetime of that union member, as described below. For an expression
2586
  `E`, define the set S(E) of subexpressions of `E` as follows:
2587
 
2588
  - If `E` is of the form `A.B`, S(E) contains the elements of S(A), and
2589
  also contains `A.B` if `B` names a union member of a non-class,
2590
  non-array type, or of a class type with a trivial default constructor
 
2593
  subscripting operator, S(E) is S(A) if `A` is of array type, S(B) if
2594
  `B` is of array type, and empty otherwise.
2595
  - Otherwise, S(E) is empty.
2596
 
2597
  In an assignment expression of the form `E1 = E2` that uses either the
2598
+ built-in assignment operator [[expr.ass]] or a trivial assignment
2599
+ operator [[class.copy.assign]], for each element `X` of S(`E1`), if
2600
  modification of `X` would have undefined behavior under  [[basic.life]],
2601
  an object of the type of `X` is implicitly created in the nominated
2602
  storage; no initialization is performed and the beginning of its
2603
  lifetime is sequenced after the value computation of the left and right
2604
  operands and before the assignment.
2605
 
2606
  [*Note 4*: This ends the lifetime of the previously-active member of
2607
+ the union, if any [[basic.life]]. — *end note*]
2608
 
2609
  [*Example 2*:
2610
 
2611
  ``` cpp
2612
  union A { int x; int y[4]; };
 
2620
  }
2621
 
2622
  struct X { const int a; int b; };
2623
  union Y { X x; int k; };
2624
  void g() {
2625
+ Y y = { { 1, 2 } }; // OK, y.x is active union member[class.mem]
2626
  int n = y.x.a;
2627
  y.k = 4; // OK: ends lifetime of y.x, y.k is active member of union
2628
  y.x.b = n; // undefined behavior: y.x.b modified outside its lifetime,
2629
  // S(y.x.b) is empty because X's default constructor is deleted,
2630
  // so union member y.x's lifetime does not implicitly start
 
2655
 
2656
  ### Anonymous unions <a id="class.union.anon">[[class.union.anon]]</a>
2657
 
2658
  A union of the form
2659
 
2660
+ ``` bnf
2661
+ union '{' member-specification '}' ';'
2662
+ ```
2663
+
2664
  is called an *anonymous union*; it defines an unnamed type and an
2665
  unnamed object of that type called an *anonymous union object*. Each
2666
  *member-declaration* in the *member-specification* of an anonymous union
2667
  shall either define a non-static data member or be a
2668
+ *static_assert-declaration*. Nested types, anonymous unions, and
2669
+ functions shall not be declared within an anonymous union. The names of
2670
+ the members of an anonymous union shall be distinct from the names of
2671
+ any other entity in the scope in which the anonymous union is declared.
2672
+ For the purpose of name lookup, after the anonymous union definition,
2673
+ the members of the anonymous union are considered to have been defined
2674
+ in the scope in which the anonymous union is declared.
 
 
 
2675
 
2676
  [*Example 1*:
2677
 
2678
  ``` cpp
2679
  void f() {
 
2691
  Anonymous unions declared in a named namespace or in the global
2692
  namespace shall be declared `static`. Anonymous unions declared at block
2693
  scope shall be declared with any storage class allowed for a block-scope
2694
  variable, or with no storage class. A storage class is not allowed in a
2695
  declaration of an anonymous union in a class scope. An anonymous union
2696
+ shall not have private or protected members [[class.access]]. An
2697
+ anonymous union shall not have member functions.
2698
 
2699
  A union for which objects, pointers, or references are declared is not
2700
  an anonymous union.
2701
 
2702
  [*Example 2*:
 
2713
  visible outside the union, and even if it were visible, it is not
2714
  associated with any particular object.
2715
 
2716
  — *end example*]
2717
 
2718
+ [*Note 1*: Initialization of unions with no user-declared constructors
2719
  is described in  [[dcl.init.aggr]]. — *end note*]
2720
 
2721
  A *union-like class* is a union or a class that has an anonymous union
2722
  as a direct member. A union-like class `X` has a set of *variant
2723
  members*. If `X` is a union, a non-static data member of `X` that is not
 
2744
  — *end example*]
2745
 
2746
  ## Local class declarations <a id="class.local">[[class.local]]</a>
2747
 
2748
  A class can be declared within a function definition; such a class is
2749
+ called a *local class*. The name of a local class is local to its
2750
  enclosing scope. The local class is in the scope of the enclosing scope,
2751
  and has the same access to names outside the function as does the
2752
+ enclosing function.
2753
+
2754
+ [*Note 1*: A declaration in a local class cannot odr-use
2755
+ [[basic.def.odr]] a local entity from an enclosing scope. — *end note*]
2756
 
2757
  [*Example 1*:
2758
 
2759
  ``` cpp
2760
  int x;
2761
  void f() {
2762
  static int s;
2763
  int x;
2764
  const int N = 5;
2765
  extern int q();
2766
+ int arr[2];
2767
+ auto [y, z] = arr;
2768
 
2769
  struct local {
2770
+ int g() { return x; } // error: odr-use of non-odr-usable variable x
2771
  int h() { return s; } // OK
2772
  int k() { return ::x; } // OK
2773
  int l() { return q(); } // OK
2774
  int m() { return N; } // OK: not an odr-use
2775
+ int* n() { return &N; } // error: odr-use of non-odr-usable variable N
2776
+ int p() { return y; } // error: odr-use of non-odr-usable structured binding y
2777
  };
2778
  }
2779
 
2780
  local* p = 0; // error: local not in scope
2781
  ```
2782
 
2783
  — *end example*]
2784
 
2785
  An enclosing function has no special access to members of the local
2786
+ class; it obeys the usual access rules [[class.access]]. Member
2787
+ functions of a local class shall be defined within their class
2788
  definition, if they are defined at all.
2789
 
2790
  If class `X` is a local class a nested class `Y` may be declared in
2791
  class `X` and later defined in the definition of class `X` or be later
2792
  defined in the same scope as the definition of class `X`. A class nested
2793
  within a local class is a local class.
2794
 
2795
+ [*Note 2*: A local class cannot have static data members
2796
+ [[class.static.data]]. — *end note*]
2797
 
2798
+ ## Derived classes <a id="class.derived">[[class.derived]]</a>
2799
+
2800
+ A list of base classes can be specified in a class definition using the
2801
+ notation:
2802
+
2803
+ ``` bnf
2804
+ base-clause:
2805
+ ':' base-specifier-list
2806
+ ```
2807
+
2808
+ ``` bnf
2809
+ base-specifier-list:
2810
+ base-specifier '...'ₒₚₜ
2811
+ base-specifier-list ',' base-specifier '...'ₒₚₜ
2812
+ ```
2813
+
2814
+ ``` bnf
2815
+ base-specifier:
2816
+ attribute-specifier-seqₒₚₜ class-or-decltype
2817
+ attribute-specifier-seqₒₚₜ virtual access-specifierₒₚₜ class-or-decltype
2818
+ attribute-specifier-seqₒₚₜ access-specifier virtualₒₚₜ class-or-decltype
2819
+ ```
2820
+
2821
+ ``` bnf
2822
+ class-or-decltype:
2823
+ nested-name-specifierₒₚₜ type-name
2824
+ nested-name-specifier template simple-template-id
2825
+ decltype-specifier
2826
+ ```
2827
+
2828
+ ``` bnf
2829
+ access-specifier:
2830
+ private
2831
+ protected
2832
+ public
2833
+ ```
2834
+
2835
+ The optional *attribute-specifier-seq* appertains to the
2836
+ *base-specifier*.
2837
+
2838
+ A *class-or-decltype* shall denote a (possibly cv-qualified) class type
2839
+ that is not an incompletely defined class [[class.mem]]; any
2840
+ cv-qualifiers are ignored. The class denoted by the *class-or-decltype*
2841
+ of a *base-specifier* is called a *direct base class* for the class
2842
+ being defined. During the lookup for a base class name, non-type names
2843
+ are ignored [[basic.scope.hiding]]. A class `B` is a base class of a
2844
+ class `D` if it is a direct base class of `D` or a direct base class of
2845
+ one of `D`’s base classes. A class is an *indirect base class* of
2846
+ another if it is a base class but not a direct base class. A class is
2847
+ said to be (directly or indirectly) *derived* from its (direct or
2848
+ indirect) base classes.
2849
+
2850
+ [*Note 1*: See [[class.access]] for the meaning of
2851
+ *access-specifier*. — *end note*]
2852
+
2853
+ Unless redeclared in the derived class, members of a base class are also
2854
+ considered to be members of the derived class. Members of a base class
2855
+ other than constructors are said to be *inherited* by the derived class.
2856
+ Constructors of a base class can also be inherited as described in 
2857
+ [[namespace.udecl]]. Inherited members can be referred to in expressions
2858
+ in the same manner as other members of the derived class, unless their
2859
+ names are hidden or ambiguous [[class.member.lookup]].
2860
+
2861
+ [*Note 2*: The scope resolution operator `::` [[expr.prim.id.qual]] can
2862
+ be used to refer to a direct or indirect base member explicitly. This
2863
+ allows access to a name that has been redeclared in the derived class. A
2864
+ derived class can itself serve as a base class subject to access
2865
+ control; see  [[class.access.base]]. A pointer to a derived class can be
2866
+ implicitly converted to a pointer to an accessible unambiguous base
2867
+ class [[conv.ptr]]. An lvalue of a derived class type can be bound to a
2868
+ reference to an accessible unambiguous base class
2869
+ [[dcl.init.ref]]. — *end note*]
2870
+
2871
+ The *base-specifier-list* specifies the type of the *base class
2872
+ subobjects* contained in an object of the derived class type.
2873
+
2874
+ [*Example 1*:
2875
+
2876
+ ``` cpp
2877
+ struct Base {
2878
+ int a, b, c;
2879
+ };
2880
+ ```
2881
+
2882
+ ``` cpp
2883
+ struct Derived : Base {
2884
+ int b;
2885
+ };
2886
+ ```
2887
+
2888
+ ``` cpp
2889
+ struct Derived2 : Derived {
2890
+ int c;
2891
+ };
2892
+ ```
2893
+
2894
+ Here, an object of class `Derived2` will have a subobject of class
2895
+ `Derived` which in turn will have a subobject of class `Base`.
2896
+
2897
+ — *end example*]
2898
+
2899
+ A *base-specifier* followed by an ellipsis is a pack expansion
2900
+ [[temp.variadic]].
2901
+
2902
+ The order in which the base class subobjects are allocated in the most
2903
+ derived object [[intro.object]] is unspecified.
2904
+
2905
+ [*Note 3*: A derived class and its base class subobjects can be
2906
+ represented by a directed acyclic graph (DAG) where an arrow means
2907
+ “directly derived from” (see Figure [[fig:class.dag]]). An arrow
2908
+ need not have a physical representation in memory. A DAG of subobjects
2909
+ is often referred to as a “subobject lattice”.
2910
+
2911
+ <a id="fig:class.dag"></a>
2912
+
2913
+ ![Directed acyclic graph \[fig:class.dag\]](images/figdag.svg)
2914
+
2915
+ — *end note*]
2916
+
2917
+ [*Note 4*: Initialization of objects representing base classes can be
2918
+ specified in constructors; see  [[class.base.init]]. — *end note*]
2919
+
2920
+ [*Note 5*: A base class subobject might have a layout [[basic.stc]]
2921
+ different from the layout of a most derived object of the same type. A
2922
+ base class subobject might have a polymorphic behavior [[class.cdtor]]
2923
+ different from the polymorphic behavior of a most derived object of the
2924
+ same type. A base class subobject may be of zero size [[class]];
2925
+ however, two subobjects that have the same class type and that belong to
2926
+ the same most derived object must not be allocated at the same address
2927
+ [[expr.eq]]. — *end note*]
2928
+
2929
+ ### Multiple base classes <a id="class.mi">[[class.mi]]</a>
2930
+
2931
+ A class can be derived from any number of base classes.
2932
+
2933
+ [*Note 1*: The use of more than one direct base class is often called
2934
+ multiple inheritance. — *end note*]
2935
+
2936
+ [*Example 1*:
2937
+
2938
+ ``` cpp
2939
+ class A { ... };
2940
+ class B { ... };
2941
+ class C { ... };
2942
+ class D : public A, public B, public C { ... };
2943
+ ```
2944
+
2945
+ — *end example*]
2946
+
2947
+ [*Note 2*: The order of derivation is not significant except as
2948
+ specified by the semantics of initialization by constructor
2949
+ [[class.base.init]], cleanup [[class.dtor]], and storage layout (
2950
+ [[class.mem]], [[class.access.spec]]). — *end note*]
2951
+
2952
+ A class shall not be specified as a direct base class of a derived class
2953
+ more than once.
2954
+
2955
+ [*Note 3*: A class can be an indirect base class more than once and can
2956
+ be a direct and an indirect base class. There are limited things that
2957
+ can be done with such a class. The non-static data members and member
2958
+ functions of the direct base class cannot be referred to in the scope of
2959
+ the derived class. However, the static members, enumerations and types
2960
+ can be unambiguously referred to. — *end note*]
2961
+
2962
+ [*Example 2*:
2963
+
2964
+ ``` cpp
2965
+ class X { ... };
2966
+ class Y : public X, public X { ... }; // error
2967
+ ```
2968
+
2969
+ ``` cpp
2970
+ class L { public: int next; ... };
2971
+ class A : public L { ... };
2972
+ class B : public L { ... };
2973
+ class C : public A, public B { void f(); ... }; // well-formed
2974
+ class D : public A, public L { void f(); ... }; // well-formed
2975
+ ```
2976
+
2977
+ — *end example*]
2978
+
2979
+ A base class specifier that does not contain the keyword `virtual`
2980
+ specifies a *non-virtual base class*. A base class specifier that
2981
+ contains the keyword `virtual` specifies a *virtual base class*. For
2982
+ each distinct occurrence of a non-virtual base class in the class
2983
+ lattice of the most derived class, the most derived object
2984
+ [[intro.object]] shall contain a corresponding distinct base class
2985
+ subobject of that type. For each distinct base class that is specified
2986
+ virtual, the most derived object shall contain a single base class
2987
+ subobject of that type.
2988
+
2989
+ [*Note 4*:
2990
+
2991
+ For an object of class type `C`, each distinct occurrence of a
2992
+ (non-virtual) base class `L` in the class lattice of `C` corresponds
2993
+ one-to-one with a distinct `L` subobject within the object of type `C`.
2994
+ Given the class `C` defined above, an object of class `C` will have two
2995
+ subobjects of class `L` as shown in Figure [[fig:class.nonvirt]].
2996
+
2997
+ <a id="fig:class.nonvirt"></a>
2998
+
2999
+ ![Non-virtual base \[fig:class.nonvirt\]](images/fignonvirt.svg)
3000
+
3001
+ In such lattices, explicit qualification can be used to specify which
3002
+ subobject is meant. The body of function `C::f` could refer to the
3003
+ member `next` of each `L` subobject:
3004
+
3005
+ ``` cpp
3006
+ void C::f() { A::next = B::next; } // well-formed
3007
+ ```
3008
+
3009
+ Without the `A::` or `B::` qualifiers, the definition of `C::f` above
3010
+ would be ill-formed because of ambiguity [[class.member.lookup]].
3011
+
3012
+ — *end note*]
3013
+
3014
+ [*Note 5*:
3015
+
3016
+ In contrast, consider the case with a virtual base class:
3017
+
3018
+ ``` cpp
3019
+ class V { ... };
3020
+ class A : virtual public V { ... };
3021
+ class B : virtual public V { ... };
3022
+ class C : public A, public B { ... };
3023
+ ```
3024
+
3025
+ <a id="fig:class.virt"></a>
3026
+
3027
+ ![Virtual base \[fig:class.virt\]](images/figvirt.svg)
3028
+
3029
+ For an object `c` of class type `C`, a single subobject of type `V` is
3030
+ shared by every base class subobject of `c` that has a `virtual` base
3031
+ class of type `V`. Given the class `C` defined above, an object of class
3032
+ `C` will have one subobject of class `V`, as shown in Figure
3033
+ [[fig:class.virt]].
3034
+
3035
+ — *end note*]
3036
+
3037
+ [*Note 6*:
3038
+
3039
+ A class can have both virtual and non-virtual base classes of a given
3040
+ type.
3041
+
3042
+ ``` cpp
3043
+ class B { ... };
3044
+ class X : virtual public B { ... };
3045
+ class Y : virtual public B { ... };
3046
+ class Z : public B { ... };
3047
+ class AA : public X, public Y, public Z { ... };
3048
+ ```
3049
+
3050
+ For an object of class `AA`, all `virtual` occurrences of base class `B`
3051
+ in the class lattice of `AA` correspond to a single `B` subobject within
3052
+ the object of type `AA`, and every other occurrence of a (non-virtual)
3053
+ base class `B` in the class lattice of `AA` corresponds one-to-one with
3054
+ a distinct `B` subobject within the object of type `AA`. Given the class
3055
+ `AA` defined above, class `AA` has two subobjects of class `B`: `Z`’s
3056
+ `B` and the virtual `B` shared by `X` and `Y`, as shown in Figure
3057
+ [[fig:class.virtnonvirt]].
3058
+
3059
+ <a id="fig:class.virtnonvirt"></a>
3060
+
3061
+ ![Virtual and non-virtual base \[fig:class.virtnonvirt\]](images/figvirtnonvirt.svg)
3062
+
3063
+ — *end note*]
3064
+
3065
+ ### Virtual functions <a id="class.virtual">[[class.virtual]]</a>
3066
+
3067
+ A non-static member function is a *virtual function* if it is first
3068
+ declared with the keyword `virtual` or if it overrides a virtual member
3069
+ function declared in a base class (see below).[^7]
3070
+
3071
+ [*Note 1*: Virtual functions support dynamic binding and
3072
+ object-oriented programming. — *end note*]
3073
+
3074
+ A class that declares or inherits a virtual function is called a
3075
+ *polymorphic class*.[^8]
3076
+
3077
+ If a virtual member function `vf` is declared in a class `Base` and in a
3078
+ class `Derived`, derived directly or indirectly from `Base`, a member
3079
+ function `vf` with the same name, parameter-type-list [[dcl.fct]],
3080
+ cv-qualification, and ref-qualifier (or absence of same) as `Base::vf`
3081
+ is declared, then `Derived::vf` *overrides*[^9] `Base::vf`. For
3082
+ convenience we say that any virtual function overrides itself. A virtual
3083
+ member function `C::vf` of a class object `S` is a *final overrider*
3084
+ unless the most derived class [[intro.object]] of which `S` is a base
3085
+ class subobject (if any) declares or inherits another member function
3086
+ that overrides `vf`. In a derived class, if a virtual member function of
3087
+ a base class subobject has more than one final overrider the program is
3088
+ ill-formed.
3089
+
3090
+ [*Example 1*:
3091
+
3092
+ ``` cpp
3093
+ struct A {
3094
+ virtual void f();
3095
+ };
3096
+ struct B : virtual A {
3097
+ virtual void f();
3098
+ };
3099
+ struct C : B , virtual A {
3100
+ using A::f;
3101
+ };
3102
+
3103
+ void foo() {
3104
+ C c;
3105
+ c.f(); // calls B::f, the final overrider
3106
+ c.C::f(); // calls A::f because of the using-declaration
3107
+ }
3108
+ ```
3109
+
3110
+ — *end example*]
3111
+
3112
+ [*Example 2*:
3113
+
3114
+ ``` cpp
3115
+ struct A { virtual void f(); };
3116
+ struct B : A { };
3117
+ struct C : A { void f(); };
3118
+ struct D : B, C { }; // OK: A::f and C::f are the final overriders
3119
+ // for the B and C subobjects, respectively
3120
+ ```
3121
+
3122
+ — *end example*]
3123
+
3124
+ [*Note 2*:
3125
+
3126
+ A virtual member function does not have to be visible to be overridden,
3127
+ for example,
3128
+
3129
+ ``` cpp
3130
+ struct B {
3131
+ virtual void f();
3132
+ };
3133
+ struct D : B {
3134
+ void f(int);
3135
+ };
3136
+ struct D2 : D {
3137
+ void f();
3138
+ };
3139
+ ```
3140
+
3141
+ the function `f(int)` in class `D` hides the virtual function `f()` in
3142
+ its base class `B`; `D::f(int)` is not a virtual function. However,
3143
+ `f()` declared in class `D2` has the same name and the same parameter
3144
+ list as `B::f()`, and therefore is a virtual function that overrides the
3145
+ function `B::f()` even though `B::f()` is not visible in class `D2`.
3146
+
3147
+ — *end note*]
3148
+
3149
+ If a virtual function `f` in some class `B` is marked with the
3150
+ *virt-specifier* `final` and in a class `D` derived from `B` a function
3151
+ `D::f` overrides `B::f`, the program is ill-formed.
3152
+
3153
+ [*Example 3*:
3154
+
3155
+ ``` cpp
3156
+ struct B {
3157
+ virtual void f() const final;
3158
+ };
3159
+
3160
+ struct D : B {
3161
+ void f() const; // error: D::f attempts to override final B::f
3162
+ };
3163
+ ```
3164
+
3165
+ — *end example*]
3166
+
3167
+ If a virtual function is marked with the *virt-specifier* `override` and
3168
+ does not override a member function of a base class, the program is
3169
+ ill-formed.
3170
+
3171
+ [*Example 4*:
3172
+
3173
+ ``` cpp
3174
+ struct B {
3175
+ virtual void f(int);
3176
+ };
3177
+
3178
+ struct D : B {
3179
+ virtual void f(long) override; // error: wrong signature overriding B::f
3180
+ virtual void f(int) override; // OK
3181
+ };
3182
+ ```
3183
+
3184
+ — *end example*]
3185
+
3186
+ A virtual function shall not have a trailing *requires-clause*
3187
+ [[dcl.decl]].
3188
+
3189
+ [*Example 5*:
3190
+
3191
+ ``` cpp
3192
+ struct A {
3193
+ virtual void f() requires true; // error: virtual function cannot be constrained[temp.constr.decl]
3194
+ };
3195
+ ```
3196
+
3197
+ — *end example*]
3198
+
3199
+ Even though destructors are not inherited, a destructor in a derived
3200
+ class overrides a base class destructor declared virtual; see 
3201
+ [[class.dtor]] and  [[class.free]].
3202
+
3203
+ The return type of an overriding function shall be either identical to
3204
+ the return type of the overridden function or *covariant* with the
3205
+ classes of the functions. If a function `D::f` overrides a function
3206
+ `B::f`, the return types of the functions are covariant if they satisfy
3207
+ the following criteria:
3208
+
3209
+ - both are pointers to classes, both are lvalue references to classes,
3210
+ or both are rvalue references to classes[^10]
3211
+ - the class in the return type of `B::f` is the same class as the class
3212
+ in the return type of `D::f`, or is an unambiguous and accessible
3213
+ direct or indirect base class of the class in the return type of
3214
+ `D::f`
3215
+ - both pointers or references have the same cv-qualification and the
3216
+ class type in the return type of `D::f` has the same cv-qualification
3217
+ as or less cv-qualification than the class type in the return type of
3218
+ `B::f`.
3219
+
3220
+ If the class type in the covariant return type of `D::f` differs from
3221
+ that of `B::f`, the class type in the return type of `D::f` shall be
3222
+ complete at the point of declaration of `D::f` or shall be the class
3223
+ type `D`. When the overriding function is called as the final overrider
3224
+ of the overridden function, its result is converted to the type returned
3225
+ by the (statically chosen) overridden function [[expr.call]].
3226
+
3227
+ [*Example 6*:
3228
+
3229
+ ``` cpp
3230
+ class B { };
3231
+ class D : private B { friend class Derived; };
3232
+ struct Base {
3233
+ virtual void vf1();
3234
+ virtual void vf2();
3235
+ virtual void vf3();
3236
+ virtual B* vf4();
3237
+ virtual B* vf5();
3238
+ void f();
3239
+ };
3240
+
3241
+ struct No_good : public Base {
3242
+ D* vf4(); // error: B (base class of D) inaccessible
3243
+ };
3244
+
3245
+ class A;
3246
+ struct Derived : public Base {
3247
+ void vf1(); // virtual and overrides Base::vf1()
3248
+ void vf2(int); // not virtual, hides Base::vf2()
3249
+ char vf3(); // error: invalid difference in return type only
3250
+ D* vf4(); // OK: returns pointer to derived class
3251
+ A* vf5(); // error: returns pointer to incomplete class
3252
+ void f();
3253
+ };
3254
+
3255
+ void g() {
3256
+ Derived d;
3257
+ Base* bp = &d; // standard conversion:
3258
+ // Derived* to Base*
3259
+ bp->vf1(); // calls Derived::vf1()
3260
+ bp->vf2(); // calls Base::vf2()
3261
+ bp->f(); // calls Base::f() (not virtual)
3262
+ B* p = bp->vf4(); // calls Derived::vf4() and converts the
3263
+ // result to B*
3264
+ Derived* dp = &d;
3265
+ D* q = dp->vf4(); // calls Derived::vf4() and does not
3266
+ // convert the result to B*
3267
+ dp->vf2(); // error: argument mismatch
3268
+ }
3269
+ ```
3270
+
3271
+ — *end example*]
3272
+
3273
+ [*Note 3*: The interpretation of the call of a virtual function depends
3274
+ on the type of the object for which it is called (the dynamic type),
3275
+ whereas the interpretation of a call of a non-virtual member function
3276
+ depends only on the type of the pointer or reference denoting that
3277
+ object (the static type) [[expr.call]]. — *end note*]
3278
+
3279
+ [*Note 4*: The `virtual` specifier implies membership, so a virtual
3280
+ function cannot be a non-member [[dcl.fct.spec]] function. Nor can a
3281
+ virtual function be a static member, since a virtual function call
3282
+ relies on a specific object for determining which function to invoke. A
3283
+ virtual function declared in one class can be declared a friend (
3284
+ [[class.friend]]) in another class. — *end note*]
3285
+
3286
+ A virtual function declared in a class shall be defined, or declared
3287
+ pure [[class.abstract]] in that class, or both; no diagnostic is
3288
+ required [[basic.def.odr]].
3289
+
3290
+ [*Example 7*:
3291
+
3292
+ Here are some uses of virtual functions with multiple base classes:
3293
+
3294
+ ``` cpp
3295
+ struct A {
3296
+ virtual void f();
3297
+ };
3298
+
3299
+ struct B1 : A { // note non-virtual derivation
3300
+ void f();
3301
+ };
3302
+
3303
+ struct B2 : A {
3304
+ void f();
3305
+ };
3306
+
3307
+ struct D : B1, B2 { // D has two separate A subobjects
3308
+ };
3309
+
3310
+ void foo() {
3311
+ D d;
3312
+ // A* ap = &d; // would be ill-formed: ambiguous
3313
+ B1* b1p = &d;
3314
+ A* ap = b1p;
3315
+ D* dp = &d;
3316
+ ap->f(); // calls D::B1::f
3317
+ dp->f(); // error: ambiguous
3318
+ }
3319
+ ```
3320
+
3321
+ In class `D` above there are two occurrences of class `A` and hence two
3322
+ occurrences of the virtual member function `A::f`. The final overrider
3323
+ of `B1::A::f` is `B1::f` and the final overrider of `B2::A::f` is
3324
+ `B2::f`.
3325
+
3326
+ — *end example*]
3327
+
3328
+ [*Example 8*:
3329
+
3330
+ The following example shows a function that does not have a unique final
3331
+ overrider:
3332
+
3333
+ ``` cpp
3334
+ struct A {
3335
+ virtual void f();
3336
+ };
3337
+
3338
+ struct VB1 : virtual A { // note virtual derivation
3339
+ void f();
3340
+ };
3341
+
3342
+ struct VB2 : virtual A {
3343
+ void f();
3344
+ };
3345
+
3346
+ struct Error : VB1, VB2 { // error
3347
+ };
3348
+
3349
+ struct Okay : VB1, VB2 {
3350
+ void f();
3351
+ };
3352
+ ```
3353
+
3354
+ Both `VB1::f` and `VB2::f` override `A::f` but there is no overrider of
3355
+ both of them in class `Error`. This example is therefore ill-formed.
3356
+ Class `Okay` is well-formed, however, because `Okay::f` is a final
3357
+ overrider.
3358
+
3359
+ — *end example*]
3360
+
3361
+ [*Example 9*:
3362
+
3363
+ The following example uses the well-formed classes from above.
3364
+
3365
+ ``` cpp
3366
+ struct VB1a : virtual A { // does not declare f
3367
+ };
3368
+
3369
+ struct Da : VB1a, VB2 {
3370
+ };
3371
+
3372
+ void foe() {
3373
+ VB1a* vb1ap = new Da;
3374
+ vb1ap->f(); // calls VB2::f
3375
+ }
3376
+ ```
3377
+
3378
+ — *end example*]
3379
+
3380
+ Explicit qualification with the scope operator [[expr.prim.id.qual]]
3381
+ suppresses the virtual call mechanism.
3382
+
3383
+ [*Example 10*:
3384
+
3385
+ ``` cpp
3386
+ class B { public: virtual void f(); };
3387
+ class D : public B { public: void f(); };
3388
+
3389
+ void D::f() { ... B::f(); }
3390
+ ```
3391
+
3392
+ Here, the function call in `D::f` really does call `B::f` and not
3393
+ `D::f`.
3394
+
3395
+ — *end example*]
3396
+
3397
+ A function with a deleted definition [[dcl.fct.def]] shall not override
3398
+ a function that does not have a deleted definition. Likewise, a function
3399
+ that does not have a deleted definition shall not override a function
3400
+ with a deleted definition.
3401
+
3402
+ A `consteval` virtual function shall not override a virtual function
3403
+ that is not `consteval`. A `consteval` virtual function shall not be
3404
+ overridden by a virtual function that is not `consteval`.
3405
+
3406
+ ### Abstract classes <a id="class.abstract">[[class.abstract]]</a>
3407
+
3408
+ [*Note 1*: The abstract class mechanism supports the notion of a
3409
+ general concept, such as a `shape`, of which only more concrete
3410
+ variants, such as `circle` and `square`, can actually be used. An
3411
+ abstract class can also be used to define an interface for which derived
3412
+ classes provide a variety of implementations. — *end note*]
3413
+
3414
+ A virtual function is specified as a *pure virtual function* by using a
3415
+ *pure-specifier* [[class.mem]] in the function declaration in the class
3416
+ definition.
3417
+
3418
+ [*Note 2*: Such a function might be inherited: see
3419
+ below. — *end note*]
3420
+
3421
+ A class is an *abstract class* if it has at least one pure virtual
3422
+ function.
3423
+
3424
+ [*Note 3*: An abstract class can be used only as a base class of some
3425
+ other class; no objects of an abstract class can be created except as
3426
+ subobjects of a class derived from it ([[basic.def]],
3427
+ [[class.mem]]). — *end note*]
3428
+
3429
+ A pure virtual function need be defined only if called with, or as if
3430
+ with [[class.dtor]], the *qualified-id* syntax [[expr.prim.id.qual]].
3431
+
3432
+ [*Example 1*:
3433
+
3434
+ ``` cpp
3435
+ class point { ... };
3436
+ class shape { // abstract class
3437
+ point center;
3438
+ public:
3439
+ point where() { return center; }
3440
+ void move(point p) { center=p; draw(); }
3441
+ virtual void rotate(int) = 0; // pure virtual
3442
+ virtual void draw() = 0; // pure virtual
3443
+ };
3444
+ ```
3445
+
3446
+ — *end example*]
3447
+
3448
+ [*Note 4*: A function declaration cannot provide both a
3449
+ *pure-specifier* and a definition. — *end note*]
3450
+
3451
+ [*Example 2*:
3452
+
3453
+ ``` cpp
3454
+ struct C {
3455
+ virtual void f() = 0 { }; // error
3456
+ };
3457
+ ```
3458
+
3459
+ — *end example*]
3460
+
3461
+ [*Note 5*: An abstract class type cannot be used as a parameter or
3462
+ return type of a function being defined [[dcl.fct]] or called
3463
+ [[expr.call]], except as specified in [[dcl.type.simple]]. Further, an
3464
+ abstract class type cannot be used as the type of an explicit type
3465
+ conversion ([[expr.static.cast]], [[expr.reinterpret.cast]],
3466
+ [[expr.const.cast]]), because the resulting prvalue would be of abstract
3467
+ class type [[basic.lval]]. However, pointers and references to abstract
3468
+ class types can appear in such contexts. — *end note*]
3469
+
3470
+ A class is abstract if it contains or inherits at least one pure virtual
3471
+ function for which the final overrider is pure virtual.
3472
+
3473
+ [*Example 3*:
3474
+
3475
+ ``` cpp
3476
+ class ab_circle : public shape {
3477
+ int radius;
3478
+ public:
3479
+ void rotate(int) { }
3480
+ // ab_circle::draw() is a pure virtual
3481
+ };
3482
+ ```
3483
+
3484
+ Since `shape::draw()` is a pure virtual function `ab_circle::draw()` is
3485
+ a pure virtual by default. The alternative declaration,
3486
+
3487
+ ``` cpp
3488
+ class circle : public shape {
3489
+ int radius;
3490
+ public:
3491
+ void rotate(int) { }
3492
+ void draw(); // a definition is required somewhere
3493
+ };
3494
+ ```
3495
+
3496
+ would make class `circle` non-abstract and a definition of
3497
+ `circle::draw()` must be provided.
3498
+
3499
+ — *end example*]
3500
+
3501
+ [*Note 6*: An abstract class can be derived from a class that is not
3502
+ abstract, and a pure virtual function may override a virtual function
3503
+ which is not pure. — *end note*]
3504
+
3505
+ Member functions can be called from a constructor (or destructor) of an
3506
+ abstract class; the effect of making a virtual call [[class.virtual]] to
3507
+ a pure virtual function directly or indirectly for the object being
3508
+ created (or destroyed) from such a constructor (or destructor) is
3509
+ undefined.
3510
+
3511
+ ## Member name lookup <a id="class.member.lookup">[[class.member.lookup]]</a>
3512
+
3513
+ Member name lookup determines the meaning of a name (*id-expression*) in
3514
+ a class scope [[basic.scope.class]]. Name lookup can result in an
3515
+ ambiguity, in which case the program is ill-formed. For an
3516
+ *unqualified-id*, name lookup begins in the class scope of `this`; for a
3517
+ *qualified-id*, name lookup begins in the scope of the
3518
+ *nested-name-specifier*. Name lookup takes place before access control (
3519
+ [[basic.lookup]], [[class.access]]).
3520
+
3521
+ The following steps define the result of name lookup for a member name
3522
+ `f` in a class scope `C`.
3523
+
3524
+ The *lookup set* for `f` in `C`, called S(f,C), consists of two
3525
+ component sets: the *declaration set*, a set of members named `f`; and
3526
+ the *subobject set*, a set of subobjects where declarations of these
3527
+ members (possibly including *using-declaration*s) were found. In the
3528
+ declaration set, *using-declaration*s are replaced by the set of
3529
+ designated members that are not hidden or overridden by members of the
3530
+ derived class [[namespace.udecl]], and type declarations (including
3531
+ injected-class-names) are replaced by the types they designate. S(f,C)
3532
+ is calculated as follows:
3533
+
3534
+ If `C` contains a declaration of the name `f`, the declaration set
3535
+ contains every declaration of `f` declared in `C` that satisfies the
3536
+ requirements of the language construct in which the lookup occurs.
3537
+
3538
+ [*Note 1*: Looking up a name in an *elaborated-type-specifier*
3539
+ [[basic.lookup.elab]] or *base-specifier* [[class.derived]], for
3540
+ instance, ignores all non-type declarations, while looking up a name in
3541
+ a *nested-name-specifier* [[basic.lookup.qual]] ignores function,
3542
+ variable, and enumerator declarations. As another example, looking up a
3543
+ name in a *using-declaration* [[namespace.udecl]] includes the
3544
+ declaration of a class or enumeration that would ordinarily be hidden by
3545
+ another declaration of that name in the same scope. — *end note*]
3546
+
3547
+ If the resulting declaration set is not empty, the subobject set
3548
+ contains `C` itself, and calculation is complete.
3549
+
3550
+ Otherwise (i.e., `C` does not contain a declaration of `f` or the
3551
+ resulting declaration set is empty), S(f,C) is initially empty. If `C`
3552
+ has base classes, calculate the lookup set for `f` in each direct base
3553
+ class subobject Bᵢ, and merge each such lookup set S(f,Bᵢ) in turn into
3554
+ S(f,C).
3555
+
3556
+ The following steps define the result of merging lookup set S(f,Bᵢ) into
3557
+ the intermediate S(f,C):
3558
+
3559
+ - If each of the subobject members of S(f,Bᵢ) is a base class subobject
3560
+ of at least one of the subobject members of S(f,C), or if S(f,Bᵢ) is
3561
+ empty, S(f,C) is unchanged and the merge is complete. Conversely, if
3562
+ each of the subobject members of S(f,C) is a base class subobject of
3563
+ at least one of the subobject members of S(f,Bᵢ), or if S(f,C) is
3564
+ empty, the new S(f,C) is a copy of S(f,Bᵢ).
3565
+ - Otherwise, if the declaration sets of S(f,Bᵢ) and S(f,C) differ, the
3566
+ merge is ambiguous: the new S(f,C) is a lookup set with an invalid
3567
+ declaration set and the union of the subobject sets. In subsequent
3568
+ merges, an invalid declaration set is considered different from any
3569
+ other.
3570
+ - Otherwise, the new S(f,C) is a lookup set with the shared set of
3571
+ declarations and the union of the subobject sets.
3572
+
3573
+ The result of name lookup for `f` in `C` is the declaration set of
3574
+ S(f,C). If it is an invalid set, the program is ill-formed.
3575
+
3576
+ [*Example 1*:
3577
+
3578
+ ``` cpp
3579
+ struct A { int x; }; // S(x,A) = { { A::x }, { A } }
3580
+ struct B { float x; }; // S(x,B) = { { B::x }, { B } }
3581
+ struct C: public A, public B { }; // S(x,C) = { invalid, { A in C, B in C } }
3582
+ struct D: public virtual C { }; // S(x,D) = S(x,C)
3583
+ struct E: public virtual C { char x; }; // S(x,E) = { { E::x }, { E } }
3584
+ struct F: public D, public E { }; // S(x,F) = S(x,E)
3585
+ int main() {
3586
+ F f;
3587
+ f.x = 0; // OK, lookup finds E::x
3588
+ }
3589
+ ```
3590
+
3591
+ S(x,F) is unambiguous because the `A` and `B` base class subobjects of
3592
+ `D` are also base class subobjects of `E`, so S(x,D) is discarded in the
3593
+ first merge step.
3594
+
3595
+ — *end example*]
3596
+
3597
+ If the name of an overloaded function is unambiguously found, overload
3598
+ resolution [[over.match]] also takes place before access control.
3599
+ Ambiguities can often be resolved by qualifying a name with its class
3600
+ name.
3601
+
3602
+ [*Example 2*:
3603
+
3604
+ ``` cpp
3605
+ struct A {
3606
+ int f();
3607
+ };
3608
+ ```
3609
+
3610
+ ``` cpp
3611
+ struct B {
3612
+ int f();
3613
+ };
3614
+ ```
3615
+
3616
+ ``` cpp
3617
+ struct C : A, B {
3618
+ int f() { return A::f() + B::f(); }
3619
+ };
3620
+ ```
3621
+
3622
+ — *end example*]
3623
+
3624
+ [*Note 2*: A static member, a nested type or an enumerator defined in a
3625
+ base class `T` can unambiguously be found even if an object has more
3626
+ than one base class subobject of type `T`. Two base class subobjects
3627
+ share the non-static member subobjects of their common virtual base
3628
+ classes. — *end note*]
3629
+
3630
+ [*Example 3*:
3631
+
3632
+ ``` cpp
3633
+ struct V {
3634
+ int v;
3635
+ };
3636
+ struct A {
3637
+ int a;
3638
+ static int s;
3639
+ enum { e };
3640
+ };
3641
+ struct B : A, virtual V { };
3642
+ struct C : A, virtual V { };
3643
+ struct D : B, C { };
3644
+
3645
+ void f(D* pd) {
3646
+ pd->v++; // OK: only one v (virtual)
3647
+ pd->s++; // OK: only one s (static)
3648
+ int i = pd->e; // OK: only one e (enumerator)
3649
+ pd->a++; // error: ambiguous: two a{s} in D
3650
+ }
3651
+ ```
3652
+
3653
+ — *end example*]
3654
+
3655
+ [*Note 3*: When virtual base classes are used, a hidden declaration
3656
+ can be reached along a path through the subobject lattice that does not
3657
+ pass through the hiding declaration. This is not an ambiguity. The
3658
+ identical use with non-virtual base classes is an ambiguity; in that
3659
+ case there is no unique instance of the name that hides all the
3660
+ others. — *end note*]
3661
+
3662
+ [*Example 4*:
3663
+
3664
+ ``` cpp
3665
+ struct V { int f(); int x; };
3666
+ struct W { int g(); int y; };
3667
+ struct B : virtual V, W {
3668
+ int f(); int x;
3669
+ int g(); int y;
3670
+ };
3671
+ struct C : virtual V, W { };
3672
+
3673
+ struct D : B, C { void glorp(); };
3674
+ ```
3675
+
3676
+ <a id="fig:class.lookup"></a>
3677
+
3678
+ ![Name lookup \[fig:class.lookup\]](images/figname.svg)
3679
+
3680
+ As illustrated in Figure [[fig:class.lookup]], the names declared in
3681
+ `V` and the left-hand instance of `W` are hidden by those in `B`, but
3682
+ the names declared in the right-hand instance of `W` are not hidden at
3683
+ all.
3684
+
3685
+ ``` cpp
3686
+ void D::glorp() {
3687
+ x++; // OK: B::x hides V::x
3688
+ f(); // OK: B::f() hides V::f()
3689
+ y++; // error: B::y and C's W::y
3690
+ g(); // error: B::g() and C's W::g()
3691
+ }
3692
+ ```
3693
+
3694
+ — *end example*]
3695
+
3696
+ An explicit or implicit conversion from a pointer to or an expression
3697
+ designating an object of a derived class to a pointer or reference to
3698
+ one of its base classes shall unambiguously refer to a unique object
3699
+ representing the base class.
3700
+
3701
+ [*Example 5*:
3702
+
3703
+ ``` cpp
3704
+ struct V { };
3705
+ struct A { };
3706
+ struct B : A, virtual V { };
3707
+ struct C : A, virtual V { };
3708
+ struct D : B, C { };
3709
+
3710
+ void g() {
3711
+ D d;
3712
+ B* pb = &d;
3713
+ A* pa = &d; // error: ambiguous: C's A or B's A?
3714
+ V* pv = &d; // OK: only one V subobject
3715
+ }
3716
+ ```
3717
+
3718
+ — *end example*]
3719
+
3720
+ [*Note 4*: Even if the result of name lookup is unambiguous, use of a
3721
+ name found in multiple subobjects might still be ambiguous (
3722
+ [[conv.mem]], [[expr.ref]], [[class.access.base]]). — *end note*]
3723
+
3724
+ [*Example 6*:
3725
+
3726
+ ``` cpp
3727
+ struct B1 {
3728
+ void f();
3729
+ static void f(int);
3730
+ int i;
3731
+ };
3732
+ struct B2 {
3733
+ void f(double);
3734
+ };
3735
+ struct I1: B1 { };
3736
+ struct I2: B1 { };
3737
+
3738
+ struct D: I1, I2, B2 {
3739
+ using B1::f;
3740
+ using B2::f;
3741
+ void g() {
3742
+ f(); // Ambiguous conversion of this
3743
+ f(0); // Unambiguous (static)
3744
+ f(0.0); // Unambiguous (only one B2)
3745
+ int B1::* mpB1 = &D::i; // Unambiguous
3746
+ int D::* mpD = &D::i; // Ambiguous conversion
3747
+ }
3748
+ };
3749
+ ```
3750
+
3751
+ — *end example*]
3752
+
3753
+ ## Member access control <a id="class.access">[[class.access]]</a>
3754
+
3755
+ A member of a class can be
3756
+
3757
+ - private; that is, its name can be used only by members and friends of
3758
+ the class in which it is declared.
3759
+ - protected; that is, its name can be used only by members and friends
3760
+ of the class in which it is declared, by classes derived from that
3761
+ class, and by their friends (see  [[class.protected]]).
3762
+ - public; that is, its name can be used anywhere without access
3763
+ restriction.
3764
+
3765
+ A member of a class can also access all the names to which the class has
3766
+ access. A local class of a member function may access the same names
3767
+ that the member function itself may access.[^11]
3768
+
3769
+ Members of a class defined with the keyword `class` are `private` by
3770
+ default. Members of a class defined with the keywords `struct` or
3771
+ `union` are public by default.
3772
+
3773
+ [*Example 1*:
3774
+
3775
+ ``` cpp
3776
+ class X {
3777
+ int a; // X::a is private by default
3778
+ };
3779
+
3780
+ struct S {
3781
+ int a; // S::a is public by default
3782
+ };
3783
+ ```
3784
+
3785
+ — *end example*]
3786
+
3787
+ Access control is applied uniformly to all names, whether the names are
3788
+ referred to from declarations or expressions.
3789
+
3790
+ [*Note 1*: Access control applies to names nominated by friend
3791
+ declarations [[class.friend]] and *using-declaration*s
3792
+ [[namespace.udecl]]. — *end note*]
3793
+
3794
+ In the case of overloaded function names, access control is applied to
3795
+ the function selected by overload resolution.
3796
+
3797
+ [*Note 2*:
3798
+
3799
+ Because access control applies to names, if access control is applied to
3800
+ a typedef name, only the accessibility of the typedef name itself is
3801
+ considered. The accessibility of the entity referred to by the typedef
3802
+ is not considered. For example,
3803
+
3804
+ ``` cpp
3805
+ class A {
3806
+ class B { };
3807
+ public:
3808
+ typedef B BB;
3809
+ };
3810
+
3811
+ void f() {
3812
+ A::BB x; // OK, typedef name A::BB is public
3813
+ A::B y; // access error, A::B is private
3814
+ }
3815
+ ```
3816
+
3817
+ — *end note*]
3818
+
3819
+ [*Note 3*: Access to members and base classes is controlled, not their
3820
+ visibility [[basic.scope.hiding]]. Names of members are still visible,
3821
+ and implicit conversions to base classes are still considered, when
3822
+ those members and base classes are inaccessible. — *end note*]
3823
+
3824
+ The interpretation of a given construct is established without regard to
3825
+ access control. If the interpretation established makes use of
3826
+ inaccessible member names or base classes, the construct is ill-formed.
3827
+
3828
+ All access controls in [[class.access]] affect the ability to access a
3829
+ class member name from the declaration of a particular entity, including
3830
+ parts of the declaration preceding the name of the entity being declared
3831
+ and, if the entity is a class, the definitions of members of the class
3832
+ appearing outside the class’s *member-specification*.
3833
+
3834
+ [*Note 4*: This access also applies to implicit references to
3835
+ constructors, conversion functions, and destructors. — *end note*]
3836
+
3837
+ [*Example 2*:
3838
+
3839
+ ``` cpp
3840
+ class A {
3841
+ typedef int I; // private member
3842
+ I f();
3843
+ friend I g(I);
3844
+ static I x;
3845
+ template<int> struct Q;
3846
+ template<int> friend struct R;
3847
+ protected:
3848
+ struct B { };
3849
+ };
3850
+
3851
+ A::I A::f() { return 0; }
3852
+ A::I g(A::I p = A::x);
3853
+ A::I g(A::I p) { return 0; }
3854
+ A::I A::x = 0;
3855
+ template<A::I> struct A::Q { };
3856
+ template<A::I> struct R { };
3857
+
3858
+ struct D: A::B, A { };
3859
+ ```
3860
+
3861
+ Here, all the uses of `A::I` are well-formed because `A::f`, `A::x`, and
3862
+ `A::Q` are members of class `A` and `g` and `R` are friends of class
3863
+ `A`. This implies, for example, that access checking on the first use of
3864
+ `A::I` must be deferred until it is determined that this use of `A::I`
3865
+ is as the return type of a member of class `A`. Similarly, the use of
3866
+ `A::B` as a *base-specifier* is well-formed because `D` is derived from
3867
+ `A`, so checking of *base-specifier*s must be deferred until the entire
3868
+ *base-specifier-list* has been seen.
3869
+
3870
+ — *end example*]
3871
+
3872
+ The names in a default argument [[dcl.fct.default]] are bound at the
3873
+ point of declaration, and access is checked at that point rather than at
3874
+ any points of use of the default argument. Access checking for default
3875
+ arguments in function templates and in member functions of class
3876
+ templates is performed as described in  [[temp.inst]].
3877
+
3878
+ The names in a default *template-argument* [[temp.param]] have their
3879
+ access checked in the context in which they appear rather than at any
3880
+ points of use of the default *template-argument*.
3881
+
3882
+ [*Example 3*:
3883
+
3884
+ ``` cpp
3885
+ class B { };
3886
+ template <class T> class C {
3887
+ protected:
3888
+ typedef T TT;
3889
+ };
3890
+
3891
+ template <class U, class V = typename U::TT>
3892
+ class D : public U { };
3893
+
3894
+ D <C<B> >* d; // access error, C::TT is protected
3895
+ ```
3896
+
3897
+ — *end example*]
3898
+
3899
+ ### Access specifiers <a id="class.access.spec">[[class.access.spec]]</a>
3900
+
3901
+ Member declarations can be labeled by an *access-specifier* (
3902
+ [[class.derived]]):
3903
+
3904
+ ``` bnf
3905
+ access-specifier ':' member-specificationₒₚₜ
3906
+ ```
3907
+
3908
+ An *access-specifier* specifies the access rules for members following
3909
+ it until the end of the class or until another *access-specifier* is
3910
+ encountered.
3911
+
3912
+ [*Example 1*:
3913
+
3914
+ ``` cpp
3915
+ class X {
3916
+ int a; // X::a is private by default: class used
3917
+ public:
3918
+ int b; // X::b is public
3919
+ int c; // X::c is public
3920
+ };
3921
+ ```
3922
+
3923
+ — *end example*]
3924
+
3925
+ Any number of access specifiers is allowed and no particular order is
3926
+ required.
3927
+
3928
+ [*Example 2*:
3929
+
3930
+ ``` cpp
3931
+ struct S {
3932
+ int a; // S::a is public by default: struct used
3933
+ protected:
3934
+ int b; // S::b is protected
3935
+ private:
3936
+ int c; // S::c is private
3937
+ public:
3938
+ int d; // S::d is public
3939
+ };
3940
+ ```
3941
+
3942
+ — *end example*]
3943
+
3944
+ [*Note 1*: The effect of access control on the order of allocation of
3945
+ data members is specified in  [[expr.rel]]. — *end note*]
3946
+
3947
+ When a member is redeclared within its class definition, the access
3948
+ specified at its redeclaration shall be the same as at its initial
3949
+ declaration.
3950
+
3951
+ [*Example 3*:
3952
+
3953
+ ``` cpp
3954
+ struct S {
3955
+ class A;
3956
+ enum E : int;
3957
+ private:
3958
+ class A { }; // error: cannot change access
3959
+ enum E: int { e0 }; // error: cannot change access
3960
+ };
3961
+ ```
3962
+
3963
+ — *end example*]
3964
+
3965
+ [*Note 2*: In a derived class, the lookup of a base class name will
3966
+ find the injected-class-name instead of the name of the base class in
3967
+ the scope in which it was declared. The injected-class-name might be
3968
+ less accessible than the name of the base class in the scope in which it
3969
+ was declared. — *end note*]
3970
+
3971
+ [*Example 4*:
3972
+
3973
+ ``` cpp
3974
+ class A { };
3975
+ class B : private A { };
3976
+ class C : public B {
3977
+ A* p; // error: injected-class-name A is inaccessible
3978
+ ::A* q; // OK
3979
+ };
3980
+ ```
3981
+
3982
+ — *end example*]
3983
+
3984
+ ### Accessibility of base classes and base class members <a id="class.access.base">[[class.access.base]]</a>
3985
+
3986
+ If a class is declared to be a base class [[class.derived]] for another
3987
+ class using the `public` access specifier, the public members of the
3988
+ base class are accessible as public members of the derived class and
3989
+ protected members of the base class are accessible as protected members
3990
+ of the derived class. If a class is declared to be a base class for
3991
+ another class using the `protected` access specifier, the public and
3992
+ protected members of the base class are accessible as protected members
3993
+ of the derived class. If a class is declared to be a base class for
3994
+ another class using the `private` access specifier, the public and
3995
+ protected members of the base class are accessible as private members of
3996
+ the derived class.[^12]
3997
+
3998
+ In the absence of an *access-specifier* for a base class, `public` is
3999
+ assumed when the derived class is defined with the *class-key* `struct`
4000
+ and `private` is assumed when the class is defined with the *class-key*
4001
+ `class`.
4002
+
4003
+ [*Example 1*:
4004
+
4005
+ ``` cpp
4006
+ class B { ... };
4007
+ class D1 : private B { ... };
4008
+ class D2 : public B { ... };
4009
+ class D3 : B { ... }; // B private by default
4010
+ struct D4 : public B { ... };
4011
+ struct D5 : private B { ... };
4012
+ struct D6 : B { ... }; // B public by default
4013
+ class D7 : protected B { ... };
4014
+ struct D8 : protected B { ... };
4015
+ ```
4016
+
4017
+ Here `B` is a public base of `D2`, `D4`, and `D6`, a private base of
4018
+ `D1`, `D3`, and `D5`, and a protected base of `D7` and `D8`.
4019
+
4020
+ — *end example*]
4021
+
4022
+ [*Note 1*:
4023
+
4024
+ A member of a private base class might be inaccessible as an inherited
4025
+ member name, but accessible directly. Because of the rules on pointer
4026
+ conversions [[conv.ptr]] and explicit casts ([[expr.type.conv]],
4027
+ [[expr.static.cast]], [[expr.cast]]), a conversion from a pointer to a
4028
+ derived class to a pointer to an inaccessible base class might be
4029
+ ill-formed if an implicit conversion is used, but well-formed if an
4030
+ explicit cast is used. For example,
4031
+
4032
+ ``` cpp
4033
+ class B {
4034
+ public:
4035
+ int mi; // non-static member
4036
+ static int si; // static member
4037
+ };
4038
+ class D : private B {
4039
+ };
4040
+ class DD : public D {
4041
+ void f();
4042
+ };
4043
+
4044
+ void DD::f() {
4045
+ mi = 3; // error: mi is private in D
4046
+ si = 3; // error: si is private in D
4047
+ ::B b;
4048
+ b.mi = 3; // OK (b.mi is different from this->mi)
4049
+ b.si = 3; // OK (b.si is different from this->si)
4050
+ ::B::si = 3; // OK
4051
+ ::B* bp1 = this; // error: B is a private base class
4052
+ ::B* bp2 = (::B*)this; // OK with cast
4053
+ bp2->mi = 3; // OK: access through a pointer to B.
4054
+ }
4055
+ ```
4056
+
4057
+ — *end note*]
4058
+
4059
+ A base class `B` of `N` is *accessible* at *R*, if
4060
+
4061
+ - an invented public member of `B` would be a public member of `N`, or
4062
+ - *R* occurs in a member or friend of class `N`, and an invented public
4063
+ member of `B` would be a private or protected member of `N`, or
4064
+ - *R* occurs in a member or friend of a class `P` derived from `N`, and
4065
+ an invented public member of `B` would be a private or protected
4066
+ member of `P`, or
4067
+ - there exists a class `S` such that `B` is a base class of `S`
4068
+ accessible at *R* and `S` is a base class of `N` accessible at *R*.
4069
+
4070
+ [*Example 2*:
4071
+
4072
+ ``` cpp
4073
+ class B {
4074
+ public:
4075
+ int m;
4076
+ };
4077
+
4078
+ class S: private B {
4079
+ friend class N;
4080
+ };
4081
+
4082
+ class N: private S {
4083
+ void f() {
4084
+ B* p = this; // OK because class S satisfies the fourth condition above: B is a base class of N
4085
+ // accessible in f() because B is an accessible base class of S and S is an accessible
4086
+ // base class of N.
4087
+ }
4088
+ };
4089
+ ```
4090
+
4091
+ — *end example*]
4092
+
4093
+ If a base class is accessible, one can implicitly convert a pointer to a
4094
+ derived class to a pointer to that base class ([[conv.ptr]],
4095
+ [[conv.mem]]).
4096
+
4097
+ [*Note 2*: It follows that members and friends of a class `X` can
4098
+ implicitly convert an `X*` to a pointer to a private or protected
4099
+ immediate base class of `X`. — *end note*]
4100
+
4101
+ The access to a member is affected by the class in which the member is
4102
+ named. This naming class is the class in which the member name was
4103
+ looked up and found.
4104
+
4105
+ [*Note 3*: This class can be explicit, e.g., when a *qualified-id* is
4106
+ used, or implicit, e.g., when a class member access operator
4107
+ [[expr.ref]] is used (including cases where an implicit “`this->`” is
4108
+ added). If both a class member access operator and a *qualified-id* are
4109
+ used to name the member (as in `p->T::m`), the class naming the member
4110
+ is the class denoted by the *nested-name-specifier* of the
4111
+ *qualified-id* (that is, `T`). — *end note*]
4112
+
4113
+ A member `m` is accessible at the point *R* when named in class `N` if
4114
+
4115
+ - `m` as a member of `N` is public, or
4116
+ - `m` as a member of `N` is private, and *R* occurs in a member or
4117
+ friend of class `N`, or
4118
+ - `m` as a member of `N` is protected, and *R* occurs in a member or
4119
+ friend of class `N`, or in a member of a class `P` derived from `N`,
4120
+ where `m` as a member of `P` is public, private, or protected, or
4121
+ - there exists a base class `B` of `N` that is accessible at *R*, and
4122
+ `m` is accessible at *R* when named in class `B`.
4123
+ \[*Example 3*:
4124
+ ``` cpp
4125
+ class B;
4126
+ class A {
4127
+ private:
4128
+ int i;
4129
+ friend void f(B*);
4130
+ };
4131
+ class B : public A { };
4132
+ void f(B* p) {
4133
+ p->i = 1; // OK: B* can be implicitly converted to A*, and f has access to i in A
4134
+ }
4135
+ ```
4136
+
4137
+ — *end example*]
4138
+
4139
+ If a class member access operator, including an implicit “`this->`”, is
4140
+ used to access a non-static data member or non-static member function,
4141
+ the reference is ill-formed if the left operand (considered as a pointer
4142
+ in the “`.`” operator case) cannot be implicitly converted to a pointer
4143
+ to the naming class of the right operand.
4144
+
4145
+ [*Note 4*: This requirement is in addition to the requirement that the
4146
+ member be accessible as named. — *end note*]
4147
+
4148
+ ### Friends <a id="class.friend">[[class.friend]]</a>
4149
+
4150
+ A friend of a class is a function or class that is given permission to
4151
+ use the private and protected member names from the class. A class
4152
+ specifies its friends, if any, by way of friend declarations. Such
4153
+ declarations give special access rights to the friends, but they do not
4154
+ make the nominated friends members of the befriending class.
4155
+
4156
+ [*Example 1*:
4157
+
4158
+ The following example illustrates the differences between members and
4159
+ friends:
4160
+
4161
+ ``` cpp
4162
+ class X {
4163
+ int a;
4164
+ friend void friend_set(X*, int);
4165
+ public:
4166
+ void member_set(int);
4167
+ };
4168
+
4169
+ void friend_set(X* p, int i) { p->a = i; }
4170
+ void X::member_set(int i) { a = i; }
4171
+
4172
+ void f() {
4173
+ X obj;
4174
+ friend_set(&obj,10);
4175
+ obj.member_set(10);
4176
+ }
4177
+ ```
4178
+
4179
+ — *end example*]
4180
+
4181
+ Declaring a class to be a friend implies that the names of private and
4182
+ protected members from the class granting friendship can be accessed in
4183
+ the *base-specifier*s and member declarations of the befriended class.
4184
+
4185
+ [*Example 2*:
4186
+
4187
+ ``` cpp
4188
+ class A {
4189
+ class B { };
4190
+ friend class X;
4191
+ };
4192
+
4193
+ struct X : A::B { // OK: A::B accessible to friend
4194
+ A::B mx; // OK: A::B accessible to member of friend
4195
+ class Y {
4196
+ A::B my; // OK: A::B accessible to nested member of friend
4197
+ };
4198
+ };
4199
+ ```
4200
+
4201
+ — *end example*]
4202
+
4203
+ [*Example 3*:
4204
+
4205
+ ``` cpp
4206
+ class X {
4207
+ enum { a=100 };
4208
+ friend class Y;
4209
+ };
4210
+
4211
+ class Y {
4212
+ int v[X::a]; // OK, Y is a friend of X
4213
+ };
4214
+
4215
+ class Z {
4216
+ int v[X::a]; // error: X::a is private
4217
+ };
4218
+ ```
4219
+
4220
+ — *end example*]
4221
+
4222
+ A class shall not be defined in a friend declaration.
4223
+
4224
+ [*Example 4*:
4225
+
4226
+ ``` cpp
4227
+ class A {
4228
+ friend class B { }; // error: cannot define class in friend declaration
4229
+ };
4230
+ ```
4231
+
4232
+ — *end example*]
4233
+
4234
+ A friend declaration that does not declare a function shall have one of
4235
+ the following forms:
4236
+
4237
+ ``` bnf
4238
+ friend elaborated-type-specifier ';'
4239
+ friend simple-type-specifier ';'
4240
+ friend typename-specifier ';'
4241
+ ```
4242
+
4243
+ [*Note 1*: A friend declaration may be the *declaration* in a
4244
+ *template-declaration* ([[temp.pre]], [[temp.friend]]). — *end note*]
4245
+
4246
+ If the type specifier in a `friend` declaration designates a (possibly
4247
+ cv-qualified) class type, that class is declared as a friend; otherwise,
4248
+ the friend declaration is ignored.
4249
+
4250
+ [*Example 5*:
4251
+
4252
+ ``` cpp
4253
+ class C;
4254
+ typedef C Ct;
4255
+
4256
+ class X1 {
4257
+ friend C; // OK: class C is a friend
4258
+ };
4259
+
4260
+ class X2 {
4261
+ friend Ct; // OK: class C is a friend
4262
+ friend D; // error: no type-name D in scope
4263
+ friend class D; // OK: elaborated-type-specifier declares new class
4264
+ };
4265
+
4266
+ template <typename T> class R {
4267
+ friend T;
4268
+ };
4269
+
4270
+ R<C> rc; // class C is a friend of R<C>
4271
+ R<int> Ri; // OK: "friend int;" is ignored
4272
+ ```
4273
+
4274
+ — *end example*]
4275
+
4276
+ A function first declared in a friend declaration has the linkage of the
4277
+ namespace of which it is a member ([[basic.link]],
4278
+ [[namespace.memdef]]). Otherwise, the function retains its previous
4279
+ linkage [[dcl.stc]].
4280
+
4281
+ When a friend declaration refers to an overloaded name or operator, only
4282
+ the function specified by the parameter types becomes a friend. A member
4283
+ function of a class `X` can be a friend of a class `Y`.
4284
+
4285
+ [*Example 6*:
4286
+
4287
+ ``` cpp
4288
+ class Y {
4289
+ friend char* X::foo(int);
4290
+ friend X::X(char); // constructors can be friends
4291
+ friend X::~X(); // destructors can be friends
4292
+ };
4293
+ ```
4294
+
4295
+ — *end example*]
4296
+
4297
+ A function can be defined in a friend declaration of a class if and only
4298
+ if the class is a non-local class [[class.local]], the function name is
4299
+ unqualified, and the function has namespace scope.
4300
+
4301
+ [*Example 7*:
4302
+
4303
+ ``` cpp
4304
+ class M {
4305
+ friend void f() { } // definition of global f, a friend of M,
4306
+ // not the definition of a member function
4307
+ };
4308
+ ```
4309
+
4310
+ — *end example*]
4311
+
4312
+ Such a function is implicitly an inline [[dcl.inline]] function if it is
4313
+ attached to the global module. A friend function defined in a class is
4314
+ in the (lexical) scope of the class in which it is defined. A friend
4315
+ function defined outside the class is not [[basic.lookup.unqual]].
4316
+
4317
+ No *storage-class-specifier* shall appear in the *decl-specifier-seq* of
4318
+ a friend declaration.
4319
+
4320
+ A name nominated by a friend declaration shall be accessible in the
4321
+ scope of the class containing the friend declaration. The meaning of the
4322
+ friend declaration is the same whether the friend declaration appears in
4323
+ the private, protected, or public [[class.mem]] portion of the class
4324
+ *member-specification*.
4325
+
4326
+ Friendship is neither inherited nor transitive.
4327
+
4328
+ [*Example 8*:
4329
+
4330
+ ``` cpp
4331
+ class A {
4332
+ friend class B;
4333
+ int a;
4334
+ };
4335
+
4336
+ class B {
4337
+ friend class C;
4338
+ };
4339
+
4340
+ class C {
4341
+ void f(A* p) {
4342
+ p->a++; // error: C is not a friend of A despite being a friend of a friend
4343
+ }
4344
+ };
4345
+
4346
+ class D : public B {
4347
+ void f(A* p) {
4348
+ p->a++; // error: D is not a friend of A despite being derived from a friend
4349
+ }
4350
+ };
4351
+ ```
4352
+
4353
+ — *end example*]
4354
+
4355
+ If a friend declaration appears in a local class [[class.local]] and the
4356
+ name specified is an unqualified name, a prior declaration is looked up
4357
+ without considering scopes that are outside the innermost enclosing
4358
+ non-class scope. For a friend function declaration, if there is no prior
4359
+ declaration, the program is ill-formed. For a friend class declaration,
4360
+ if there is no prior declaration, the class that is specified belongs to
4361
+ the innermost enclosing non-class scope, but if it is subsequently
4362
+ referenced, its name is not found by name lookup until a matching
4363
+ declaration is provided in the innermost enclosing non-class scope.
4364
+
4365
+ [*Example 9*:
4366
+
4367
+ ``` cpp
4368
+ class X;
4369
+ void a();
4370
+ void f() {
4371
+ class Y;
4372
+ extern void b();
4373
+ class A {
4374
+ friend class X; // OK, but X is a local class, not ::X
4375
+ friend class Y; // OK
4376
+ friend class Z; // OK, introduces local class Z
4377
+ friend void a(); // error, ::a is not considered
4378
+ friend void b(); // OK
4379
+ friend void c(); // error
4380
+ };
4381
+ X* px; // OK, but ::X is found
4382
+ Z* pz; // error: no Z is found
4383
+ }
4384
+ ```
4385
+
4386
+ — *end example*]
4387
+
4388
+ ### Protected member access <a id="class.protected">[[class.protected]]</a>
4389
+
4390
+ An additional access check beyond those described earlier in
4391
+ [[class.access]] is applied when a non-static data member or non-static
4392
+ member function is a protected member of its naming class
4393
+ [[class.access.base]].[^13] As described earlier, access to a protected
4394
+ member is granted because the reference occurs in a friend or member of
4395
+ some class `C`. If the access is to form a pointer to member
4396
+ [[expr.unary.op]], the *nested-name-specifier* shall denote `C` or a
4397
+ class derived from `C`. All other accesses involve a (possibly implicit)
4398
+ object expression [[expr.ref]]. In this case, the class of the object
4399
+ expression shall be `C` or a class derived from `C`.
4400
+
4401
+ [*Example 1*:
4402
+
4403
+ ``` cpp
4404
+ class B {
4405
+ protected:
4406
+ int i;
4407
+ static int j;
4408
+ };
4409
+
4410
+ class D1 : public B {
4411
+ };
4412
+
4413
+ class D2 : public B {
4414
+ friend void fr(B*,D1*,D2*);
4415
+ void mem(B*,D1*);
4416
+ };
4417
+
4418
+ void fr(B* pb, D1* p1, D2* p2) {
4419
+ pb->i = 1; // error
4420
+ p1->i = 2; // error
4421
+ p2->i = 3; // OK (access through a D2)
4422
+ p2->B::i = 4; // OK (access through a D2, even though naming class is B)
4423
+ int B::* pmi_B = &B::i; // error
4424
+ int B::* pmi_B2 = &D2::i; // OK (type of &D2::i is int B::*)
4425
+ B::j = 5; // error: not a friend of naming class B
4426
+ D2::j = 6; // OK (because refers to static member)
4427
+ }
4428
+
4429
+ void D2::mem(B* pb, D1* p1) {
4430
+ pb->i = 1; // error
4431
+ p1->i = 2; // error
4432
+ i = 3; // OK (access through this)
4433
+ B::i = 4; // OK (access through this, qualification ignored)
4434
+ int B::* pmi_B = &B::i; // error
4435
+ int B::* pmi_B2 = &D2::i; // OK
4436
+ j = 5; // OK (because j refers to static member)
4437
+ B::j = 6; // OK (because B::j refers to static member)
4438
+ }
4439
+
4440
+ void g(B* pb, D1* p1, D2* p2) {
4441
+ pb->i = 1; // error
4442
+ p1->i = 2; // error
4443
+ p2->i = 3; // error
4444
+ }
4445
+ ```
4446
+
4447
+ — *end example*]
4448
+
4449
+ ### Access to virtual functions <a id="class.access.virt">[[class.access.virt]]</a>
4450
+
4451
+ The access rules [[class.access]] for a virtual function are determined
4452
+ by its declaration and are not affected by the rules for a function that
4453
+ later overrides it.
4454
+
4455
+ [*Example 1*:
4456
+
4457
+ ``` cpp
4458
+ class B {
4459
+ public:
4460
+ virtual int f();
4461
+ };
4462
+
4463
+ class D : public B {
4464
+ private:
4465
+ int f();
4466
+ };
4467
+
4468
+ void f() {
4469
+ D d;
4470
+ B* pb = &d;
4471
+ D* pd = &d;
4472
+
4473
+ pb->f(); // OK: B::f() is public, D::f() is invoked
4474
+ pd->f(); // error: D::f() is private
4475
+ }
4476
+ ```
4477
+
4478
+ — *end example*]
4479
+
4480
+ Access is checked at the call point using the type of the expression
4481
+ used to denote the object for which the member function is called (`B*`
4482
+ in the example above). The access of the member function in the class in
4483
+ which it was defined (`D` in the example above) is in general not known.
4484
+
4485
+ ### Multiple access <a id="class.paths">[[class.paths]]</a>
4486
+
4487
+ If a name can be reached by several paths through a multiple inheritance
4488
+ graph, the access is that of the path that gives most access.
4489
+
4490
+ [*Example 1*:
4491
+
4492
+ ``` cpp
4493
+ class W { public: void f(); };
4494
+ class A : private virtual W { };
4495
+ class B : public virtual W { };
4496
+ class C : public A, public B {
4497
+ void f() { W::f(); } // OK
4498
+ };
4499
+ ```
4500
+
4501
+ Since `W::f()` is available to `C::f()` along the public path through
4502
+ `B`, access is allowed.
4503
+
4504
+ — *end example*]
4505
+
4506
+ ### Nested classes <a id="class.access.nest">[[class.access.nest]]</a>
4507
+
4508
+ A nested class is a member and as such has the same access rights as any
4509
+ other member. The members of an enclosing class have no special access
4510
+ to members of a nested class; the usual access rules [[class.access]]
4511
+ shall be obeyed.
4512
+
4513
+ [*Example 1*:
4514
+
4515
+ ``` cpp
4516
+ class E {
4517
+ int x;
4518
+ class B { };
4519
+
4520
+ class I {
4521
+ B b; // OK: E::I can access E::B
4522
+ int y;
4523
+ void f(E* p, int i) {
4524
+ p->x = i; // OK: E::I can access E::x
4525
+ }
4526
+ };
4527
+
4528
+ int g(I* p) {
4529
+ return p->y; // error: I::y is private
4530
+ }
4531
+ };
4532
+ ```
4533
+
4534
+ — *end example*]
4535
+
4536
+ ## Initialization <a id="class.init">[[class.init]]</a>
4537
+
4538
+ When no initializer is specified for an object of (possibly
4539
+ cv-qualified) class type (or array thereof), or the initializer has the
4540
+ form `()`, the object is initialized as specified in  [[dcl.init]].
4541
+
4542
+ An object of class type (or array thereof) can be explicitly
4543
+ initialized; see  [[class.expl.init]] and  [[class.base.init]].
4544
+
4545
+ When an array of class objects is initialized (either explicitly or
4546
+ implicitly) and the elements are initialized by constructor, the
4547
+ constructor shall be called for each element of the array, following the
4548
+ subscript order; see  [[dcl.array]].
4549
+
4550
+ [*Note 1*: Destructors for the array elements are called in reverse
4551
+ order of their construction. — *end note*]
4552
+
4553
+ ### Explicit initialization <a id="class.expl.init">[[class.expl.init]]</a>
4554
+
4555
+ An object of class type can be initialized with a parenthesized
4556
+ *expression-list*, where the *expression-list* is construed as an
4557
+ argument list for a constructor that is called to initialize the object.
4558
+ Alternatively, a single *assignment-expression* can be specified as an
4559
+ *initializer* using the `=` form of initialization. Either
4560
+ direct-initialization semantics or copy-initialization semantics apply;
4561
+ see  [[dcl.init]].
4562
+
4563
+ [*Example 1*:
4564
+
4565
+ ``` cpp
4566
+ struct complex {
4567
+ complex();
4568
+ complex(double);
4569
+ complex(double,double);
4570
+ };
4571
+
4572
+ complex sqrt(complex,complex);
4573
+
4574
+ complex a(1); // initialized by calling complex(double) with argument 1
4575
+ complex b = a; // initialized as a copy of a
4576
+ complex c = complex(1,2); // initialized by calling complex(double,double) with arguments 1 and 2
4577
+ complex d = sqrt(b,c); // initialized by calling sqrt(complex,complex) with d as its result object
4578
+ complex e; // initialized by calling complex()
4579
+ complex f = 3; // initialized by calling complex(double) with argument 3
4580
+ complex g = { 1, 2 }; // initialized by calling complex(double, double) with arguments 1 and 2
4581
+ ```
4582
+
4583
+ — *end example*]
4584
+
4585
+ [*Note 1*: Overloading of the assignment operator [[over.ass]] has no
4586
+ effect on initialization. — *end note*]
4587
+
4588
+ An object of class type can also be initialized by a *braced-init-list*.
4589
+ List-initialization semantics apply; see  [[dcl.init]] and 
4590
+ [[dcl.init.list]].
4591
+
4592
+ [*Example 2*:
4593
+
4594
+ ``` cpp
4595
+ complex v[6] = { 1, complex(1,2), complex(), 2 };
4596
+ ```
4597
+
4598
+ Here, `complex::complex(double)` is called for the initialization of
4599
+ `v[0]` and `v[3]`, `complex::complex({}double, double)` is called for
4600
+ the initialization of `v[1]`, `complex::complex()` is called for the
4601
+ initialization `v[2]`, `v[4]`, and `v[5]`. For another example,
4602
+
4603
+ ``` cpp
4604
+ struct X {
4605
+ int i;
4606
+ float f;
4607
+ complex c;
4608
+ } x = { 99, 88.8, 77.7 };
4609
+ ```
4610
+
4611
+ Here, `x.i` is initialized with 99, `x.f` is initialized with 88.8, and
4612
+ `complex::complex(double)` is called for the initialization of `x.c`.
4613
+
4614
+ — *end example*]
4615
+
4616
+ [*Note 2*: Braces can be elided in the *initializer-list* for any
4617
+ aggregate, even if the aggregate has members of a class type with
4618
+ user-defined type conversions; see  [[dcl.init.aggr]]. — *end note*]
4619
+
4620
+ [*Note 3*: If `T` is a class type with no default constructor, any
4621
+ declaration of an object of type `T` (or array thereof) is ill-formed if
4622
+ no *initializer* is explicitly specified (see  [[class.init]] and 
4623
+ [[dcl.init]]). — *end note*]
4624
+
4625
+ [*Note 4*: The order in which objects with static or thread storage
4626
+ duration are initialized is described in  [[basic.start.dynamic]] and 
4627
+ [[stmt.dcl]]. — *end note*]
4628
+
4629
+ ### Initializing bases and members <a id="class.base.init">[[class.base.init]]</a>
4630
+
4631
+ In the definition of a constructor for a class, initializers for direct
4632
+ and virtual base class subobjects and non-static data members can be
4633
+ specified by a *ctor-initializer*, which has the form
4634
+
4635
+ ``` bnf
4636
+ ctor-initializer:
4637
+ ':' mem-initializer-list
4638
+ ```
4639
+
4640
+ ``` bnf
4641
+ mem-initializer-list:
4642
+ mem-initializer '...'ₒₚₜ
4643
+ mem-initializer-list ',' mem-initializer '...'ₒₚₜ
4644
+ ```
4645
+
4646
+ ``` bnf
4647
+ mem-initializer:
4648
+ mem-initializer-id '(' expression-listₒₚₜ ')'
4649
+ mem-initializer-id braced-init-list
4650
+ ```
4651
+
4652
+ ``` bnf
4653
+ mem-initializer-id:
4654
+ class-or-decltype
4655
+ identifier
4656
+ ```
4657
+
4658
+ In a *mem-initializer-id* an initial unqualified *identifier* is looked
4659
+ up in the scope of the constructor’s class and, if not found in that
4660
+ scope, it is looked up in the scope containing the constructor’s
4661
+ definition.
4662
+
4663
+ [*Note 1*: If the constructor’s class contains a member with the same
4664
+ name as a direct or virtual base class of the class, a
4665
+ *mem-initializer-id* naming the member or base class and composed of a
4666
+ single identifier refers to the class member. A *mem-initializer-id* for
4667
+ the hidden base class may be specified using a qualified
4668
+ name. — *end note*]
4669
+
4670
+ Unless the *mem-initializer-id* names the constructor’s class, a
4671
+ non-static data member of the constructor’s class, or a direct or
4672
+ virtual base of that class, the *mem-initializer* is ill-formed.
4673
+
4674
+ A *mem-initializer-list* can initialize a base class using any
4675
+ *class-or-decltype* that denotes that base class type.
4676
+
4677
+ [*Example 1*:
4678
+
4679
+ ``` cpp
4680
+ struct A { A(); };
4681
+ typedef A global_A;
4682
+ struct B { };
4683
+ struct C: public A, public B { C(); };
4684
+ C::C(): global_A() { } // mem-initializer for base A
4685
+ ```
4686
+
4687
+ — *end example*]
4688
+
4689
+ If a *mem-initializer-id* is ambiguous because it designates both a
4690
+ direct non-virtual base class and an inherited virtual base class, the
4691
+ *mem-initializer* is ill-formed.
4692
+
4693
+ [*Example 2*:
4694
+
4695
+ ``` cpp
4696
+ struct A { A(); };
4697
+ struct B: public virtual A { };
4698
+ struct C: public A, public B { C(); };
4699
+ C::C(): A() { } // error: which A?
4700
+ ```
4701
+
4702
+ — *end example*]
4703
+
4704
+ A *ctor-initializer* may initialize a variant member of the
4705
+ constructor’s class. If a *ctor-initializer* specifies more than one
4706
+ *mem-initializer* for the same member or for the same base class, the
4707
+ *ctor-initializer* is ill-formed.
4708
+
4709
+ A *mem-initializer-list* can delegate to another constructor of the
4710
+ constructor’s class using any *class-or-decltype* that denotes the
4711
+ constructor’s class itself. If a *mem-initializer-id* designates the
4712
+ constructor’s class, it shall be the only *mem-initializer*; the
4713
+ constructor is a *delegating constructor*, and the constructor selected
4714
+ by the *mem-initializer* is the *target constructor*. The target
4715
+ constructor is selected by overload resolution. Once the target
4716
+ constructor returns, the body of the delegating constructor is executed.
4717
+ If a constructor delegates to itself directly or indirectly, the program
4718
+ is ill-formed, no diagnostic required.
4719
+
4720
+ [*Example 3*:
4721
+
4722
+ ``` cpp
4723
+ struct C {
4724
+ C( int ) { } // #1: non-delegating constructor
4725
+ C(): C(42) { } // #2: delegates to #1
4726
+ C( char c ) : C(42.0) { } // #3: ill-formed due to recursion with #4
4727
+ C( double d ) : C('a') { } // #4: ill-formed due to recursion with #3
4728
+ };
4729
+ ```
4730
+
4731
+ — *end example*]
4732
+
4733
+ The *expression-list* or *braced-init-list* in a *mem-initializer* is
4734
+ used to initialize the designated subobject (or, in the case of a
4735
+ delegating constructor, the complete class object) according to the
4736
+ initialization rules of  [[dcl.init]] for direct-initialization.
4737
+
4738
+ [*Example 4*:
4739
+
4740
+ ``` cpp
4741
+ struct B1 { B1(int); ... };
4742
+ struct B2 { B2(int); ... };
4743
+ struct D : B1, B2 {
4744
+ D(int);
4745
+ B1 b;
4746
+ const int c;
4747
+ };
4748
+
4749
+ D::D(int a) : B2(a+1), B1(a+2), c(a+3), b(a+4) { ... }
4750
+ D d(10);
4751
+ ```
4752
+
4753
+ — *end example*]
4754
+
4755
+ [*Note 2*: The initialization performed by each *mem-initializer*
4756
+ constitutes a full-expression [[intro.execution]]. Any expression in a
4757
+ *mem-initializer* is evaluated as part of the full-expression that
4758
+ performs the initialization. — *end note*]
4759
+
4760
+ A *mem-initializer* where the *mem-initializer-id* denotes a virtual
4761
+ base class is ignored during execution of a constructor of any class
4762
+ that is not the most derived class.
4763
+
4764
+ A temporary expression bound to a reference member in a
4765
+ *mem-initializer* is ill-formed.
4766
+
4767
+ [*Example 5*:
4768
+
4769
+ ``` cpp
4770
+ struct A {
4771
+ A() : v(42) { } // error
4772
+ const int& v;
4773
+ };
4774
+ ```
4775
+
4776
+ — *end example*]
4777
+
4778
+ In a non-delegating constructor, if a given potentially constructed
4779
+ subobject is not designated by a *mem-initializer-id* (including the
4780
+ case where there is no *mem-initializer-list* because the constructor
4781
+ has no *ctor-initializer*), then
4782
+
4783
+ - if the entity is a non-static data member that has a default member
4784
+ initializer [[class.mem]] and either
4785
+ - the constructor’s class is a union [[class.union]], and no other
4786
+ variant member of that union is designated by a *mem-initializer-id*
4787
+ or
4788
+ - the constructor’s class is not a union, and, if the entity is a
4789
+ member of an anonymous union, no other member of that union is
4790
+ designated by a *mem-initializer-id*,
4791
+
4792
+ the entity is initialized from its default member initializer as
4793
+ specified in  [[dcl.init]];
4794
+ - otherwise, if the entity is an anonymous union or a variant member
4795
+ [[class.union.anon]], no initialization is performed;
4796
+ - otherwise, the entity is default-initialized [[dcl.init]].
4797
+
4798
+ [*Note 3*: An abstract class [[class.abstract]] is never a most derived
4799
+ class, thus its constructors never initialize virtual base classes,
4800
+ therefore the corresponding *mem-initializer*s may be
4801
+ omitted. — *end note*]
4802
+
4803
+ An attempt to initialize more than one non-static data member of a union
4804
+ renders the program ill-formed.
4805
+
4806
+ [*Note 4*: After the call to a constructor for class `X` for an object
4807
+ with automatic or dynamic storage duration has completed, if the
4808
+ constructor was not invoked as part of value-initialization and a member
4809
+ of `X` is neither initialized nor given a value during execution of the
4810
+ *compound-statement* of the body of the constructor, the member has an
4811
+ indeterminate value. — *end note*]
4812
+
4813
+ [*Example 6*:
4814
+
4815
+ ``` cpp
4816
+ struct A {
4817
+ A();
4818
+ };
4819
+
4820
+ struct B {
4821
+ B(int);
4822
+ };
4823
+
4824
+ struct C {
4825
+ C() { } // initializes members as follows:
4826
+ A a; // OK: calls A::A()
4827
+ const B b; // error: B has no default constructor
4828
+ int i; // OK: i has indeterminate value
4829
+ int j = 5; // OK: j has the value 5
4830
+ };
4831
+ ```
4832
+
4833
+ — *end example*]
4834
+
4835
+ If a given non-static data member has both a default member initializer
4836
+ and a *mem-initializer*, the initialization specified by the
4837
+ *mem-initializer* is performed, and the non-static data member’s default
4838
+ member initializer is ignored.
4839
+
4840
+ [*Example 7*:
4841
+
4842
+ Given
4843
+
4844
+ ``` cpp
4845
+ struct A {
4846
+ int i = /* some integer expression with side effects */ ;
4847
+ A(int arg) : i(arg) { }
4848
+ // ...
4849
+ };
4850
+ ```
4851
+
4852
+ the `A(int)` constructor will simply initialize `i` to the value of
4853
+ `arg`, and the side effects in `i`’s default member initializer will not
4854
+ take place.
4855
+
4856
+ — *end example*]
4857
+
4858
+ A temporary expression bound to a reference member from a default member
4859
+ initializer is ill-formed.
4860
+
4861
+ [*Example 8*:
4862
+
4863
+ ``` cpp
4864
+ struct A {
4865
+ A() = default; // OK
4866
+ A(int v) : v(v) { } // OK
4867
+ const int& v = 42; // OK
4868
+ };
4869
+ A a1; // error: ill-formed binding of temporary to reference
4870
+ A a2(1); // OK, unfortunately
4871
+ ```
4872
+
4873
+ — *end example*]
4874
+
4875
+ In a non-delegating constructor, the destructor for each potentially
4876
+ constructed subobject of class type is potentially invoked
4877
+ [[class.dtor]].
4878
+
4879
+ [*Note 5*: This provision ensures that destructors can be called for
4880
+ fully-constructed subobjects in case an exception is thrown
4881
+ [[except.ctor]]. — *end note*]
4882
+
4883
+ In a non-delegating constructor, initialization proceeds in the
4884
+ following order:
4885
+
4886
+ - First, and only for the constructor of the most derived class
4887
+ [[intro.object]], virtual base classes are initialized in the order
4888
+ they appear on a depth-first left-to-right traversal of the directed
4889
+ acyclic graph of base classes, where “left-to-right” is the order of
4890
+ appearance of the base classes in the derived class
4891
+ *base-specifier-list*.
4892
+ - Then, direct base classes are initialized in declaration order as they
4893
+ appear in the *base-specifier-list* (regardless of the order of the
4894
+ *mem-initializer*s).
4895
+ - Then, non-static data members are initialized in the order they were
4896
+ declared in the class definition (again regardless of the order of the
4897
+ *mem-initializer*s).
4898
+ - Finally, the *compound-statement* of the constructor body is executed.
4899
+
4900
+ [*Note 6*: The declaration order is mandated to ensure that base and
4901
+ member subobjects are destroyed in the reverse order of
4902
+ initialization. — *end note*]
4903
+
4904
+ [*Example 9*:
4905
+
4906
+ ``` cpp
4907
+ struct V {
4908
+ V();
4909
+ V(int);
4910
+ };
4911
+
4912
+ struct A : virtual V {
4913
+ A();
4914
+ A(int);
4915
+ };
4916
+
4917
+ struct B : virtual V {
4918
+ B();
4919
+ B(int);
4920
+ };
4921
+
4922
+ struct C : A, B, virtual V {
4923
+ C();
4924
+ C(int);
4925
+ };
4926
+
4927
+ A::A(int i) : V(i) { ... }
4928
+ B::B(int i) { ... }
4929
+ C::C(int i) { ... }
4930
+
4931
+ V v(1); // use V(int)
4932
+ A a(2); // use V(int)
4933
+ B b(3); // use V()
4934
+ C c(4); // use V()
4935
+ ```
4936
+
4937
+ — *end example*]
4938
+
4939
+ Names in the *expression-list* or *braced-init-list* of a
4940
+ *mem-initializer* are evaluated in the scope of the constructor for
4941
+ which the *mem-initializer* is specified.
4942
+
4943
+ [*Example 10*:
4944
+
4945
+ ``` cpp
4946
+ class X {
4947
+ int a;
4948
+ int b;
4949
+ int i;
4950
+ int j;
4951
+ public:
4952
+ const int& r;
4953
+ X(int i): r(a), b(i), i(i), j(this->i) { }
4954
+ };
4955
+ ```
4956
+
4957
+ initializes `X::r` to refer to `X::a`, initializes `X::b` with the value
4958
+ of the constructor parameter `i`, initializes `X::i` with the value of
4959
+ the constructor parameter `i`, and initializes `X::j` with the value of
4960
+ `X::i`; this takes place each time an object of class `X` is created.
4961
+
4962
+ — *end example*]
4963
+
4964
+ [*Note 7*: Because the *mem-initializer* are evaluated in the scope of
4965
+ the constructor, the `this` pointer can be used in the *expression-list*
4966
+ of a *mem-initializer* to refer to the object being
4967
+ initialized. — *end note*]
4968
+
4969
+ Member functions (including virtual member functions, [[class.virtual]])
4970
+ can be called for an object under construction. Similarly, an object
4971
+ under construction can be the operand of the `typeid` operator
4972
+ [[expr.typeid]] or of a `dynamic_cast` [[expr.dynamic.cast]]. However,
4973
+ if these operations are performed in a *ctor-initializer* (or in a
4974
+ function called directly or indirectly from a *ctor-initializer*) before
4975
+ all the *mem-initializer*s for base classes have completed, the program
4976
+ has undefined behavior.
4977
+
4978
+ [*Example 11*:
4979
+
4980
+ ``` cpp
4981
+ class A {
4982
+ public:
4983
+ A(int);
4984
+ };
4985
+
4986
+ class B : public A {
4987
+ int j;
4988
+ public:
4989
+ int f();
4990
+ B() : A(f()), // undefined behavior: calls member function but base A not yet initialized
4991
+ j(f()) { } // well-defined: bases are all initialized
4992
+ };
4993
+
4994
+ class C {
4995
+ public:
4996
+ C(int);
4997
+ };
4998
+
4999
+ class D : public B, C {
5000
+ int i;
5001
+ public:
5002
+ D() : C(f()), // undefined behavior: calls member function but base C not yet initialized
5003
+ i(f()) { } // well-defined: bases are all initialized
5004
+ };
5005
+ ```
5006
+
5007
+ — *end example*]
5008
+
5009
+ [*Note 8*: [[class.cdtor]] describes the result of virtual function
5010
+ calls, `typeid` and `dynamic_cast`s during construction for the
5011
+ well-defined cases; that is, describes the polymorphic behavior of an
5012
+ object under construction. — *end note*]
5013
+
5014
+ A *mem-initializer* followed by an ellipsis is a pack expansion
5015
+ [[temp.variadic]] that initializes the base classes specified by a pack
5016
+ expansion in the *base-specifier-list* for the class.
5017
+
5018
+ [*Example 12*:
5019
+
5020
+ ``` cpp
5021
+ template<class... Mixins>
5022
+ class X : public Mixins... {
5023
+ public:
5024
+ X(const Mixins&... mixins) : Mixins(mixins)... { }
5025
+ };
5026
+ ```
5027
+
5028
+ — *end example*]
5029
+
5030
+ ### Initialization by inherited constructor <a id="class.inhctor.init">[[class.inhctor.init]]</a>
5031
+
5032
+ When a constructor for type `B` is invoked to initialize an object of a
5033
+ different type `D` (that is, when the constructor was inherited
5034
+ [[namespace.udecl]]), initialization proceeds as if a defaulted default
5035
+ constructor were used to initialize the `D` object and each base class
5036
+ subobject from which the constructor was inherited, except that the `B`
5037
+ subobject is initialized by the invocation of the inherited constructor.
5038
+ The complete initialization is considered to be a single function call;
5039
+ in particular, the initialization of the inherited constructor’s
5040
+ parameters is sequenced before the initialization of any part of the `D`
5041
+ object.
5042
+
5043
+ [*Example 1*:
5044
+
5045
+ ``` cpp
5046
+ struct B1 {
5047
+ B1(int, ...) { }
5048
+ };
5049
+
5050
+ struct B2 {
5051
+ B2(double) { }
5052
+ };
5053
+
5054
+ int get();
5055
+
5056
+ struct D1 : B1 {
5057
+ using B1::B1; // inherits B1(int, ...)
5058
+ int x;
5059
+ int y = get();
5060
+ };
5061
+
5062
+ void test() {
5063
+ D1 d(2, 3, 4); // OK: B1 is initialized by calling B1(2, 3, 4),
5064
+ // then d.x is default-initialized (no initialization is performed),
5065
+ // then d.y is initialized by calling get()
5066
+ D1 e; // error: D1 has a deleted default constructor
5067
+ }
5068
+
5069
+ struct D2 : B2 {
5070
+ using B2::B2;
5071
+ B1 b;
5072
+ };
5073
+
5074
+ D2 f(1.0); // error: B1 has a deleted default constructor
5075
+
5076
+ struct W { W(int); };
5077
+ struct X : virtual W { using W::W; X() = delete; };
5078
+ struct Y : X { using X::X; };
5079
+ struct Z : Y, virtual W { using Y::Y; };
5080
+ Z z(0); // OK: initialization of Y does not invoke default constructor of X
5081
+
5082
+ template<class T> struct Log : T {
5083
+ using T::T; // inherits all constructors from class T
5084
+ ~Log() { std::clog << "Destroying wrapper" << std::endl; }
5085
+ };
5086
+ ```
5087
+
5088
+ Class template `Log` wraps any class and forwards all of its
5089
+ constructors, while writing a message to the standard log whenever an
5090
+ object of class `Log` is destroyed.
5091
+
5092
+ — *end example*]
5093
+
5094
+ If the constructor was inherited from multiple base class subobjects of
5095
+ type `B`, the program is ill-formed.
5096
+
5097
+ [*Example 2*:
5098
+
5099
+ ``` cpp
5100
+ struct A { A(int); };
5101
+ struct B : A { using A::A; };
5102
+
5103
+ struct C1 : B { using B::B; };
5104
+ struct C2 : B { using B::B; };
5105
+
5106
+ struct D1 : C1, C2 {
5107
+ using C1::C1;
5108
+ using C2::C2;
5109
+ };
5110
+
5111
+ struct V1 : virtual B { using B::B; };
5112
+ struct V2 : virtual B { using B::B; };
5113
+
5114
+ struct D2 : V1, V2 {
5115
+ using V1::V1;
5116
+ using V2::V2;
5117
+ };
5118
+
5119
+ D1 d1(0); // error: ambiguous
5120
+ D2 d2(0); // OK: initializes virtual B base class, which initializes the A base class
5121
+ // then initializes the V1 and V2 base classes as if by a defaulted default constructor
5122
+
5123
+ struct M { M(); M(int); };
5124
+ struct N : M { using M::M; };
5125
+ struct O : M {};
5126
+ struct P : N, O { using N::N; using O::O; };
5127
+ P p(0); // OK: use M(0) to initialize N's base class,
5128
+ // use M() to initialize O's base class
5129
+ ```
5130
+
5131
+ — *end example*]
5132
+
5133
+ When an object is initialized by an inherited constructor,
5134
+ initialization of the object is complete when the initialization of all
5135
+ subobjects is complete.
5136
+
5137
+ ### Construction and destruction <a id="class.cdtor">[[class.cdtor]]</a>
5138
+
5139
+ For an object with a non-trivial constructor, referring to any
5140
+ non-static member or base class of the object before the constructor
5141
+ begins execution results in undefined behavior. For an object with a
5142
+ non-trivial destructor, referring to any non-static member or base class
5143
+ of the object after the destructor finishes execution results in
5144
+ undefined behavior.
5145
+
5146
+ [*Example 1*:
5147
+
5148
+ ``` cpp
5149
+ struct X { int i; };
5150
+ struct Y : X { Y(); }; // non-trivial
5151
+ struct A { int a; };
5152
+ struct B : public A { int j; Y y; }; // non-trivial
5153
+
5154
+ extern B bobj;
5155
+ B* pb = &bobj; // OK
5156
+ int* p1 = &bobj.a; // undefined behavior: refers to base class member
5157
+ int* p2 = &bobj.y.i; // undefined behavior: refers to member's member
5158
+
5159
+ A* pa = &bobj; // undefined behavior: upcast to a base class type
5160
+ B bobj; // definition of bobj
5161
+
5162
+ extern X xobj;
5163
+ int* p3 = &xobj.i; // OK, X is a trivial class
5164
+ X xobj;
5165
+ ```
5166
+
5167
+ For another example,
5168
+
5169
+ ``` cpp
5170
+ struct W { int j; };
5171
+ struct X : public virtual W { };
5172
+ struct Y {
5173
+ int* p;
5174
+ X x;
5175
+ Y() : p(&x.j) { // undefined, x is not yet constructed
5176
+ }
5177
+ };
5178
+ ```
5179
+
5180
+ — *end example*]
5181
+
5182
+ During the construction of an object, if the value of the object or any
5183
+ of its subobjects is accessed through a glvalue that is not obtained,
5184
+ directly or indirectly, from the constructor’s `this` pointer, the value
5185
+ of the object or subobject thus obtained is unspecified.
5186
+
5187
+ [*Example 2*:
5188
+
5189
+ ``` cpp
5190
+ struct C;
5191
+ void no_opt(C*);
5192
+
5193
+ struct C {
5194
+ int c;
5195
+ C() : c(0) { no_opt(this); }
5196
+ };
5197
+
5198
+ const C cobj;
5199
+
5200
+ void no_opt(C* cptr) {
5201
+ int i = cobj.c * 100; // value of cobj.c is unspecified
5202
+ cptr->c = 1;
5203
+ cout << cobj.c * 100 // value of cobj.c is unspecified
5204
+ << '\n';
5205
+ }
5206
+
5207
+ extern struct D d;
5208
+ struct D {
5209
+ D(int a) : a(a), b(d.a) {}
5210
+ int a, b;
5211
+ };
5212
+ D d = D(1); // value of d.b is unspecified
5213
+ ```
5214
+
5215
+ — *end example*]
5216
+
5217
+ To explicitly or implicitly convert a pointer (a glvalue) referring to
5218
+ an object of class `X` to a pointer (reference) to a direct or indirect
5219
+ base class `B` of `X`, the construction of `X` and the construction of
5220
+ all of its direct or indirect bases that directly or indirectly derive
5221
+ from `B` shall have started and the destruction of these classes shall
5222
+ not have completed, otherwise the conversion results in undefined
5223
+ behavior. To form a pointer to (or access the value of) a direct
5224
+ non-static member of an object `obj`, the construction of `obj` shall
5225
+ have started and its destruction shall not have completed, otherwise the
5226
+ computation of the pointer value (or accessing the member value) results
5227
+ in undefined behavior.
5228
+
5229
+ [*Example 3*:
5230
+
5231
+ ``` cpp
5232
+ struct A { };
5233
+ struct B : virtual A { };
5234
+ struct C : B { };
5235
+ struct D : virtual A { D(A*); };
5236
+ struct X { X(A*); };
5237
+
5238
+ struct E : C, D, X {
5239
+ E() : D(this), // undefined behavior: upcast from E* to A* might use path E* → D* → A*
5240
+ // but D is not constructed
5241
+
5242
+ // ``D((C*)this)'' would be defined: E* → C* is defined because E() has started,
5243
+ // and C* → A* is defined because C is fully constructed
5244
+
5245
+ X(this) {} // defined: upon construction of X, C/B/D/A sublattice is fully constructed
5246
+ };
5247
+ ```
5248
+
5249
+ — *end example*]
5250
+
5251
+ Member functions, including virtual functions [[class.virtual]], can be
5252
+ called during construction or destruction [[class.base.init]]. When a
5253
+ virtual function is called directly or indirectly from a constructor or
5254
+ from a destructor, including during the construction or destruction of
5255
+ the class’s non-static data members, and the object to which the call
5256
+ applies is the object (call it `x`) under construction or destruction,
5257
+ the function called is the final overrider in the constructor’s or
5258
+ destructor’s class and not one overriding it in a more-derived class. If
5259
+ the virtual function call uses an explicit class member access
5260
+ [[expr.ref]] and the object expression refers to the complete object of
5261
+ `x` or one of that object’s base class subobjects but not `x` or one of
5262
+ its base class subobjects, the behavior is undefined.
5263
+
5264
+ [*Example 4*:
5265
+
5266
+ ``` cpp
5267
+ struct V {
5268
+ virtual void f();
5269
+ virtual void g();
5270
+ };
5271
+
5272
+ struct A : virtual V {
5273
+ virtual void f();
5274
+ };
5275
+
5276
+ struct B : virtual V {
5277
+ virtual void g();
5278
+ B(V*, A*);
5279
+ };
5280
+
5281
+ struct D : A, B {
5282
+ virtual void f();
5283
+ virtual void g();
5284
+ D() : B((A*)this, this) { }
5285
+ };
5286
+
5287
+ B::B(V* v, A* a) {
5288
+ f(); // calls V::f, not A::f
5289
+ g(); // calls B::g, not D::g
5290
+ v->g(); // v is base of B, the call is well-defined, calls B::g
5291
+ a->f(); // undefined behavior: a's type not a base of B
5292
+ }
5293
+ ```
5294
+
5295
+ — *end example*]
5296
+
5297
+ The `typeid` operator [[expr.typeid]] can be used during construction or
5298
+ destruction [[class.base.init]]. When `typeid` is used in a constructor
5299
+ (including the *mem-initializer* or default member initializer
5300
+ [[class.mem]] for a non-static data member) or in a destructor, or used
5301
+ in a function called (directly or indirectly) from a constructor or
5302
+ destructor, if the operand of `typeid` refers to the object under
5303
+ construction or destruction, `typeid` yields the `std::type_info` object
5304
+ representing the constructor or destructor’s class. If the operand of
5305
+ `typeid` refers to the object under construction or destruction and the
5306
+ static type of the operand is neither the constructor or destructor’s
5307
+ class nor one of its bases, the behavior is undefined.
5308
+
5309
+ `dynamic_cast`s [[expr.dynamic.cast]] can be used during construction or
5310
+ destruction [[class.base.init]]. When a `dynamic_cast` is used in a
5311
+ constructor (including the *mem-initializer* or default member
5312
+ initializer for a non-static data member) or in a destructor, or used in
5313
+ a function called (directly or indirectly) from a constructor or
5314
+ destructor, if the operand of the `dynamic_cast` refers to the object
5315
+ under construction or destruction, this object is considered to be a
5316
+ most derived object that has the type of the constructor or destructor’s
5317
+ class. If the operand of the `dynamic_cast` refers to the object under
5318
+ construction or destruction and the static type of the operand is not a
5319
+ pointer to or object of the constructor or destructor’s own class or one
5320
+ of its bases, the `dynamic_cast` results in undefined behavior.
5321
+
5322
+ [*Example 5*:
5323
+
5324
+ ``` cpp
5325
+ struct V {
5326
+ virtual void f();
5327
+ };
5328
+
5329
+ struct A : virtual V { };
5330
+
5331
+ struct B : virtual V {
5332
+ B(V*, A*);
5333
+ };
5334
+
5335
+ struct D : A, B {
5336
+ D() : B((A*)this, this) { }
5337
+ };
5338
+
5339
+ B::B(V* v, A* a) {
5340
+ typeid(*this); // type_info for B
5341
+ typeid(*v); // well-defined: *v has type V, a base of B yields type_info for B
5342
+ typeid(*a); // undefined behavior: type A not a base of B
5343
+ dynamic_cast<B*>(v); // well-defined: v of type V*, V base of B results in B*
5344
+ dynamic_cast<B*>(a); // undefined behavior: a has type A*, A not a base of B
5345
+ }
5346
+ ```
5347
+
5348
+ — *end example*]
5349
+
5350
+ ### Copy/move elision <a id="class.copy.elision">[[class.copy.elision]]</a>
5351
+
5352
+ When certain criteria are met, an implementation is allowed to omit the
5353
+ copy/move construction of a class object, even if the constructor
5354
+ selected for the copy/move operation and/or the destructor for the
5355
+ object have side effects. In such cases, the implementation treats the
5356
+ source and target of the omitted copy/move operation as simply two
5357
+ different ways of referring to the same object. If the first parameter
5358
+ of the selected constructor is an rvalue reference to the object’s type,
5359
+ the destruction of that object occurs when the target would have been
5360
+ destroyed; otherwise, the destruction occurs at the later of the times
5361
+ when the two objects would have been destroyed without the
5362
+ optimization.[^14] This elision of copy/move operations, called *copy
5363
+ elision*, is permitted in the following circumstances (which may be
5364
+ combined to eliminate multiple copies):
5365
+
5366
+ - in a `return` statement in a function with a class return type, when
5367
+ the *expression* is the name of a non-volatile object with automatic
5368
+ storage duration (other than a function parameter or a variable
5369
+ introduced by the *exception-declaration* of a *handler*
5370
+ [[except.handle]]) with the same type (ignoring cv-qualification) as
5371
+ the function return type, the copy/move operation can be omitted by
5372
+ constructing the object directly into the function call’s return
5373
+ object
5374
+ - in a *throw-expression* [[expr.throw]], when the operand is the name
5375
+ of a non-volatile object with automatic storage duration (other than a
5376
+ function or catch-clause parameter) whose scope does not extend beyond
5377
+ the end of the innermost enclosing *try-block* (if there is one), the
5378
+ copy/move operation can be omitted by constructing the object directly
5379
+ into the exception object
5380
+ - in a coroutine [[dcl.fct.def.coroutine]], a copy of a coroutine
5381
+ parameter can be omitted and references to that copy replaced with
5382
+ references to the corresponding parameter if the meaning of the
5383
+ program will be unchanged except for the execution of a constructor
5384
+ and destructor for the parameter copy object
5385
+ - when the *exception-declaration* of an exception handler
5386
+ [[except.pre]] declares an object of the same type (except for
5387
+ cv-qualification) as the exception object [[except.throw]], the copy
5388
+ operation can be omitted by treating the *exception-declaration* as an
5389
+ alias for the exception object if the meaning of the program will be
5390
+ unchanged except for the execution of constructors and destructors for
5391
+ the object declared by the *exception-declaration*. \[*Note 1*: There
5392
+ cannot be a move from the exception object because it is always an
5393
+ lvalue. — *end note*]
5394
+
5395
+ Copy elision is not permitted where an expression is evaluated in a
5396
+ context requiring a constant expression [[expr.const]] and in constant
5397
+ initialization [[basic.start.static]].
5398
+
5399
+ [*Note 2*: Copy elision might be performed if the same expression is
5400
+ evaluated in another context. — *end note*]
5401
+
5402
+ [*Example 1*:
5403
+
5404
+ ``` cpp
5405
+ class Thing {
5406
+ public:
5407
+ Thing();
5408
+ ~Thing();
5409
+ Thing(const Thing&);
5410
+ };
5411
+
5412
+ Thing f() {
5413
+ Thing t;
5414
+ return t;
5415
+ }
5416
+
5417
+ Thing t2 = f();
5418
+
5419
+ struct A {
5420
+ void *p;
5421
+ constexpr A(): p(this) {}
5422
+ };
5423
+
5424
+ constexpr A g() {
5425
+ A loc;
5426
+ return loc;
5427
+ }
5428
+
5429
+ constexpr A a; // well-formed, a.p points to a
5430
+ constexpr A b = g(); // error: b.p would be dangling[expr.const]
5431
+
5432
+ void h() {
5433
+ A c = g(); // well-formed, c.p may point to c or to an ephemeral temporary
5434
+ }
5435
+ ```
5436
+
5437
+ Here the criteria for elision can eliminate the copying of the object
5438
+ `t` with automatic storage duration into the result object for the
5439
+ function call `f()`, which is the global object `t2`. Effectively, the
5440
+ construction of the local object `t` can be viewed as directly
5441
+ initializing the global object `t2`, and that object’s destruction will
5442
+ occur at program exit. Adding a move constructor to `Thing` has the same
5443
+ effect, but it is the move construction from the object with automatic
5444
+ storage duration to `t2` that is elided.
5445
+
5446
+ — *end example*]
5447
+
5448
+ An *implicitly movable entity* is a variable of automatic storage
5449
+ duration that is either a non-volatile object or an rvalue reference to
5450
+ a non-volatile object type. In the following copy-initialization
5451
+ contexts, a move operation might be used instead of a copy operation:
5452
+
5453
+ - If the *expression* in a `return` [[stmt.return]] or `co_return`
5454
+ [[stmt.return.coroutine]] statement is a (possibly parenthesized)
5455
+ *id-expression* that names an implicitly movable entity declared in
5456
+ the body or *parameter-declaration-clause* of the innermost enclosing
5457
+ function or *lambda-expression*, or
5458
+ - if the operand of a *throw-expression* [[expr.throw]] is a (possibly
5459
+ parenthesized) *id-expression* that names an implicitly movable entity
5460
+ whose scope does not extend beyond the *compound-statement* of the
5461
+ innermost *try-block* or *function-try-block* (if any) whose
5462
+ *compound-statement* or *ctor-initializer* encloses the
5463
+ *throw-expression*,
5464
+
5465
+ overload resolution to select the constructor for the copy or the
5466
+ `return_value` overload to call is first performed as if the expression
5467
+ or operand were an rvalue. If the first overload resolution fails or was
5468
+ not performed, overload resolution is performed again, considering the
5469
+ expression or operand as an lvalue.
5470
+
5471
+ [*Note 3*: This two-stage overload resolution must be performed
5472
+ regardless of whether copy elision will occur. It determines the
5473
+ constructor or the `return_value` overload to be called if elision is
5474
+ not performed, and the selected constructor or `return_value` overload
5475
+ must be accessible even if the call is elided. — *end note*]
5476
+
5477
+ [*Example 2*:
5478
+
5479
+ ``` cpp
5480
+ class Thing {
5481
+ public:
5482
+ Thing();
5483
+ ~Thing();
5484
+ Thing(Thing&&);
5485
+ private:
5486
+ Thing(const Thing&);
5487
+ };
5488
+
5489
+ Thing f(bool b) {
5490
+ Thing t;
5491
+ if (b)
5492
+ throw t; // OK: Thing(Thing&&) used (or elided) to throw t
5493
+ return t; // OK: Thing(Thing&&) used (or elided) to return t
5494
+ }
5495
+
5496
+ Thing t2 = f(false); // OK: no extra copy/move performed, t2 constructed by call to f
5497
+
5498
+ struct Weird {
5499
+ Weird();
5500
+ Weird(Weird&);
5501
+ };
5502
+
5503
+ Weird g() {
5504
+ Weird w;
5505
+ return w; // OK: first overload resolution fails, second overload resolution selects Weird(Weird&)
5506
+ }
5507
+ ```
5508
+
5509
+ — *end example*]
5510
+
5511
+ [*Example 3*:
5512
+
5513
+ ``` cpp
5514
+ template<class T> void g(const T&);
5515
+
5516
+ template<class T> void f() {
5517
+ T x;
5518
+ try {
5519
+ T y;
5520
+ try { g(x); }
5521
+ catch (...) {
5522
+ if (/*...*/)
5523
+ throw x; // does not move
5524
+ throw y; // moves
5525
+ }
5526
+ g(y);
5527
+ } catch(...) {
5528
+ g(x);
5529
+ g(y); // error: y is not in scope
5530
+ }
5531
+ }
5532
+ ```
5533
+
5534
+ — *end example*]
5535
+
5536
+ ## Comparisons <a id="class.compare">[[class.compare]]</a>
5537
+
5538
+ ### Defaulted comparison operator functions <a id="class.compare.default">[[class.compare.default]]</a>
5539
+
5540
+ A defaulted comparison operator function [[over.binary]] for some class
5541
+ `C` shall be a non-template function that is
5542
+
5543
+ - a non-static const non-volatile member of `C` having one parameter of
5544
+ type `const C&` and either no *ref-qualifier* or the *ref-qualifier*
5545
+ `&`, or
5546
+ - a friend of `C` having either two parameters of type `const C&` or two
5547
+ parameters of type `C`.
5548
+
5549
+ A comparison operator function for class `C` that is defaulted on its
5550
+ first declaration and is not defined as deleted is *implicitly defined*
5551
+ when it is odr-used or needed for constant evaluation. Name lookups in
5552
+ the defaulted definition of a comparison operator function are performed
5553
+ from a context equivalent to its *function-body*. A definition of a
5554
+ comparison operator as defaulted that appears in a class shall be the
5555
+ first declaration of that function.
5556
+
5557
+ A defaulted `<=>` or `==` operator function for class `C` is defined as
5558
+ deleted if any non-static data member of `C` is of reference type or `C`
5559
+ has variant members [[class.union.anon]].
5560
+
5561
+ A binary operator expression `a @ b` is *usable* if either
5562
+
5563
+ - `a` or `b` is of class or enumeration type and overload resolution
5564
+ [[over.match]] as applied to `a @ b` results in a usable candidate, or
5565
+ - neither `a` nor `b` is of class or enumeration type and `a @ b` is a
5566
+ valid expression.
5567
+
5568
+ A defaulted comparison function is *constexpr-compatible* if it
5569
+ satisfies the requirements for a constexpr function [[dcl.constexpr]]
5570
+ and no overload resolution performed when determining whether to delete
5571
+ the function results in a usable candidate that is a non-constexpr
5572
+ function.
5573
+
5574
+ [*Note 1*:
5575
+
5576
+ This includes the overload resolutions performed:
5577
+
5578
+ - for an `operator<=>` whose return type is not `auto`, when determining
5579
+ whether a synthesized three-way comparison is defined,
5580
+ - for an `operator<=>` whose return type is `auto` or for an
5581
+ `operator==`, for a comparison between an element of the expanded list
5582
+ of subobjects and itself, or
5583
+ - for a secondary comparison operator `@`, for the expression `x @ y`.
5584
+
5585
+ — *end note*]
5586
+
5587
+ If the *member-specification* does not explicitly declare any member or
5588
+ friend named `operator==`, an `==` operator function is declared
5589
+ implicitly for each three-way comparison operator function defined as
5590
+ defaulted in the *member-specification*, with the same access and
5591
+ *function-definition* and in the same class scope as the respective
5592
+ three-way comparison operator function, except that the return type is
5593
+ replaced with `bool` and the *declarator-id* is replaced with
5594
+ `operator==`.
5595
+
5596
+ [*Note 2*: Such an implicitly-declared `==` operator for a class `X` is
5597
+ defined as defaulted in the definition of `X` and has the same
5598
+ *parameter-declaration-clause* and trailing *requires-clause* as the
5599
+ respective three-way comparison operator. It is declared with `friend`,
5600
+ `virtual`, `constexpr`, or `consteval` if the three-way comparison
5601
+ operator function is so declared. If the three-way comparison operator
5602
+ function has no *noexcept-specifier*, the implicitly-declared `==`
5603
+ operator function has an implicit exception specification
5604
+ [[except.spec]] that may differ from the implicit exception
5605
+ specification of the three-way comparison operator
5606
+ function. — *end note*]
5607
+
5608
+ [*Example 1*:
5609
+
5610
+ ``` cpp
5611
+ template<typename T> struct X {
5612
+ friend constexpr std::partial_ordering operator<=>(X, X) requires (sizeof(T) != 1) = default;
5613
+ // implicitly declares: friend constexpr bool operator==(X, X) requires (sizeof(T) != 1) = default;
5614
+
5615
+ [[nodiscard]] virtual std::strong_ordering operator<=>(const X&) const = default;
5616
+ // implicitly declares: [[nodiscard]] virtual bool operator==(const X&) const = default;
5617
+ };
5618
+ ```
5619
+
5620
+ — *end example*]
5621
+
5622
+ [*Note 3*: The `==` operator function is declared implicitly even if
5623
+ the defaulted three-way comparison operator function is defined as
5624
+ deleted. — *end note*]
5625
+
5626
+ The direct base class subobjects of `C`, in the order of their
5627
+ declaration in the *base-specifier-list* of `C`, followed by the
5628
+ non-static data members of `C`, in the order of their declaration in the
5629
+ *member-specification* of `C`, form a list of subobjects. In that list,
5630
+ any subobject of array type is recursively expanded to the sequence of
5631
+ its elements, in the order of increasing subscript. Let `xᵢ` be an
5632
+ lvalue denoting the iᵗʰ element in the expanded list of subobjects for
5633
+ an object `x` (of length n), where `xᵢ` is formed by a sequence of
5634
+ derived-to-base conversions [[over.best.ics]], class member access
5635
+ expressions [[expr.ref]], and array subscript expressions [[expr.sub]]
5636
+ applied to `x`.
5637
+
5638
+ ### Equality operator <a id="class.eq">[[class.eq]]</a>
5639
+
5640
+ A defaulted equality operator function [[over.binary]] shall have a
5641
+ declared return type `bool`.
5642
+
5643
+ A defaulted `==` operator function for a class `C` is defined as deleted
5644
+ unless, for each `xᵢ` in the expanded list of subobjects for an object
5645
+ `x` of type `C`, `xᵢ`` == ``xᵢ` is usable [[class.compare.default]].
5646
+
5647
+ The return value `V` of a defaulted `==` operator function with
5648
+ parameters `x` and `y` is determined by comparing corresponding elements
5649
+ `xᵢ` and `yᵢ` in the expanded lists of subobjects for `x` and `y` (in
5650
+ increasing index order) until the first index i where `xᵢ`` == ``yᵢ`
5651
+ yields a result value which, when contextually converted to `bool`,
5652
+ yields `false`. If no such index exists, `V` is `true`. Otherwise, `V`
5653
+ is `false`.
5654
+
5655
+ [*Example 1*:
5656
+
5657
+ ``` cpp
5658
+ struct D {
5659
+ int i;
5660
+ friend bool operator==(const D& x, const D& y) = default;
5661
+ // OK, returns x.i == y.i
5662
+ };
5663
+ ```
5664
+
5665
+ — *end example*]
5666
+
5667
+ ### Three-way comparison <a id="class.spaceship">[[class.spaceship]]</a>
5668
+
5669
+ The *synthesized three-way comparison* of type `R` [[cmp.categories]] of
5670
+ glvalues `a` and `b` of the same type is defined as follows:
5671
+
5672
+ - If `a <=> b` is usable [[class.compare.default]],
5673
+ `static_cast<R>(a <=> b)`.
5674
+ - Otherwise, if overload resolution for `a <=> b` is performed and finds
5675
+ at least one viable candidate, the synthesized three-way comparison is
5676
+ not defined.
5677
+ - Otherwise, if `R` is not a comparison category type, or either the
5678
+ expression `a == b` or the expression `a < b` is not usable, the
5679
+ synthesized three-way comparison is not defined.
5680
+ - Otherwise, if `R` is `strong_ordering`, then
5681
+ ``` cpp
5682
+ a == b ? strong_ordering::equal :
5683
+ a < b ? strong_ordering::less :
5684
+ strong_ordering::greater
5685
+ ```
5686
+ - Otherwise, if `R` is `weak_ordering`, then
5687
+ ``` cpp
5688
+ a == b ? weak_ordering::equivalent :
5689
+ a < b ? weak_ordering::less :
5690
+ weak_ordering::greater
5691
+ ```
5692
+ - Otherwise (when `R` is `partial_ordering`),
5693
+ ``` cpp
5694
+ a == b ? partial_ordering::equivalent :
5695
+ a < b ? partial_ordering::less :
5696
+ b < a ? partial_ordering::greater :
5697
+ partial_ordering::unordered
5698
+ ```
5699
+
5700
+ [*Note 1*: A synthesized three-way comparison may be ill-formed if
5701
+ overload resolution finds usable candidates that do not otherwise meet
5702
+ the requirements implied by the defined expression. — *end note*]
5703
+
5704
+ Let `R` be the declared return type of a defaulted three-way comparison
5705
+ operator function, and let `xᵢ` be the elements of the expanded list of
5706
+ subobjects for an object `x` of type `C`.
5707
+
5708
+ - If `R` is `auto`, then let cvᵢ~`Rᵢ` be the type of the expression
5709
+ `xᵢ`` <=> ``xᵢ`. The operator function is defined as deleted if that
5710
+ expression is not usable or if `Rᵢ` is not a comparison category type
5711
+ [[cmp.categories.pre]] for any i. The return type is deduced as the
5712
+ common comparison type (see below) of `R₀`, `R₁`, …, `R_n-1`.
5713
+ - Otherwise, `R` shall not contain a placeholder type. If the
5714
+ synthesized three-way comparison of type `R` between any objects `xᵢ`
5715
+ and `xᵢ` is not defined, the operator function is defined as deleted.
5716
+
5717
+ The return value `V` of type `R` of the defaulted three-way comparison
5718
+ operator function with parameters `x` and `y` of the same type is
5719
+ determined by comparing corresponding elements `xᵢ` and `yᵢ` in the
5720
+ expanded lists of subobjects for `x` and `y` (in increasing index order)
5721
+ until the first index i where the synthesized three-way comparison of
5722
+ type `R` between `xᵢ` and `yᵢ` yields a result value `vᵢ` where
5723
+ `vᵢ` `!=` 0, contextually converted to `bool`, yields `true`; `V` is a
5724
+ copy of `vᵢ`. If no such index exists, `V` is
5725
+ `static_cast<R>(std::strong_ordering::equal)`.
5726
+
5727
+ The *common comparison type* `U` of a possibly-empty list of n
5728
+ comparison category types `T₀`, `T₁`, …, `T_n-1` is defined as follows:
5729
+
5730
+ - If at least one `Tᵢ` is `std::partial_ordering`, `U` is
5731
+ `std::partial_ordering` [[cmp.partialord]].
5732
+ - Otherwise, if at least one `Tᵢ` is `std::weak_ordering`, `U` is
5733
+ `std::weak_ordering` [[cmp.weakord]].
5734
+ - Otherwise, `U` is `std::strong_ordering` [[cmp.strongord]].
5735
+ \[*Note 2*: In particular, this is the result when n is
5736
+ 0. — *end note*]
5737
+
5738
+ ### Secondary comparison operators <a id="class.compare.secondary">[[class.compare.secondary]]</a>
5739
+
5740
+ A *secondary comparison operator* is a relational operator [[expr.rel]]
5741
+ or the `!=` operator. A defaulted operator function [[over.binary]] for
5742
+ a secondary comparison operator `@` shall have a declared return type
5743
+ `bool`.
5744
+
5745
+ The operator function with parameters `x` and `y` is defined as deleted
5746
+ if
5747
+
5748
+ - overload resolution [[over.match]], as applied to `x @ y`, does not
5749
+ result in a usable candidate, or
5750
+ - the candidate selected by overload resolution is not a rewritten
5751
+ candidate.
5752
+
5753
+ Otherwise, the operator function yields `x @ y`. The defaulted operator
5754
+ function is not considered as a candidate in the overload resolution for
5755
+ the `@` operator.
5756
+
5757
+ [*Example 1*:
5758
+
5759
+ ``` cpp
5760
+ struct HasNoLessThan { };
5761
+
5762
+ struct C {
5763
+ friend HasNoLessThan operator<=>(const C&, const C&);
5764
+ bool operator<(const C&) const = default; // OK, function is deleted
5765
+ };
5766
+ ```
5767
+
5768
+ — *end example*]
5769
+
5770
+ ## Free store <a id="class.free">[[class.free]]</a>
5771
+
5772
+ Any allocation function for a class `T` is a static member (even if not
5773
+ explicitly declared `static`).
5774
+
5775
+ [*Example 1*:
5776
+
5777
+ ``` cpp
5778
+ class Arena;
5779
+ struct B {
5780
+ void* operator new(std::size_t, Arena*);
5781
+ };
5782
+ struct D1 : B {
5783
+ };
5784
+
5785
+ Arena* ap;
5786
+ void foo(int i) {
5787
+ new (ap) D1; // calls B::operator new(std::size_t, Arena*)
5788
+ new D1[i]; // calls ::operator new[](std::size_t)
5789
+ new D1; // error: ::operator new(std::size_t) hidden
5790
+ }
5791
+ ```
5792
+
5793
+ — *end example*]
5794
+
5795
+ When an object is deleted with a *delete-expression* [[expr.delete]], a
5796
+ deallocation function (`operator delete()` for non-array objects or
5797
+ `operator delete[]()` for arrays) is (implicitly) called to reclaim the
5798
+ storage occupied by the object [[basic.stc.dynamic.deallocation]].
5799
+
5800
+ Class-specific deallocation function lookup is a part of general
5801
+ deallocation function lookup [[expr.delete]] and occurs as follows. If
5802
+ the *delete-expression* is used to deallocate a class object whose
5803
+ static type has a virtual destructor, the deallocation function is the
5804
+ one selected at the point of definition of the dynamic type’s virtual
5805
+ destructor [[class.dtor]].[^15] Otherwise, if the *delete-expression* is
5806
+ used to deallocate an object of class `T` or array thereof, the
5807
+ deallocation function’s name is looked up in the scope of `T`. If this
5808
+ lookup fails to find the name, general deallocation function lookup
5809
+ [[expr.delete]] continues. If the result of the lookup is ambiguous or
5810
+ inaccessible, or if the lookup selects a placement deallocation
5811
+ function, the program is ill-formed.
5812
+
5813
+ Any deallocation function for a class `X` is a static member (even if
5814
+ not explicitly declared `static`).
5815
+
5816
+ [*Example 2*:
5817
+
5818
+ ``` cpp
5819
+ class X {
5820
+ void operator delete(void*);
5821
+ void operator delete[](void*, std::size_t);
5822
+ };
5823
+
5824
+ class Y {
5825
+ void operator delete(void*, std::size_t);
5826
+ void operator delete[](void*);
5827
+ };
5828
+ ```
5829
+
5830
+ — *end example*]
5831
+
5832
+ Since member allocation and deallocation functions are `static` they
5833
+ cannot be virtual.
5834
+
5835
+ [*Note 1*:
5836
+
5837
+ However, when the *cast-expression* of a *delete-expression* refers to
5838
+ an object of class type, because the deallocation function actually
5839
+ called is looked up in the scope of the class that is the dynamic type
5840
+ of the object if the destructor is virtual, the effect is the same in
5841
+ that case. For example,
5842
+
5843
+ ``` cpp
5844
+ struct B {
5845
+ virtual ~B();
5846
+ void operator delete(void*, std::size_t);
5847
+ };
5848
+
5849
+ struct D : B {
5850
+ void operator delete(void*);
5851
+ };
5852
+
5853
+ struct E : B {
5854
+ void log_deletion();
5855
+ void operator delete(E *p, std::destroying_delete_t) {
5856
+ p->log_deletion();
5857
+ p->~E();
5858
+ ::operator delete(p);
5859
+ }
5860
+ };
5861
+
5862
+ void f() {
5863
+ B* bp = new D;
5864
+ delete bp; // 1: uses D::operator delete(void*)
5865
+ bp = new E;
5866
+ delete bp; // 2: uses E::operator delete(E*, std::destroying_delete_t)
5867
+ }
5868
+ ```
5869
+
5870
+ Here, storage for the object of class `D` is deallocated by
5871
+ `D::operator delete()`, and the object of class `E` is destroyed and its
5872
+ storage is deallocated by `E::operator delete()`, due to the virtual
5873
+ destructor.
5874
+
5875
+ — *end note*]
5876
+
5877
+ [*Note 2*:
5878
+
5879
+ Virtual destructors have no effect on the deallocation function actually
5880
+ called when the *cast-expression* of a *delete-expression* refers to an
5881
+ array of objects of class type. For example,
5882
+
5883
+ ``` cpp
5884
+ struct B {
5885
+ virtual ~B();
5886
+ void operator delete[](void*, std::size_t);
5887
+ };
5888
+
5889
+ struct D : B {
5890
+ void operator delete[](void*, std::size_t);
5891
+ };
5892
+
5893
+ void f(int i) {
5894
+ D* dp = new D[i];
5895
+ delete [] dp; // uses D::operator delete[](void*, std::size_t)
5896
+ B* bp = new D[i];
5897
+ delete[] bp; // undefined behavior
5898
+ }
5899
+ ```
5900
+
5901
+ — *end note*]
5902
+
5903
+ Access to the deallocation function is checked statically. Hence, even
5904
+ though a different one might actually be executed, the statically
5905
+ visible deallocation function is required to be accessible.
5906
+
5907
+ [*Example 3*: For the call on line “// 1” above, if
5908
+ `B::operator delete()` had been private, the delete expression would
5909
+ have been ill-formed. — *end example*]
5910
+
5911
+ [*Note 3*: If a deallocation function has no explicit
5912
+ *noexcept-specifier*, it has a non-throwing exception specification
5913
+ [[except.spec]]. — *end note*]
5914
+
5915
+ <!-- Link reference definitions -->
5916
+ [basic.compound]: basic.md#basic.compound
5917
+ [basic.def]: basic.md#basic.def
5918
+ [basic.def.odr]: basic.md#basic.def.odr
5919
+ [basic.life]: basic.md#basic.life
5920
+ [basic.link]: basic.md#basic.link
5921
+ [basic.lookup]: basic.md#basic.lookup
5922
+ [basic.lookup.elab]: basic.md#basic.lookup.elab
5923
+ [basic.lookup.qual]: basic.md#basic.lookup.qual
5924
+ [basic.lookup.unqual]: basic.md#basic.lookup.unqual
5925
+ [basic.lval]: expr.md#basic.lval
5926
+ [basic.scope]: basic.md#basic.scope
5927
+ [basic.scope.class]: basic.md#basic.scope.class
5928
+ [basic.scope.hiding]: basic.md#basic.scope.hiding
5929
+ [basic.start.dynamic]: basic.md#basic.start.dynamic
5930
+ [basic.start.static]: basic.md#basic.start.static
5931
+ [basic.start.term]: basic.md#basic.start.term
5932
+ [basic.stc]: basic.md#basic.stc
5933
+ [basic.stc.auto]: basic.md#basic.stc.auto
5934
+ [basic.stc.dynamic]: basic.md#basic.stc.dynamic
5935
+ [basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
5936
+ [basic.stc.static]: basic.md#basic.stc.static
5937
+ [basic.stc.thread]: basic.md#basic.stc.thread
5938
+ [basic.type.qualifier]: basic.md#basic.type.qualifier
5939
+ [basic.types]: basic.md#basic.types
5940
+ [class]: #class
5941
+ [class.abstract]: #class.abstract
5942
+ [class.access]: #class.access
5943
+ [class.access.base]: #class.access.base
5944
+ [class.access.nest]: #class.access.nest
5945
+ [class.access.spec]: #class.access.spec
5946
+ [class.access.virt]: #class.access.virt
5947
+ [class.base.init]: #class.base.init
5948
+ [class.bit]: #class.bit
5949
+ [class.cdtor]: #class.cdtor
5950
+ [class.compare]: #class.compare
5951
+ [class.compare.default]: #class.compare.default
5952
+ [class.compare.secondary]: #class.compare.secondary
5953
+ [class.conv]: #class.conv
5954
+ [class.conv.ctor]: #class.conv.ctor
5955
+ [class.conv.fct]: #class.conv.fct
5956
+ [class.copy.assign]: #class.copy.assign
5957
+ [class.copy.ctor]: #class.copy.ctor
5958
+ [class.copy.elision]: #class.copy.elision
5959
+ [class.ctor]: #class.ctor
5960
+ [class.default.ctor]: #class.default.ctor
5961
+ [class.derived]: #class.derived
5962
+ [class.dtor]: #class.dtor
5963
+ [class.eq]: #class.eq
5964
+ [class.expl.init]: #class.expl.init
5965
+ [class.free]: #class.free
5966
+ [class.friend]: #class.friend
5967
+ [class.inhctor.init]: #class.inhctor.init
5968
+ [class.init]: #class.init
5969
+ [class.local]: #class.local
5970
+ [class.mem]: #class.mem
5971
+ [class.member.lookup]: #class.member.lookup
5972
+ [class.mfct]: #class.mfct
5973
+ [class.mfct.non-static]: #class.mfct.non-static
5974
+ [class.mi]: #class.mi
5975
+ [class.name]: #class.name
5976
+ [class.nest]: #class.nest
5977
+ [class.nested.type]: #class.nested.type
5978
+ [class.paths]: #class.paths
5979
+ [class.pre]: #class.pre
5980
+ [class.prop]: #class.prop
5981
+ [class.protected]: #class.protected
5982
+ [class.qual]: basic.md#class.qual
5983
+ [class.spaceship]: #class.spaceship
5984
+ [class.static]: #class.static
5985
+ [class.static.data]: #class.static.data
5986
+ [class.static.mfct]: #class.static.mfct
5987
+ [class.temporary]: basic.md#class.temporary
5988
+ [class.this]: #class.this
5989
+ [class.union]: #class.union
5990
+ [class.union.anon]: #class.union.anon
5991
+ [class.virtual]: #class.virtual
5992
+ [cmp.categories]: support.md#cmp.categories
5993
+ [cmp.categories.pre]: support.md#cmp.categories.pre
5994
+ [cmp.partialord]: support.md#cmp.partialord
5995
+ [cmp.strongord]: support.md#cmp.strongord
5996
+ [cmp.weakord]: support.md#cmp.weakord
5997
+ [conv]: expr.md#conv
5998
+ [conv.mem]: expr.md#conv.mem
5999
+ [conv.ptr]: expr.md#conv.ptr
6000
+ [conv.rval]: expr.md#conv.rval
6001
+ [dcl.array]: dcl.md#dcl.array
6002
+ [dcl.attr.nouniqueaddr]: dcl.md#dcl.attr.nouniqueaddr
6003
+ [dcl.constexpr]: dcl.md#dcl.constexpr
6004
+ [dcl.decl]: dcl.md#dcl.decl
6005
+ [dcl.enum]: dcl.md#dcl.enum
6006
+ [dcl.fct]: dcl.md#dcl.fct
6007
+ [dcl.fct.def]: dcl.md#dcl.fct.def
6008
+ [dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
6009
+ [dcl.fct.def.delete]: dcl.md#dcl.fct.def.delete
6010
+ [dcl.fct.def.general]: dcl.md#dcl.fct.def.general
6011
+ [dcl.fct.default]: dcl.md#dcl.fct.default
6012
+ [dcl.fct.spec]: dcl.md#dcl.fct.spec
6013
+ [dcl.init]: dcl.md#dcl.init
6014
+ [dcl.init.aggr]: dcl.md#dcl.init.aggr
6015
+ [dcl.init.list]: dcl.md#dcl.init.list
6016
+ [dcl.init.ref]: dcl.md#dcl.init.ref
6017
+ [dcl.inline]: dcl.md#dcl.inline
6018
+ [dcl.spec.auto]: dcl.md#dcl.spec.auto
6019
+ [dcl.stc]: dcl.md#dcl.stc
6020
+ [dcl.type.cv]: dcl.md#dcl.type.cv
6021
+ [dcl.type.elab]: dcl.md#dcl.type.elab
6022
+ [dcl.type.simple]: dcl.md#dcl.type.simple
6023
+ [dcl.typedef]: dcl.md#dcl.typedef
6024
+ [depr.impldec]: future.md#depr.impldec
6025
+ [depr.static.constexpr]: future.md#depr.static.constexpr
6026
+ [diff.class]: compatibility.md#diff.class
6027
+ [except.ctor]: except.md#except.ctor
6028
+ [except.handle]: except.md#except.handle
6029
+ [except.pre]: except.md#except.pre
6030
+ [except.spec]: except.md#except.spec
6031
+ [except.throw]: except.md#except.throw
6032
+ [expr.ass]: expr.md#expr.ass
6033
+ [expr.call]: expr.md#expr.call
6034
+ [expr.cast]: expr.md#expr.cast
6035
+ [expr.const]: expr.md#expr.const
6036
+ [expr.const.cast]: expr.md#expr.const.cast
6037
+ [expr.delete]: expr.md#expr.delete
6038
+ [expr.dynamic.cast]: expr.md#expr.dynamic.cast
6039
+ [expr.eq]: expr.md#expr.eq
6040
+ [expr.new]: expr.md#expr.new
6041
+ [expr.prim.id]: expr.md#expr.prim.id
6042
+ [expr.prim.id.dtor]: expr.md#expr.prim.id.dtor
6043
+ [expr.prim.id.qual]: expr.md#expr.prim.id.qual
6044
+ [expr.prim.this]: expr.md#expr.prim.this
6045
+ [expr.ref]: expr.md#expr.ref
6046
+ [expr.reinterpret.cast]: expr.md#expr.reinterpret.cast
6047
+ [expr.rel]: expr.md#expr.rel
6048
+ [expr.static.cast]: expr.md#expr.static.cast
6049
+ [expr.sub]: expr.md#expr.sub
6050
+ [expr.throw]: expr.md#expr.throw
6051
+ [expr.type.conv]: expr.md#expr.type.conv
6052
+ [expr.typeid]: expr.md#expr.typeid
6053
+ [expr.unary.op]: expr.md#expr.unary.op
6054
+ [intro.execution]: basic.md#intro.execution
6055
+ [intro.object]: basic.md#intro.object
6056
+ [namespace.def]: dcl.md#namespace.def
6057
+ [namespace.memdef]: dcl.md#namespace.memdef
6058
+ [namespace.udecl]: dcl.md#namespace.udecl
6059
+ [over]: over.md#over
6060
+ [over.ass]: over.md#over.ass
6061
+ [over.best.ics]: over.md#over.best.ics
6062
+ [over.binary]: over.md#over.binary
6063
+ [over.ics.ref]: over.md#over.ics.ref
6064
+ [over.load]: over.md#over.load
6065
+ [over.match]: over.md#over.match
6066
+ [over.match.best]: over.md#over.match.best
6067
+ [over.match.call]: over.md#over.match.call
6068
+ [over.match.copy]: over.md#over.match.copy
6069
+ [over.match.funcs]: over.md#over.match.funcs
6070
+ [over.oper]: over.md#over.oper
6071
+ [over.over]: over.md#over.over
6072
+ [special]: #special
6073
+ [stmt.dcl]: stmt.md#stmt.dcl
6074
+ [stmt.return]: stmt.md#stmt.return
6075
+ [stmt.return.coroutine]: stmt.md#stmt.return.coroutine
6076
+ [string.classes]: strings.md#string.classes
6077
+ [temp.arg]: temp.md#temp.arg
6078
+ [temp.class.spec]: temp.md#temp.class.spec
6079
+ [temp.constr]: temp.md#temp.constr
6080
+ [temp.constr.order]: temp.md#temp.constr.order
6081
+ [temp.dep.type]: temp.md#temp.dep.type
6082
+ [temp.expl.spec]: temp.md#temp.expl.spec
6083
+ [temp.friend]: temp.md#temp.friend
6084
+ [temp.inst]: temp.md#temp.inst
6085
+ [temp.mem]: temp.md#temp.mem
6086
+ [temp.param]: temp.md#temp.param
6087
+ [temp.pre]: temp.md#temp.pre
6088
+ [temp.spec]: temp.md#temp.spec
6089
+ [temp.variadic]: temp.md#temp.variadic
6090
+
6091
+ [^1]: This ensures that two subobjects that have the same class type and
6092
+ that belong to the same most derived object are not allocated at the
6093
+ same address [[expr.eq]].
6094
+
6095
+ [^2]: See, for example, `<cstring>`.
6096
+
6097
+ [^3]: This implies that the reference parameter of the
6098
+ implicitly-declared copy constructor cannot bind to a `volatile`
6099
+ lvalue; see  [[diff.class]].
6100
+
6101
+ [^4]: Because a template assignment operator or an assignment operator
6102
+ taking an rvalue reference parameter is never a copy assignment
6103
+ operator, the presence of such an assignment operator does not
6104
+ suppress the implicit declaration of a copy assignment operator.
6105
+ Such assignment operators participate in overload resolution with
6106
+ other assignment operators, including copy assignment operators,
6107
+ and, if selected, will be used to assign an object.
6108
+
6109
+ [^5]: This implies that the reference parameter of the
6110
+ implicitly-declared copy assignment operator cannot bind to a
6111
+ `volatile` lvalue; see  [[diff.class]].
6112
+
6113
+ [^6]: These conversions are considered as standard conversions for the
6114
+ purposes of overload resolution ([[over.best.ics]],
6115
+ [[over.ics.ref]]) and therefore initialization [[dcl.init]] and
6116
+ explicit casts [[expr.static.cast]]. A conversion to `void` does not
6117
+ invoke any conversion function [[expr.static.cast]]. Even though
6118
+ never directly called to perform a conversion, such conversion
6119
+ functions can be declared and can potentially be reached through a
6120
+ call to a virtual conversion function in a base class.
6121
+
6122
+ [^7]: The use of the `virtual` specifier in the declaration of an
6123
+ overriding function is valid but redundant (has empty semantics).
6124
+
6125
+ [^8]: If all virtual functions are immediate functions, the class is
6126
+ still polymorphic even though its internal representation might not
6127
+ otherwise require any additions for that polymorphic behavior.
6128
+
6129
+ [^9]: A function with the same name but a different parameter list
6130
+ [[over]] as a virtual function is not necessarily virtual and does
6131
+ not override. Access control [[class.access]] is not considered in
6132
+ determining overriding.
6133
+
6134
+ [^10]: Multi-level pointers to classes or references to multi-level
6135
+ pointers to classes are not allowed.
6136
+
6137
+ [^11]: Access permissions are thus transitive and cumulative to nested
6138
+ and local classes.
6139
+
6140
+ [^12]: As specified previously in [[class.access]], private members of a
6141
+ base class remain inaccessible even to derived classes unless friend
6142
+ declarations within the base class definition are used to grant
6143
+ access explicitly.
6144
+
6145
+ [^13]: This additional check does not apply to other members, e.g.,
6146
+ static data members or enumerator member constants.
6147
+
6148
+ [^14]: Because only one object is destroyed instead of two, and one
6149
+ copy/move constructor is not executed, there is still one object
6150
+ destroyed for each one constructed.
6151
+
6152
+ [^15]: A similar provision is not needed for the array version of
6153
+ `operator` `delete` because  [[expr.delete]] requires that in this
6154
+ situation, the static type of the object to be deleted be the same
6155
+ as its dynamic type.