forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.tex
1821 lines (1593 loc) · 63.1 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}}
\pnum
\indextext{\idxcode{\{\}}!class declaration}%
\indextext{\idxcode{\{\}}!class definition}%
\indextext{type!class~and}%
\indextext{object~class|seealso{class~object}}%
A class is a type.
\indextext{name~class|see{class~name}}%
Its name becomes a \grammarterm{class-name}~(\ref{class.name}) within its
scope.
\begin{bnf}
\nontermdef{class-name}\br
identifier\br
simple-template-id
\end{bnf}
\grammarterm{Class-specifier}{s} and
\grammarterm{elaborated-type-specifier}{s}~(\ref{dcl.type.elab}) are used to
make \grammarterm{class-name}{s}. 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{\{} member-specification\opt{} \terminal{\}}
\end{bnf}
\begin{bnf}
\nontermdef{class-head}\br
class-key attribute-specifier-seq\opt class-head-name class-virt-specifier\opt base-clause\opt\br
class-key attribute-specifier-seq\opt base-clause\opt
\end{bnf}
\begin{bnf}
\nontermdef{class-head-name}\br
nested-name-specifier\opt class-name
\end{bnf}
\begin{bnf}
\nontermdef{class-virt-specifier}\br
\terminal{final}
\end{bnf}
\begin{bnf}
\nontermdef{class-key}\br
\terminal{class}\br
\terminal{struct}\br
\terminal{union}
\end{bnf}
A \grammarterm{class-specifier} whose
\grammarterm{class-head} omits the
\grammarterm{class-head-name} defines an unnamed class. \begin{note} An unnamed class thus can't
be \tcode{final}. \end{note}
\pnum
A \grammarterm{class-name} is inserted into the scope in which it is
declared immediately after the \grammarterm{class-name} is seen. The
\grammarterm{class-name} is also inserted into the scope of the class
itself; this is known as the \grammarterm{injected-class-name}.
\indextext{\idxgram{injected-class-name}}%
For purposes of access checking, the injected-class-name is treated as
if it were a public member name.
\indextext{definition!class}%
A \grammarterm{class-specifier} is commonly referred to as a class
definition.
\indextext{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 class is marked with the \grammarterm{class-virt-specifier} \tcode{final} and it appears
as a \grammarterm{base-type-specifier} in a \grammarterm{base-clause}
(Clause~\ref{class.derived}), the program is ill-formed. Whenever a
\grammarterm{class-key} is followed by a \grammarterm{class-head-name}, the
\grammarterm{identifier} \tcode{final}, and a colon or left brace, \tcode{final} is
interpreted as a \grammarterm{class-virt-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 final : C{}; // OK: definition of nested class \tcode{B},
// not declaration of a bit-field member \tcode{final}
};
\end{codeblock}
\end{example}
\pnum
Complete objects and member subobjects of class type shall have nonzero
size.\footnote{Base class subobjects are not so constrained.}
\begin{note}
Class objects can be assigned, passed as arguments to functions, and
returned by functions (except objects of classes for which copying or moving has
been restricted; see~\ref{class.copy}). Other plausible operators, such
as equality comparison, can be defined by the user; see~\ref{over.oper}.
\end{note}
\pnum
\indextext{\idxcode{struct}!\tcode{class}~versus}%
\indextext{structure}%
\indextext{\idxcode{union}!\tcode{class}~versus}%
A \term{union} is a class defined with the \grammarterm{class-key}
\tcode{union};
\indextext{access control!\idxcode{union} default member}%
it holds at most one data member at a time~(\ref{class.union}).
\begin{note}
Aggregates of class type are described in~\ref{dcl.init.aggr}.
\end{note}
\indextext{class!trivial}%
\indextext{trivial~class}%
\indextext{class!trivially copyable}%
\pnum
A \defn{trivially copyable class} is a class:
\begin{itemize}
\item where each copy constructor, move constructor, copy assignment operator,
and move assignment operator~(\ref{class.copy}, \ref{over.ass})
is either deleted or trivial,
\item that has at least one non-deleted copy constructor, move constructor,
copy assignment operator, or move assignment operator, and
\item that has a trivial, non-deleted destructor~(\ref{class.dtor}).
\end{itemize}
A \term{trivial class} is a class that is trivially copyable and
has one or more default constructors~(\ref{class.ctor}),
all of which are either trivial or deleted and
at least one of which is not deleted.
\begin{note} In particular, a trivially copyable or trivial class does not have
virtual functions or virtual base classes.\end{note}
\indextext{class!standard-layout}%
\indextext{standard-layout~class}%
\pnum
A class \tcode{S} is a \grammarterm{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~(\ref{class.virtual}) and no
virtual base classes~(\ref{class.mi}),
\item has the same access control (Clause~\ref{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 (defined below)
as a base class.\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~(\ref{expr.eq}).}
\end{itemize}
$M(\mathtt{X})$ is defined as follows:
\begin{itemize}
\item If \tcode{X} is a non-union class type, the set $M(\mathtt{X})$ is
empty if \tcode{X} has no (possibly inherited (Clause~\ref{class.derived}))
non-static data members;
otherwise, it consists of the type of the first non-static data member
of \tcode{X} (where said member may be an anonymous union), $\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$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}
\begin{note} $M(\mathtt{X})$ is the set of the types of all non-base-class subobjects
that are guaranteed in a standard-layout class to be at a zero offset
in \tcode{X}. \end{note}
\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}
\indextext{struct!standard-layout}%
\indextext{standard-layout~struct}%
\indextext{union!standard-layout}%
\indextext{standard-layout~union}%
\pnum
A \grammarterm{standard-layout struct} is a standard-layout class
defined with the \grammarterm{class-key} \tcode{struct} or the
\grammarterm{class-key} \tcode{class}.
A \grammarterm{standard-layout union} is a standard-layout class
defined with the
\grammarterm{class-key} \tcode{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}.\end{note}
\pnum
\indextext{POD struct}\indextext{POD class}\indextext{POD union}%
A \term{POD struct}\footnote{The acronym POD stands for ``plain old data''.}
is a non-union class that is both a trivial class and a
standard-layout class, and has no non-static data members of type non-POD struct,
non-POD union (or array of such types). Similarly, a
\term{POD union} is a union that is both a trivial class and a standard-layout
class, and has no non-static data members of type non-POD struct, non-POD
union (or array of such types). A \term{POD class} is a
class that is either a POD struct or a POD union.
\begin{example}
\begin{codeblock}
struct N { // neither trivial nor standard-layout
int i;
int j;
virtual ~N();
};
struct T { // trivial but not standard-layout
int i;
private:
int j;
};
struct SL { // standard-layout but not trivial
int i;
int j;
~SL();
};
struct POD { // both trivial and standard-layout
int i;
int j;
};
\end{codeblock}
\end{example}
\pnum
If a \grammarterm{class-head-name} contains a \grammarterm{nested-name-specifier},
the \grammarterm{class-specifier} shall refer to a class that was
previously declared directly in the class or namespace to which the
\grammarterm{nested-name-specifier} refers,
or in an element of the inline namespace set~(\ref{namespace.def}) of that namespace
(i.e., not merely inherited or
introduced by a \grammarterm{using-declaration}), and the
\grammarterm{class-specifier} shall appear in a namespace enclosing the
previous declaration.
In such cases, the \grammarterm{nested-name-specifier} of the
\grammarterm{class-head-name} of the
definition shall not begin with a \grammarterm{decltype-specifier}.
\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 an overloaded (Clause~\ref{over}) function \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}%
\indextext{class~name!scope~of}%
A class declaration introduces the class name into the scope where
\indextext{name~hiding!class definition}%
it is declared and hides any
class, variable, function, or other declaration of that name in an
enclosing scope~(\ref{basic.scope}). If a class name is declared in a
scope where a variable, function, or enumerator of the same name is also
declared, then when both declarations are in scope, the class can be
referred to only using an
\grammarterm{elaborated-type-specifier}~(\ref{basic.lookup.elab}).
\begin{example}
\begin{codeblock}
struct stat {
// ...
};
stat gstat; // use plain \tcode{stat} to
// define variable
int stat(struct stat*); // redeclare \tcode{stat} as function
void f() {
struct stat* ps; // \tcode{struct} prefix needed
// to name \tcode{struct stat}
stat(ps); // call \tcode{stat()}
}
\end{codeblock}
\end{example}
\indextext{class~name!elaborated}%
\indextext{declaration!forward class}%
A \grammarterm{declaration} consisting solely of \grammarterm{class-key
identifier;} is either a redeclaration of the name in the current scope
or a forward declaration of the identifier as a class name. It
introduces the class name into the current scope.
\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}
\begin{note}
Such declarations allow definition of classes that refer to each other.
\indextext{example!friend}%
\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 \tcode{friend}s 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}~(\ref{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 if a class of the elaborated
name is in scope the elaborated name will refer to it.
\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}. For 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 \tcode{class} \tcode{A} must be used to refer to the
class. Such artistry with names can be confusing and is best avoided.
\end{note}
\pnum
\indextext{class~name!\idxcode{typedef}}%
A \grammarterm{typedef-name}~(\ref{dcl.typedef}) that names a class type,
or a cv-qualified version thereof, is also a \grammarterm{class-name}. If a
\grammarterm{typedef-name} that names a cv-qualified class type is used
where a \grammarterm{class-name} is required, the cv-qualifiers are
ignored. A \grammarterm{typedef-name} shall not be used as the
\grammarterm{identifier} in a \grammarterm{class-head}.
\rSec1[class.mem]{Class members}%
\indextext{declaration!member}%
\indextext{data~member|see{member}}
\begin{bnf}
\nontermdef{member-specification}\br
member-declaration member-specification\opt\br
access-specifier \terminal{:} member-specification\opt
\end{bnf}
\begin{bnf}
\nontermdef{member-declaration}\br
attribute-specifier-seq\opt decl-specifier-seq\opt member-declarator-list\opt{} \terminal{;}\br
function-definition\br
using-declaration\br
static_assert-declaration\br
template-declaration\br
deduction-guide\br
alias-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 virt-specifier-seq\opt pure-specifier\opt\br
declarator brace-or-equal-initializer\opt\br
identifier\opt attribute-specifier-seq\opt{} \terminal{:} constant-expression
\end{bnf}
\begin{bnf}
\nontermdef{virt-specifier-seq}\br
virt-specifier\br
virt-specifier-seq virt-specifier
\end{bnf}
\begin{bnf}
\nontermdef{virt-specifier}\br
\terminal{override}\br
\terminal{final}
\end{bnf}
\begin{bnf}
\nontermdef{pure-specifier}\br
\terminal{= 0}
\end{bnf}
\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.
Members of a class are data members, member
functions~(\ref{class.mfct}), nested types, enumerators,
and member templates~(\ref{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~(\ref{class.friend}),
\item a \grammarterm{static_assert-declaration},
\item a \grammarterm{using-declaration}~(\ref{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~(\ref{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~(\ref{class.name},~\ref{class.nest}) and
enumerations~(\ref{dcl.enum}) declared in the class and arbitrary types
declared as members by use of a typedef declaration~(\ref{dcl.typedef})
or \grammarterm{alias-declaration}.
The enumerators of an unscoped enumeration~(\ref{dcl.enum}) defined in the class
are members of the class.
\indextext{member!static}%
\indextext{member function!static}%
\indextext{data member!static}%
\pnum
A data member or member function
may be declared \tcode{static} in its \grammarterm{member-declaration},
in which case it is a \defn{static member} (see~\ref{class.static})
(a \defn{static data member}~(\ref{class.static.data}) or
\defn{static member function}~(\ref{class.static.mfct}), respectively)
of the class.
Any other data member or member function is a \defn{non-static member}
(a \defn{non-static data member} or
\defn{non-static member function}~(\ref{class.mfct.non-static}), respectively).
\begin{note} A non-static data member of non-reference
type is a member subobject of a class object~(\ref{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 (Clause~\ref{over}).
\end{note}
\pnum
\indextext{completely~defined}%
A class is considered a completely-defined object
type~(\ref{basic.types}) (or complete type) at the closing \tcode{\}} of
the \grammarterm{class-specifier}.
Within the class
\grammarterm{member-specification}, the class is regarded as complete
within function bodies, default arguments,
\grammarterm{noexcept-specifier}{s}, and
default member initializers
(including such things in nested classes).
Otherwise it is regarded as incomplete within its own class
\grammarterm{member-specification}.
\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
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.
\pnum
A member shall not be declared with the \tcode{extern}
\nonterminal{storage-class-specifier}. Within a class definition, a member shall not be declared with the \tcode{thread_local} \nonterminal{storage-class-specifier} unless also declared \tcode{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
\tcode{friend} declaration~(\ref{class.friend}). A
\grammarterm{pure-specifier} shall be used only in the declaration of a
virtual function~(\ref{class.virtual})
that is not a \tcode{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 declaration of a virtual member
function~(\ref{class.virtual}).
\pnum
\indextext{class~object!member}%
Non-static data members shall not have
incomplete types. In particular, a class \tcode{C} shall not contain a
non-static member of class \tcode{C}, but it can contain a pointer or
reference to an object of class \tcode{C}.
\pnum
\begin{note}
See~\ref{expr.prim} 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
\indextext{example!class~definition}%
\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
\indextext{layout!class~object}%
Non-static data members of a (non-union) class
with the same access control (Clause~\ref{class.access})
are allocated so that later
members have higher addresses within a class object.
\indextext{allocation!unspecified}%
The order of allocation of non-static data members
with different access control
is unspecified (Clause~\ref{class.access}).
Implementation alignment requirements might cause two adjacent members
not to be allocated immediately after each other; so might requirements
for space for managing virtual functions~(\ref{class.virtual}) and
virtual base classes~(\ref{class.mi}).
\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~(\ref{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 enumerated 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~(\ref{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 (Clause~\ref{class})
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 corresponding entities
have layout-compatible types and either neither entity is a bit-field or
both are bit-fields with the same width.
\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 (Clause~\ref{class}) types are
\defnx{layout-compatible classes}{layout-compatible!class} if
their common initial sequence comprises all members and bit-fields of
both classes~(\ref{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~(\ref{basic.types}).
\pnum
In a standard-layout union with an active member~(\ref{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 non-volatile glvalue has
undefined behavior (\ref{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. Otherwise, its
address is the same as the address of its first base class subobject (if any).
\begin{note}
There might therefore be unnamed padding within a standard-layout struct object, but
not at its beginning, as necessary to achieve appropriate alignment.
\end{note}
\begin{note}
The object and its first subobject are
pointer-interconvertible~(\ref{basic.compound}, \ref{expr.static.cast}).
\end{note}
\rSec2[class.mfct]{Member functions}%
\indextext{member function!class}
\pnum
\indextext{member function!inline}%
\indextext{definition!member~function}%
A member function may be defined~(\ref{dcl.fct.def}) in its class
definition, in which case it is an \term{inline} member
function~(\ref{dcl.fct.spec}), or it may be defined outside of its class
definition if it has already been declared but not defined in its class
definition. A member function definition that appears outside of the
class definition shall appear in a namespace scope enclosing the class
definition. Except for member function definitions that appear outside
of a class definition, and except for explicit specializations of member
functions of class templates and member function
templates~(\ref{temp.spec}) appearing outside of the class definition, a
member function shall not be redeclared.
\pnum
An inline member function (whether static or non-static) may
also be defined outside of its class definition provided either its
declaration in the class definition or its definition outside of the
class definition declares the function as \tcode{inline} or \tcode{constexpr}.
\begin{note}
Member functions of a class in namespace scope have the linkage of that class.
Member functions of a local class~(\ref{class.local}) have no linkage.
See~\ref{basic.link}.
\end{note}
\pnum
\begin{note}
There can be at most one definition of a non-inline member function in
a program. There may be more than one
\tcode{inline} member function definition in a program.
See~\ref{basic.def.odr} and~\ref{dcl.fct.spec}.
\end{note}
\pnum
\indextext{operator!scope~resolution}%
If the definition of a member function is lexically outside its class
definition, the member function name shall be qualified by its class
name using the \tcode{::} operator.
\begin{note}
A name used in a member function definition (that is, in the
\grammarterm{parameter-declaration-clause} including the default
arguments~(\ref{dcl.fct.default}) or in the member function body) is looked up
as described in~\ref{basic.lookup}.
\end{note}
\begin{example}
\begin{codeblock}
struct X {
typedef int T;
static T count;
void f(T);
};
void X::f(T t = count) { }
\end{codeblock}
The member function \tcode{f} of class \tcode{X} is defined in global
scope; the notation \tcode{X::f} specifies that the function \tcode{f}
is a member of class \tcode{X} and in the scope of class \tcode{X}. In
the function definition, the parameter type \tcode{T} refers to the
typedef member \tcode{T} declared in class \tcode{X} and the default
argument \tcode{count} refers to the static data member \tcode{count}
declared in class \tcode{X}.
\end{example}
\pnum
\begin{note}
A \tcode{static} local variable or local type in a member function always refers to
the same entity, whether or not the member function is \tcode{inline}.
\end{note}
\pnum
Previously declared member functions may be mentioned in \tcode{friend} declarations.
\pnum
\indextext{local~class!member~function~in}%
Member functions of a local class shall be defined inline in their class
definition, if they are defined at all.
\pnum
\begin{note}
A member function can be declared (but not defined) using a typedef for
a function type. The resulting member function has exactly the same type
as it would have if the function declarator were provided explicitly,
see~\ref{dcl.fct}. For example,
\begin{codeblock}
typedef void fv();
typedef void fvc() const;
struct S {
fv memfunc1; // equivalent to: \tcode{void memfunc1();}
void memfunc2();
fvc memfunc3; // equivalent to: \tcode{void memfunc3() const;}
};
fv S::* pmfv1 = &S::memfunc1;
fv S::* pmfv2 = &S::memfunc2;
fvc S::* pmfv3 = &S::memfunc3;
\end{codeblock}
Also see~\ref{temp.arg}.
\end{note}
\rSec2[class.mfct.non-static]{Non-static member functions}%
\indextext{member function!non-static}
\pnum
A non-static member function may be called for an object of
its class type, or for an object of a class derived
(Clause~\ref{class.derived}) from its class type, using the class member
access syntax~(\ref{expr.ref},~\ref{over.match.call}). A non-static
member function may also be called directly using the function call
syntax~(\ref{expr.call},~\ref{over.match.call}) from within
the body of a member function of its class or of a class derived from
its class.
\pnum
\indextext{member function!call undefined}%
If a non-static member function of a class \tcode{X} is called for an
object that is not of type \tcode{X}, or of a type derived from
\tcode{X}, the behavior is undefined.
\pnum
When an \grammarterm{id-expression}~(\ref{expr.prim}) that is not part of a
class member access syntax~(\ref{expr.ref}) and not used to form a
pointer to member~(\ref{expr.unary.op}) is used in
a member of class \tcode{X} in a context where \tcode{this} can be
used~(\ref{expr.prim.this}),
if name
lookup~(\ref{basic.lookup}) resolves the name in the
\grammarterm{id-expression} to a non-static non-type member of some class
\tcode{C},
and if either the \grammarterm{id-expression} is potentially evaluated or
\tcode{C} is \tcode{X} or a base class of \tcode{X},
the \grammarterm{id-expression} is transformed into a class
member access expression~(\ref{expr.ref}) using
\tcode{(*this)}~(\ref{class.this}) as the \grammarterm{postfix-expression}
to the left of the \tcode{.} operator.
\begin{note}
If \tcode{C} is not \tcode{X} or a base class of \tcode{X}, the class
member access expression is ill-formed.
\end{note}
Similarly during name lookup, when an
\grammarterm{unqualified-id}~(\ref{expr.prim}) used in the definition of a
member function for class \tcode{X} resolves to a static member,
an enumerator or a nested type of class \tcode{X} or of a base class of
\tcode{X}, the \grammarterm{unqualified-id} is transformed into a
\grammarterm{qualified-id}~(\ref{expr.prim}) in which the
\grammarterm{nested-name-specifier} names the class of the member function.
These transformations do not apply in the
template definition context~(\ref{temp.dep.type}).
\indextext{example!member~function}%
\begin{example}
\begin{codeblock}
struct tnode {
char tword[20];
int count;
tnode* left;
tnode* right;
void set(const char*, tnode* l, tnode* r);
};
void tnode::set(const char* w, tnode* l, tnode* r) {
count = strlen(w)+1;
if (sizeof(tword)<=count)
perror("tnode string too long");
strcpy(tword,w);
left = l;
right = r;
}
void f(tnode n1, tnode n2) {
n1.set("abc",&n2,0);
n2.set("def",0,0);
}
\end{codeblock}
In the body of the member function \tcode{tnode::set}, the member names
\tcode{tword}, \tcode{count}, \tcode{left}, and \tcode{right} refer to
members of the object for which the function is called. Thus, in the
call \tcode{n1.set("abc",\&n2,0)}, \tcode{tword} refers to
\tcode{n1.tword}, and in the call \tcode{n2.set("def",0,0)}, it refers
to \tcode{n2.tword}. The functions \tcode{strlen}, \tcode{perror}, and
\tcode{strcpy} are not members of the class \tcode{tnode} and should be
declared elsewhere.\footnote{See, for example, \tcode{<cstring>}~(\ref{c.strings}).}
\end{example}
\pnum
A non-static member function may be declared \tcode{const},
\tcode{volatile}, or \tcode{const} \tcode{volatile}. These
\grammarterm{cv-qualifiers} affect the type of the \tcode{this}
pointer~(\ref{class.this}). They also affect the function