-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathclasses.tex
6843 lines (6145 loc) · 213 KB
/
classes.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!TEX root = std.tex
\rSec0[class]{Classes}%
\indextext{class|(}
\gramSec[gram.class]{Classes}
\indextext{class!member function|see{member function, class}}
\rSec1[class.pre]{Preamble}
\pnum
\indextext{\idxcode{\{\}}!class declaration}%
\indextext{\idxcode{\{\}}!class definition}%
\indextext{type!class and}%
\indextext{object class|see{class object}}%
A class is a type.
\indextext{name class|see{class name}}%
Its name becomes a \grammarterm{class-name}\iref{class.name} within its
scope.
\begin{bnf}
\nontermdef{class-name}\br
identifier\br
simple-template-id
\end{bnf}
A \grammarterm{class-specifier} or
an \grammarterm{elaborated-type-specifier}\iref{dcl.type.elab} is used to
make a \grammarterm{class-name}. An object of a class consists of a
(possibly empty) sequence of members and base class objects.
\begin{bnf}
\nontermdef{class-specifier}\br
class-head \terminal{\{} \opt{member-specification} \terminal{\}}
\end{bnf}
\begin{bnf}
\nontermdef{class-head}\br
class-key \opt{attribute-specifier-seq} class-head-name \opt{class-property-specifier-seq} \opt{base-clause}\br
class-key \opt{attribute-specifier-seq} \opt{base-clause}
\end{bnf}
\begin{bnf}
\nontermdef{class-head-name}\br
\opt{nested-name-specifier} class-name
\end{bnf}
\begin{bnf}
\nontermdef{class-property-specifier-seq}\br
class-property-specifier \opt{class-property-specifier-seq}
\end{bnf}
\begin{bnf}
\nontermdef{class-property-specifier}\br
\keyword{final}\br
\keyword{trivially_relocatable_if_eligible}\br
\keyword{replaceable_if_eligible}
\end{bnf}
\begin{bnf}
\nontermdef{class-key}\br
\keyword{class}\br
\keyword{struct}\br
\keyword{union}
\end{bnf}
A class declaration where the \grammarterm{class-name}
in the \grammarterm{class-head-name} is a \grammarterm{simple-template-id}
shall be an explicit specialization\iref{temp.expl.spec} or
a partial specialization\iref{temp.spec.partial}.
A \grammarterm{class-specifier} whose
\grammarterm{class-head} omits the
\grammarterm{class-head-name} defines an \defnadj{unnamed}{class}.
\begin{note}
An unnamed class thus can't
be \tcode{final}.
\end{note}
Otherwise, the \grammarterm{class-name} is an \grammarterm{identifier};
it is not looked up, and the \grammarterm{class-specifier} introduces it.
\pnum
\indextext{component name}%
The component name of the
\grammarterm{class-name} is also bound in the scope of the class (template)
itself; this is known as the \defn{injected-class-name}.
For purposes of access checking, the injected-class-name is treated as
if it were a public member name.
A \grammarterm{class-specifier} is commonly referred to as a \defnx{class
definition}{definition!class}.
A class is considered defined after the closing brace of its
\grammarterm{class-specifier} has been seen even though its member
functions are in general not yet defined.
The optional \grammarterm{attribute-specifier-seq} appertains to the class; the attributes in
the \grammarterm{attribute-specifier-seq} are thereafter considered attributes of the class
whenever it is named.
\pnum
If a \grammarterm{class-head-name} contains a \grammarterm{nested-name-specifier},
the \grammarterm{class-specifier} shall not inhabit a class scope.
If its \grammarterm{class-name} is an \grammarterm{identifier},
the \grammarterm{class-specifier} shall correspond to
one or more declarations nominable in
the class, class template, or namespace to which the
\grammarterm{nested-name-specifier} refers;
they shall all have the same target scope, and the target scope of the
\grammarterm{class-specifier} is that scope.
\begin{example}
\begin{codeblock}
namespace N {
template<class>
struct A {
struct B;
};
}
using N::A;
template<class T> struct A<T>::B {}; // OK
template<> struct A<void> {}; // OK
\end{codeblock}
\end{example}
\pnum
\begin{note}
The \grammarterm{class-key} determines
whether the class is a union\iref{class.union} and
whether access is public or private by default\iref{class.access}.
A union holds the value of at most one data member at a time.
\end{note}
\pnum
Each \grammarterm{class-property-specifier} shall appear at most once
within a single \grammarterm{class-property-specifier-seq}.
Whenever a \grammarterm{class-key} is followed
by a \grammarterm{class-head-name},
the identifier \tcode{final}, \tcode{trivially_relocatable_if_eligible},
or \tcode{replaceable_if_eligible}, and a colon or left brace,
the identifier is interpreted as a \grammarterm{class-property-specifier}.
\begin{example}
\begin{codeblock}
struct A;
struct A final {}; // OK, definition of \tcode{struct A},
// not value-initialization of variable \tcode{final}
struct X {
struct C { constexpr operator int() { return 5; } };
struct B trivially_relocatable_if_eligible : C{};
// OK, definition of nested class \tcode{B},
// not declaration of a bit-field member
// \tcode{trivially_relocatable_if_eligible}
};
\end{codeblock}
\end{example}
\pnum
If a class is marked with the \grammarterm{class-property-specifier}
\tcode{final} and that class appears as a \grammarterm{class-or-decltype}
in a \grammarterm{base-clause}\iref{class.derived}, the program is ill-formed.
\pnum
\begin{note}
Complete objects of class type have nonzero size.
Base class subobjects and
members declared with the \tcode{no_unique_address} attribute\iref{dcl.attr.nouniqueaddr}
are not so constrained.
\end{note}
\pnum
\begin{note}
Class objects can be assigned\iref{over.assign,class.copy.assign},
passed as arguments to functions\iref{dcl.init,class.copy.ctor}, and
returned by functions (except objects of classes for which copying or moving has
been restricted; see~\ref{dcl.fct.def.delete} and \ref{class.access}).
Other plausible operators, such as equality comparison,
can be defined by the user; see~\ref{over.oper}.
\end{note}
\rSec1[class.prop]{Properties of classes}
\pnum
A \defnadj{trivially copyable}{class} is a class:
\begin{itemize}
\item that has at least one eligible
copy constructor, move constructor,
copy assignment operator, or
move assignment operator\iref{special,class.copy.ctor,class.copy.assign},
\item where each eligible copy constructor, move constructor, copy assignment operator,
and move assignment operator is trivial, and
\item that has a trivial, non-deleted destructor\iref{class.dtor}.
\end{itemize}
\pnum
A class \tcode{C} is \defn{default-movable} if
\begin{itemize}
\item overload resolution for direct-initializing an object of type \tcode{C}
from an xvalue of type \tcode{C} selects a constructor that is a direct member
of \tcode{C} and is neither user-provided nor deleted,
\item overload resolution for assigning to an lvalue of type \tcode{C} from an
xvalue of type \tcode{C} selects an assignment operator function that is a
direct member of \tcode{C} and is neither user-provided nor deleted, and
\item \tcode{C} has a destructor that is neither user-provided nor deleted.
\end{itemize}
\pnum
A class is \defn{eligible for trivial relocation} unless it
\begin{itemize}
\item has any virtual base classes,
\item has a base class that is not a trivially relocatable class,
\item has a non-static data member of an object type that is not of a
trivially relocatable type, or
\item has a deleted destructor,
\end{itemize}
except that it is \impldef{whether an otherwise-eligible union having one or
more subobjects of polymorphic class type is eligible for trivial relocation}
whether an otherwise-eligible union having one or more subobjects of
polymorphic class type is eligible for trivial relocation.
\pnum
A class \tcode{C} is a \defnadj{trivially relocatable}{class}
if it is eligible for trivial relocation and
\begin{itemize}
\item has the \tcode{trivially_relocatable_if_eligible} \grammarterm{class-property-specifier},
\item is a union with no user-declared special member functions, or
\item is default-movable.
\end{itemize}
\pnum
\begin{note}
A class with const-qualified or reference non-static data members can be
trivially relocatable.
\end{note}
\pnum
A class \tcode{C} is \defn{eligible for replacement} unless
\begin{itemize}
\item it has a base class that is not a replaceable class,
\item it has a non-static data member that is not of a replaceable type,
\item overload resolution fails or selects a deleted constructor when
direct-initializing an object of type \tcode{C} from an xvalue of type
\tcode{C}\iref{dcl.init.general},
\item overload resolution fails or selects a deleted assignment operator
function when assigning to an lvalue of type \tcode{C} from an xvalue of type
\tcode{C} \iref{expr.assign,over.assign}), or
\item it has a deleted destructor.
\end{itemize}
\pnum
A class \tcode{C} is a \defnadj{replaceable}{class} if it is
eligible for replacement and
\begin{itemize}
\item has the \tcode{replaceable_if_eligible} \grammarterm{class-property-specifier},
\item is a union with no user-declared special member functions, or
\item is default-movable.
\end{itemize}
\pnum
\begin{note}
Accessibility of the special member functions is not considered when
establishing trivial relocatability or replaceability.
\end{note}
\pnum
\begin{note}
Not all trivially copyable classes are trivially relocatable or replaceable.
\end{note}
\pnum
A class \tcode{S} is a \defnadj{standard-layout}{class} if it:
\begin{itemize}
\item has no non-static data members of type non-standard-layout class
(or array of such types) or reference,
\item has no virtual functions\iref{class.virtual} and no
virtual base classes\iref{class.mi},
\item has the same access control\iref{class.access}
for all non-static data members,
\item has no non-standard-layout base classes,
\item has at most one base class subobject of any given type,
\item has all non-static data members and bit-fields in the class and
its base classes first declared in the same class, and
\item has no element of the set $M(\mathtt{S})$ of types
as a base class,
where for any type \tcode{X}, $M(\mathtt{X})$ is defined as follows.
\begin{footnote}
This ensures that two subobjects that have the same class type and that
belong to the same most derived object are not allocated at the same
address\iref{expr.eq}.
\end{footnote}
\begin{note}
$M(\mathtt{X})$ is the set of the types of all non-base-class subobjects
that can be at a zero offset in \tcode{X}.
\end{note}
\begin{itemize}
\item If \tcode{X} is a non-union class type with no non-static data members,
the set $M(\mathtt{X})$ is empty.
\item If \tcode{X} is a non-union class type with a non-static data
member of type $\mathtt{X}_0$
that is either of zero size or
is the first non-static data member of \tcode{X}
(where said member may be an anonymous union),
the set $M(\mathtt{X})$ consists of $\mathtt{X}_0$ and the elements of
$M(\mathtt{X}_0)$.
\item If \tcode{X} is a union type, the set $M(\mathtt{X})$ is
the union of all $M(\mathtt{U}_i)$ and the set containing all $\mathtt{U}_i$,
where each $\mathtt{U}_i$ is the type of the $i^\text{th}$ non-static data member
of \tcode{X}.
\item If \tcode{X} is an array type with element type $\mathtt{X}_e$,
the set $M(\mathtt{X})$ consists of $\mathtt{X}_e$
and the elements of $M(\mathtt{X}_e)$.
\item If \tcode{X} is a non-class, non-array type, the set $M(\mathtt{X})$ is empty.
\end{itemize}
\end{itemize}
\pnum
\begin{example}
\begin{codeblock}
struct B { int i; }; // standard-layout class
struct C : B { }; // standard-layout class
struct D : C { }; // standard-layout class
struct E : D { char : 4; }; // not a standard-layout class
struct Q {};
struct S : Q { };
struct T : Q { };
struct U : S, T { }; // not a standard-layout class
\end{codeblock}
\end{example}
\pnum
A \defnadj{standard-layout}{struct} is a standard-layout class
defined with the \grammarterm{class-key} \keyword{struct} or the
\grammarterm{class-key} \keyword{class}.
A \defnadj{standard-layout}{union} is a standard-layout class
defined with the
\grammarterm{class-key} \keyword{union}.
\pnum
\begin{note}
Standard-layout classes are useful for communicating with
code written in other programming languages. Their layout is specified
in~\ref{class.mem.general} and~\ref{expr.rel}.
\end{note}
\pnum
\begin{example}
\begin{codeblock}
struct N { // neither trivially copyable nor standard-layout
int i;
int j;
virtual ~N();
};
struct T { // trivially copyable but not standard-layout
int i;
private:
int j;
};
struct SL { // standard-layout but not trivially copyable
int i;
int j;
~SL();
};
struct POD { // both trivially copyable and standard-layout
int i;
int j;
};
\end{codeblock}
\end{example}
\pnum
\begin{note}
Aggregates of class type are described in~\ref{dcl.init.aggr}.
\end{note}
\pnum
A class \tcode{S} is an \defnadj{implicit-lifetime}{class} if
\begin{itemize}
\item
it is an aggregate whose destructor is not user-provided or
\item
it has at least one trivial eligible constructor and
a trivial, non-deleted destructor.
\end{itemize}
\rSec1[class.name]{Class names}
\indextext{definition!class name as type}%
\indextext{structure tag|see{class name}}%
\indextext{equivalence!type}%
\pnum
A class definition introduces a new type.
\begin{example}
\begin{codeblock}
struct X { int a; };
struct Y { int a; };
X a1;
Y a2;
int a3;
\end{codeblock}
declares three variables of three different types. This implies that
\begin{codeblock}
a1 = a2; // error: \tcode{Y} assigned to \tcode{X}
a1 = a3; // error: \tcode{int} assigned to \tcode{X}
\end{codeblock}
are type mismatches, and that
\begin{codeblock}
int f(X);
int f(Y);
\end{codeblock}
\indextext{overloading}%
declare overloads\iref{over} named \tcode{f} and not
simply a single function \tcode{f} twice. For the same reason,
\begin{codeblock}
struct S { int a; };
struct S { int a; }; // error: double definition
\end{codeblock}
is ill-formed because it defines \tcode{S} twice.
\end{example}
\pnum
\indextext{definition!scope of class}%
\begin{note}
It can be necessary to use an \grammarterm{elaborated-type-specifier}
to refer to a class
that belongs to a scope in which its name is also bound to
a variable, function, or enumerator\iref{basic.lookup.elab}.
\begin{example}
\begin{codeblock}
struct stat {
// ...
};
stat gstat; // use plain \tcode{stat} to define variable
int stat(struct stat*); // \tcode{stat} now also names a function
void f() {
struct stat* ps; // \keyword{struct} prefix needed to name \tcode{struct stat}
stat(ps); // call \tcode{stat} function
}
\end{codeblock}
\end{example}
\indextext{class name!elaborated}%
\indextext{declaration!forward class}%
An \grammarterm{elaborated-type-specifier} can also be used to declare
an \grammarterm{identifier} as a \grammarterm{class-name}.
\begin{example}
\begin{codeblock}
struct s { int a; };
void g() {
struct s; // hide global \tcode{struct s} with a block-scope declaration
s* p; // refer to local \tcode{struct s}
struct s { char* p; }; // define local \tcode{struct s}
struct s; // redeclaration, has no effect
}
\end{codeblock}
\end{example}
Such declarations allow definition of classes that refer to each other.
\begin{example}
\begin{codeblock}
class Vector;
class Matrix {
// ...
friend Vector operator*(const Matrix&, const Vector&);
};
class Vector {
// ...
friend Vector operator*(const Matrix&, const Vector&);
};
\end{codeblock}
Declaration of friends is described in~\ref{class.friend},
operator functions in~\ref{over.oper}.
\end{example}
\end{note}
\pnum
\indextext{class name!elaborated}%
\indextext{elaborated type specifier|see{class name, elaborated}}%
\begin{note}
An \grammarterm{elaborated-type-specifier}\iref{dcl.type.elab} can also
be used as a \grammarterm{type-specifier} as part of a declaration. It
differs from a class declaration in that it can refer to
an existing class of the given name.
\end{note}
\begin{example}
\begin{codeblock}
struct s { int a; };
void g(int s) {
struct s* p = new struct s; // global \tcode{s}
p->a = s; // parameter \tcode{s}
}
\end{codeblock}
\end{example}
\pnum
\indextext{class name!point of declaration}%
\begin{note}
The declaration of a class name takes effect immediately after the
\grammarterm{identifier} is seen in the class definition or
\grammarterm{elaborated-type-specifier}.
\begin{example}
\begin{codeblock}
class A * A;
\end{codeblock}
first specifies \tcode{A} to be the name of a class and then redefines
it as the name of a pointer to an object of that class. This means that
the elaborated form \keyword{class} \tcode{A} must be used to refer to the
class. Such artistry with names can be confusing and is best avoided.
\end{example}
\end{note}
\pnum
\indextext{class name!\idxcode{typedef}}%
A \grammarterm{simple-template-id} is only a \grammarterm{class-name}
if its \grammarterm{template-name} names a class template.
\rSec1[class.mem]{Class members}%
\rSec2[class.mem.general]{General}%
\indextext{declaration!member}%
\indextext{data member|see{member}}
\begin{bnf}
\nontermdef{member-specification}\br
member-declaration \opt{member-specification}\br
access-specifier \terminal{:} \opt{member-specification}
\end{bnf}
\begin{bnf}
\nontermdef{member-declaration}\br
\opt{attribute-specifier-seq} \opt{decl-specifier-seq} \opt{member-declarator-list} \terminal{;}\br
function-definition\br
friend-type-declaration\br
using-declaration\br
using-enum-declaration\br
static_assert-declaration\br
template-declaration\br
explicit-specialization\br
deduction-guide\br
alias-declaration\br
opaque-enum-declaration\br
empty-declaration
\end{bnf}
\begin{bnf}
\nontermdef{member-declarator-list}\br
member-declarator\br
member-declarator-list \terminal{,} member-declarator
\end{bnf}
\begin{bnf}
\nontermdef{member-declarator}\br
declarator \opt{virt-specifier-seq} \opt{function-contract-specifier-seq} \opt{pure-specifier}\br
declarator requires-clause \opt{function-contract-specifier-seq}\br
declarator brace-or-equal-initializer\br
\opt{identifier} \opt{attribute-specifier-seq} \terminal{:} constant-expression \opt{brace-or-equal-initializer}
\end{bnf}
\begin{bnf}
\nontermdef{virt-specifier-seq}\br
virt-specifier \opt{virt-specifier-seq}
\end{bnf}
\begin{bnf}
\nontermdef{virt-specifier}\br
\keyword{override}\br
\keyword{final}
\end{bnf}
\begin{bnf}
\nontermdef{pure-specifier}\br
\terminal{=} \terminal{0}
\end{bnf}
\begin{bnf}
\nontermdef{friend-type-declaration}\br
\keyword{friend} friend-type-specifier-list \terminal{;}
\end{bnf}
\begin{bnf}
\nontermdef{friend-type-specifier-list}\br
friend-type-specifier \opt{\terminal{...}}\br
friend-type-specifier-list \terminal{,} friend-type-specifier \opt{\terminal{...}}
\end{bnf}
\begin{bnf}
\nontermdef{friend-type-specifier}\br
simple-type-specifier\br
elaborated-type-specifier\br
typename-specifier
\end{bnf}
\pnum
In the absence of a \grammarterm{virt-specifier-seq},
the token sequence \tcode{= 0} is treated as a \grammarterm{pure-specifier}
if the type of the \grammarterm{declarator-id}\iref{dcl.meaning.general}
is a function type, and
is otherwise treated as a \grammarterm{brace-or-equal-initializer}.
\begin{note}
If the member declaration acquires a function type through
template instantiation,
the program is ill-formed; see~\ref{temp.spec.general}.
\end{note}
\pnum
The optional \grammarterm{function-contract-specifier-seq}\iref{dcl.contract.func})
in a \grammarterm{member-declarator}
shall be present only if
the \grammarterm{declarator} declares a function.
\pnum
\indextext{definition!class}%
The \grammarterm{member-specification} in a class definition declares the
full set of members of the class; no member can be added elsewhere.
A \defnadj{direct}{member} of a class \tcode{X} is a member of \tcode{X}
that was first declared within the \grammarterm{member-specification} of \tcode{X},
including anonymous union members\iref{class.union.anon} and direct members thereof.
Members of a class are data members, member
functions\iref{class.mfct}, nested types, enumerators,
and member templates\iref{temp.mem} and specializations thereof.
\begin{note}
A specialization of a static data member template is a static data member.
A specialization of a member function template is a member function.
A specialization of a member class template is a nested class.
\end{note}
\pnum
A \grammarterm{member-declaration} does not declare new members of the class
if it is
\begin{itemize}
\item a friend declaration\iref{class.friend},
\item a \grammarterm{deduction-guide}\iref{temp.deduct.guide},
\item a \grammarterm{template-declaration} whose \grammarterm{declaration} is one of the above,
\item a \grammarterm{static_assert-declaration},
\item a \grammarterm{using-declaration}\iref{namespace.udecl}, or
\item an \grammarterm{empty-declaration}.
\end{itemize}
For any other \grammarterm{member-declaration},
each declared entity
that is not an unnamed bit-field\iref{class.bit}
is a member of the class,
and each such \grammarterm{member-declaration}
shall either
declare at least one member name of the class
or declare at least one unnamed bit-field.
\pnum
A \defn{data member} is a non-function member introduced by a
\grammarterm{member-declarator}.
A \defn{member function} is a member that is a function.
Nested types are classes\iref{class.name,class.nest} and
enumerations\iref{dcl.enum} declared in the class and arbitrary types
declared as members by use of a typedef declaration\iref{dcl.typedef}
or \grammarterm{alias-declaration}.
The enumerators of an unscoped enumeration\iref{dcl.enum} defined in the class
are members of the class.
\pnum
A data member or member function
may be declared \keyword{static} in its \grammarterm{member-declaration},
in which case it is a \defnadj{static}{member} (see~\ref{class.static})
(a \defnadj{static}{data member}\iref{class.static.data} or
\defnadj{static}{member function}\iref{class.static.mfct}, respectively)
of the class.
Any other data member or member function is a \defnadj{non-static}{member}
(a \defnadj{non-static}{data member} or
\defnadj{non-static}{member function}\iref{class.mfct.non.static}, respectively).
\begin{note}
A non-static data member of non-reference
type is a member subobject of a class object\iref{intro.object}.
\end{note}
\pnum
A member shall not be declared twice in the
\grammarterm{member-specification}, except that
\begin{itemize}
\item a nested class or member
class template can be declared and then later defined, and
\item an
enumeration can be introduced with an \grammarterm{opaque-enum-declaration} and later
redeclared with an \grammarterm{enum-specifier}{}.
\end{itemize}
\begin{note}
A single name can denote several member functions provided their types
are sufficiently different\iref{basic.scope.scope}.
\end{note}
\pnum
A redeclaration of a class member outside its class definition shall be
a definition,
an explicit specialization, or
an explicit instantiation\iref{temp.expl.spec,temp.explicit}.
The member shall not be a non-static data member.
\pnum
\indextext{completely defined}%
A \defn{complete-class context} of a class (template) is a
\begin{itemize}
\item function body\iref{dcl.fct.def.general},
\item default argument\iref{dcl.fct.default},
\item default template argument\iref{temp.param},
\item \grammarterm{noexcept-specifier}\iref{except.spec},
\item \grammarterm{function-contract-specifier}\iref{dcl.contract.func}, or
\item default member initializer
\end{itemize}
within the \grammarterm{member-specification} of the class or class template.
\begin{note}
A complete-class context of a nested class is also a complete-class
context of any enclosing class, if the nested class is defined within
the \grammarterm{member-specification} of the enclosing class.
\end{note}
\pnum
A class \tcode{C} is complete at a program point $P$
if the definition of \tcode{C} is reachable from $P$\iref{module.reach}
or if $P$ is in a complete-class context of \tcode{C}.
Otherwise, \tcode{C} is incomplete at $P$.
\pnum
If a \grammarterm{member-declaration} matches
the syntactic requirements of \grammarterm{friend-type-declaration},
it is a \grammarterm{friend-type-declaration}.
\pnum
In a \grammarterm{member-declarator},
an \tcode{=} immediately following the \grammarterm{declarator}
is interpreted as introducing a \grammarterm{pure-specifier}
if the \grammarterm{declarator-id} has function type,
otherwise it is interpreted as introducing
a \grammarterm{brace-or-equal-initializer}.
\begin{example}
\begin{codeblock}
struct S {
using T = void();
T * p = 0; // OK, \grammarterm{brace-or-equal-initializer}
virtual T f = 0; // OK, \grammarterm{pure-specifier}
};
\end{codeblock}
\end{example}
\pnum
In a \grammarterm{member-declarator} for a bit-field,
the \grammarterm{constant-expression} is parsed as
the longest sequence of tokens
that could syntactically form a \grammarterm{constant-expression}.
\begin{example}
\begin{codeblock}
int a;
const int b = 0;
struct S {
int x1 : 8 = 42; // OK, \tcode{"= 42"} is \grammarterm{brace-or-equal-initializer}
int x2 : 8 { 42 }; // OK, \tcode{"\{ 42 \}"} is \grammarterm{brace-or-equal-initializer}
int y1 : true ? 8 : a = 42; // OK, \grammarterm{brace-or-equal-initializer} is absent
int y2 : true ? 8 : b = 42; // error: cannot assign to \tcode{const int}
int y3 : (true ? 8 : b) = 42; // OK, \tcode{"= 42"} is \grammarterm{brace-or-equal-initializer}
int z : 1 || new int { 0 }; // OK, \grammarterm{brace-or-equal-initializer} is absent
};
\end{codeblock}
\end{example}
\pnum
A \grammarterm{brace-or-equal-initializer} shall appear only in the
declaration of a data member. (For static data members,
see~\ref{class.static.data}; for non-static data members,
see~\ref{class.base.init} and~\ref{dcl.init.aggr}).
A \grammarterm{brace-or-equal-initializer} for a non-static data member
\indextext{member!default initializer}%
specifies a \defn{default member initializer} for the member, and
shall not directly or indirectly cause the implicit definition of a
defaulted default constructor for the enclosing class or the
exception specification of that constructor.
An immediate invocation\iref{expr.const} that
is a potentially-evaluated subexpression\iref{intro.execution}
of a default member initializer
is neither evaluated nor checked for whether it
is a constant expression at the point where the subexpression appears.
\pnum
A member shall not be declared with the \keyword{extern}
\grammarterm{storage-class-specifier}. Within a class definition, a member shall not be declared with the \keyword{thread_local} \grammarterm{storage-class-specifier} unless also declared \keyword{static}.
\pnum
The \grammarterm{decl-specifier-seq} may be omitted in constructor, destructor,
and conversion function declarations only;
when declaring another kind of member the \grammarterm{decl-specifier-seq}
shall contain a \grammarterm{type-specifier} that is not a \grammarterm{cv-qualifier}.
The
\grammarterm{member-declarator-list} can be omitted only after a
\grammarterm{class-specifier} or an \grammarterm{enum-specifier} or in a
friend declaration\iref{class.friend}. A
\grammarterm{pure-specifier} shall be used only in the declaration of a
virtual function\iref{class.virtual}
that is not a friend declaration.
\pnum
The optional \grammarterm{attribute-specifier-seq} in a \grammarterm{member-declaration}
appertains to each of the entities declared by the \grammarterm{member-declarator}{s};
it shall not appear if the optional \grammarterm{member-declarator-list} is omitted.
\pnum
A \grammarterm{virt-specifier-seq} shall contain at most one of each
\grammarterm{virt-specifier}.
A \grammarterm{virt-specifier-seq}
shall appear only in the first declaration of a virtual member
function\iref{class.virtual}.
\pnum
\indextext{class object!member}%
The type of a non-static data member shall not be an
incomplete type\iref{term.incomplete.type},
an abstract class type\iref{class.abstract},
or a (possibly multidimensional) array thereof.
\begin{note}
In particular, a class \tcode{C} cannot contain
a non-static member of class \tcode{C},
but it can contain a pointer or reference to an object of class \tcode{C}.
\end{note}
\pnum
\begin{note}
See~\ref{expr.prim.id} for restrictions on the use of non-static data
members and non-static member functions.
\end{note}
\pnum
\begin{note}
The type of a non-static member function is an ordinary function type,
and the type of a non-static data member is an ordinary object type.
There are no special member function types or data member types.
\end{note}
\pnum
\begin{example}
A simple example of a class definition is
\begin{codeblock}
struct tnode {
char tword[20];
int count;
tnode* left;
tnode* right;
};
\end{codeblock}
which contains an array of twenty characters, an integer, and two
pointers to objects of the same type. Once this definition has been
given, the declaration
\begin{codeblock}
tnode s, *sp;
\end{codeblock}
declares \tcode{s} to be a \tcode{tnode} and \tcode{sp} to be a pointer
to a \tcode{tnode}. With these declarations, \tcode{sp->count} refers to
the \tcode{count} member of the object to which \tcode{sp} points;
\tcode{s.left} refers to the \tcode{left} subtree pointer of the object
\tcode{s}; and \tcode{s.right->tword[0]} refers to the initial character
of the \tcode{tword} member of the \tcode{right} subtree of \tcode{s}.
\end{example}
\pnum
\begin{note}
\indextext{layout!class object}%
Non-variant non-static data members of
non-zero size\iref{intro.object}
are allocated so that later
members have higher addresses within a class object\iref{expr.rel}.
Implementation alignment requirements can cause two adjacent members
not to be allocated immediately after each other; so can requirements
for space for managing virtual functions\iref{class.virtual} and
virtual base classes\iref{class.mi}.
\end{note}
\pnum
If \tcode{T} is the name of a class, then each of the following shall
have a name different from \tcode{T}:
\begin{itemize}
\item every static data member of class \tcode{T};
\item every member function of class \tcode{T};
\begin{note}
This restriction does not apply to constructors, which do not have
names\iref{class.ctor}.
\end{note}%
\item every member of class \tcode{T} that is itself a type;
\item every member template of class \tcode{T};
\item every enumerator of every member of class \tcode{T} that is an
unscoped enumeration type; and
\item every member of every anonymous union that is a member of class
\tcode{T}.
\end{itemize}
\pnum
In addition, if class \tcode{T} has a user-declared
constructor\iref{class.ctor}, every non-static data member of class
\tcode{T} shall have a name different from \tcode{T}.
\pnum
The \defn{common initial sequence} of two standard-layout struct\iref{class.prop}
types is the longest sequence of non-static data
members and bit-fields in declaration order, starting with the first
such entity in each of the structs, such that
\begin{itemize}
\item
corresponding entities
have layout-compatible types\iref{basic.types},
\item
corresponding entities have the same alignment requirements\iref{basic.align},
\item
if a \grammarterm{has-attribute-expression}\iref{cpp.cond}
is not \tcode{0} for the \tcode{no_unique_address} attribute,
then neither entity is declared with
the \tcode{no_unique_address} attribute\iref{dcl.attr.nouniqueaddr}, and
\item
either both entities are bit-fields with the same width
or neither is a bit-field.
\end{itemize}
\begin{example}
\begin{codeblock}
struct A { int a; char b; };
struct B { const int b1; volatile char b2; };
struct C { int c; unsigned : 0; char b; };
struct D { int d; char b : 4; };
struct E { unsigned int e; char b; };
\end{codeblock}
The common initial sequence of \tcode{A} and \tcode{B} comprises all members
of either class. The common initial sequence of \tcode{A} and \tcode{C} and
of \tcode{A} and \tcode{D} comprises the first member in each case.
The common initial sequence of \tcode{A} and \tcode{E} is empty.
\end{example}
\pnum
Two standard-layout struct\iref{class.prop} types are
\defnx{layout-compatible classes}{layout-compatible!class} if
their common initial sequence comprises all members and bit-fields of
both classes\iref{basic.types}.
\pnum
Two standard-layout unions are layout-compatible if they
have the same number of non-static data members and corresponding
non-static data members (in any order) have layout-compatible
types\iref{term.layout.compatible.type}.
\pnum
In a standard-layout union with an active member\iref{class.union}
of struct type \tcode{T1}, it is permitted to read a non-static
data member \tcode{m} of another union member of struct type \tcode{T2}
provided \tcode{m} is part of the common initial sequence of \tcode{T1} and \tcode{T2};
the behavior is as if the corresponding member of \tcode{T1} were nominated.
\begin{example}
\begin{codeblock}
struct T1 { int a, b; };
struct T2 { int c; double d; };
union U { T1 t1; T2 t2; };
int f() {
U u = { { 1, 2 } }; // active member is \tcode{t1}
return u.t2.c; // OK, as if \tcode{u.t1.a} were nominated
}
\end{codeblock}
\end{example}
\begin{note}
Reading a volatile object through a glvalue of non-volatile type has
undefined behavior\iref{dcl.type.cv}.
\end{note}
\pnum
If a standard-layout class object has any non-static data members, its address
is the same as the address of its first non-static data member
if that member is not a bit-field. Its
address is also the same as the address of each of its base class subobjects.
\begin{note}
There can therefore be unnamed padding within a standard-layout struct object
inserted by an implementation, but
not at its beginning, as necessary to achieve appropriate alignment.
\end{note}
\begin{note}
The object and its first subobject are
pointer-interconvertible\iref{basic.compound,expr.static.cast}.
\end{note}