Repository navigation
Expand file tree
/
Copy pathintro.tex
More file actions
1733 lines (1564 loc) · 74.1 KB
/
Copy pathintro.tex
File metadata and controls
1733 lines (1564 loc) · 74.1 KB
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[intro.scope]{Scope}
\pnum
\indextext{scope|(}%
This document specifies requirements for implementations
of the \Cpp programming language. The first such requirement is that
they implement the language, so this document also
defines \Cpp. Other requirements and relaxations of the first
requirement appear at various places within this document.
\pnum
\Cpp is a general purpose programming language based on the C
programming language as described in ISO/IEC 9899:2011
\doccite{Programming languages --- C} (hereinafter referred to as the
\defnx{C standard}{C!standard}). In addition to
the facilities provided by C, \Cpp provides additional data types,
classes, templates, exceptions, namespaces, operator
overloading, function name overloading, references, free store
management operators, and additional library facilities.%
\indextext{scope|)}
\indextext{normative references|see{references, normative}}%
\rSec0[intro.refs]{Normative references}
\pnum
\indextext{references!normative|(}%
The following documents are referred to in the text
in such a way that some or all of their content
constitutes requirements
of this document. For dated references, only the edition cited applies.
For undated references, the latest edition of the referenced document
(including any amendments) applies.
\begin{itemize}
\item Ecma International, \doccite{ECMAScript Language Specification},
Standard Ecma-262, third edition, 1999.
\item ISO/IEC 2382 (all parts), \doccite{Information technology ---
Vocabulary}
\item ISO/IEC 9899:2011, \doccite{Programming languages --- C}
\item ISO/IEC 9899:2011/Cor.1:2012(E), \doccite{Programming languages --- C,
Technical Corrigendum 1}
\item ISO/IEC 9945:2003, \doccite{Information Technology --- Portable
Operating System Interface (POSIX)}
\item ISO/IEC 10646-1:1993, \doccite{Information technology ---
Universal Multiple-Octet Coded Character Set (UCS) --- Part 1:
Architecture and Basic Multilingual Plane}
\item ISO/IEC 10967-1:2012, \doccite{Information technology ---
Language independent arithmetic --- Part 1:
Integer and floating point arithmetic}
\item ISO/IEC/IEEE 60559:2011, \doccite{Information technology ---
Microprocessor Systems --- Floating-Point arithmetic}
\item ISO 80000-2:2009, \doccite{Quantities and units ---
Part 2: Mathematical signs and symbols
to be used in the natural sciences and technology}
\end{itemize}
\pnum
The library described in Clause 7 of ISO/IEC 9899:2011
is hereinafter called the
\defnx{C standard library}{C!standard library}.%
\footnote{With the qualifications noted in Clauses~\ref{\firstlibchapter}
through~\ref{\lastlibchapter} and in~\ref{diff.library}, the C standard
library is a subset of the \Cpp standard library.}
\pnum
The operating system interface described in ISO/IEC 9945:2003 is
hereinafter called \defn{POSIX}.
\pnum
The ECMAScript Language Specification described in Standard Ecma-262 is
hereinafter called \defn{ECMA-262}.
\pnum
The arithmetic specification described in ISO/IEC 10967-1:2012 is
hereinafter called \defn{LIA-1}.
\indextext{references!normative|)}
\rSec0[intro.defs]{Terms and definitions}
\pnum
\indextext{definitions|(}%
For the purposes of this document,
the terms and definitions
given in ISO/IEC 2382-1:1993,
the terms, definitions, and symbols
given in ISO 80000-2:2009,
and the following apply.
\pnum
ISO and IEC maintain terminological databases
for use in standardization
at the following addresses:
\begin{itemize}
\item IEC Electropedia: available at \url{http://www.electropedia.org/}
\item ISO Online browsing platform: available at \url{http://www.iso.org/obp}
\end{itemize}
\pnum
\ref{definitions}
defines additional terms that are used only in Clauses~\ref{library}
through~\ref{\lastlibchapter} and Annex~\ref{depr}.
\pnum
Terms that are used only in a small portion of this document
are defined where they are used and italicized where they are
defined.
\def\definition{\definitionx{\section}}%
\indexdefn{access}%
\definition{access}{defns.access}
\defncontext{execution-time action} to read or modify the value of an object
\indexdefn{argument}%
\indexdefn{argument!function call expression}
\definition{argument}{defns.argument}
\defncontext{function call expression} expression in the
comma-separated list bounded by the parentheses~(\ref{expr.call})
\indexdefn{argument}%
\indexdefn{argument!function-like macro}%
\definition{argument}{defns.argument.macro}
\defncontext{function-like macro} sequence of preprocessing tokens in the
comma-separated list bounded by the parentheses~(\ref{cpp.replace})
\indexdefn{argument}%
\indexdefn{argument!throw expression}%
\definition{argument}{defns.argument.throw}
\defncontext{throw expression} the operand of \tcode{throw}~(\ref{expr.throw})
\indexdefn{argument}%
\indexdefn{argument!template instantiation}%
\definition{argument}{defns.argument.templ}
\defncontext{template instantiation}
\grammarterm{constant-expression},
\grammarterm{type-id}, or
\grammarterm{id-expression} in the comma-separated
list bounded by the angle brackets~(\ref{temp.arg})
\indexdefn{block}%
\definition{block}{defns.block}
a thread of execution that blocks is waiting for some condition (other than
for the implementation to execute its execution steps) to be satisfied before
it can continue execution past the blocking operation
\indexdefn{behavior!conditionally-supported}%
\definition{conditionally-supported}{defns.cond.supp}
program construct that an implementation is not required to support\\
\begin{note} Each implementation documents all conditionally-supported
constructs that it does not support.\end{note}
\indexdefn{message!diagnostic}%
\definition{diagnostic message}{defns.diagnostic}
message belonging to an \impldef{diagnostic message} subset of the
implementation's output messages
\indexdefn{type!dynamic}%
\definition{dynamic type}{defns.dynamic.type}
\defncontext{glvalue} type of the most derived object~(\ref{intro.object}) to which the
glvalue refers\\
\begin{example}
If a pointer~(\ref{dcl.ptr}) \tcode{p} whose static type is ``pointer to
class \tcode{B}'' is pointing to an object of class \tcode{D}, derived
from \tcode{B} (Clause~\ref{class.derived}), the dynamic type of the
expression \tcode{*p} is ``\tcode{D}''. References~(\ref{dcl.ref}) are
treated similarly.
\end{example}
\indexdefn{type!dynamic}%
\definition{dynamic type}{defns.dynamic.type.prvalue}
\defncontext{prvalue} static type of the prvalue expression
\indexdefn{program!ill-formed}%
\definition{ill-formed program}{defns.ill.formed}
program that is not well-formed~(\ref{defns.well.formed})
\indexdefn{behavior!implementation-defined}%
\definition{implementation-defined behavior}{defns.impl.defined}
behavior, for a well-formed program construct and correct data, that
depends on the implementation and that each implementation documents
\indexdefn{limits!implementation}%
\definition{implementation limits}{defns.impl.limits}
restrictions imposed upon programs by the implementation
\indexdefn{behavior!locale-specific}%
\definition{locale-specific behavior}{defns.locale.specific}
behavior that depends on local conventions of nationality, culture, and
language that each implementation documents
\indexdefn{character!multibyte}%
\definition{multibyte character}{defns.multibyte}
sequence of one or more bytes representing a member of the extended
character set of either the source or the execution environment\\
\begin{note} The
extended character set is a superset of the basic character
set~(\ref{lex.charset}).\end{note}
\indexdefn{parameter}%
\indexdefn{parameter!function}%
\indexdefn{parameter!catch clause}%
\definition{parameter}{defns.parameter}
\defncontext{function or catch clause} object or reference declared as part of a function declaration or
definition or in the catch clause of an exception handler that
acquires a value on entry to the function or handler
\indexdefn{parameter}%
\indexdefn{parameter!function-like macro}%
\definition{parameter}{defns.parameter.macro}
\defncontext{function-like macro} identifier from
the comma-separated list bounded by the parentheses immediately
following the macro name
\indexdefn{parameter}%
\indexdefn{parameter!template}%
\definition{parameter}{defns.parameter.templ}
\defncontext{template} member of a \grammarterm{template-parameter-list}
\indexdefn{signature}%
\definition{signature}{defns.signature}
\defncontext{function} name, parameter type list~(\ref{dcl.fct}), and enclosing namespace (if any)\\
\begin{note} Signatures are used as a basis for
name mangling and linking.\end{note}
\indexdefn{signature}%
\definition{signature}{defns.signature.templ}
\defncontext{function template} name, parameter type list~(\ref{dcl.fct}), enclosing namespace (if any),
return type, and template parameter list
\indexdefn{signature}%
\definition{signature}{defns.signature.spec}
\defncontext{function template specialization} signature of the template of which it is a specialization
and its template arguments (whether explicitly specified or deduced)
\indexdefn{signature}%
\definition{signature}{defns.signature.member}
\defncontext{class member function} name, parameter type list~(\ref{dcl.fct}), class of which the
function is a member, \cv-qualifiers (if any),
and \grammarterm{ref-qualifier} (if any)
\indexdefn{signature}%
\definition{signature}{defns.signature.member.templ}
\defncontext{class member function template} name, parameter type list~(\ref{dcl.fct}), class of which the
function is a member, \cv-qualifiers (if any),
\grammarterm{ref-qualifier} (if any), return type (if any), and template parameter list
\indexdefn{signature}%
\definition{signature}{defns.signature.member.spec}
\defncontext{class member function template specialization} signature of the member function template
of which it is a specialization and its template arguments (whether explicitly specified or deduced)
\indexdefn{type!static}%
\definition{static type}{defns.static.type}
type of an expression~(\ref{basic.types}) resulting from
analysis of the program without considering execution semantics\\
\begin{note} The
static type of an expression depends only on the form of the program in
which the expression appears, and does not change while the program is
executing. \end{note}
\indexdefn{unblock}%
\definition{unblock}{defns.unblock}
satisfy a condition that one or more blocked threads of execution are waiting for
\indexdefn{behavior!undefined}%
\definition{undefined behavior}{defns.undefined}
behavior for which this International Standard
imposes no requirements\\
\begin{note} Undefined behavior may be expected when
this International Standard omits any explicit
definition of behavior or when a program uses an erroneous construct or erroneous data.
Permissible undefined behavior ranges
from ignoring the situation completely with unpredictable results, to
behaving during translation or program execution in a documented manner
characteristic of the environment (with or without the issuance of a
diagnostic message), to terminating a translation or execution (with the
issuance of a diagnostic message). Many erroneous program constructs do
not engender undefined behavior; they are required to be diagnosed.
Evaluation of a constant expression never exhibits behavior explicitly
specified as undefined~(\ref{expr.const}).
\end{note}
\indexdefn{behavior!unspecified}%
\definition{unspecified behavior}{defns.unspecified}
behavior, for a well-formed program construct and correct data, that
depends on the implementation\\
\begin{note} The implementation is not required to
document which behavior occurs. The range of
possible behaviors is usually delineated by this International Standard.
\end{note}
\indexdefn{program!well-formed}%
\definition{well-formed program}{defns.well.formed}
\Cpp program constructed according to the syntax rules, diagnosable
semantic rules, and the one-definition rule~(\ref{basic.def.odr}).%
\indextext{definitions|)}
\rSec0[intro]{General principles}
\indextext{diagnostic message|see{message, diagnostic}}%
\indexdefn{conditionally-supported behavior|see{behavior, con\-ditionally-supported}}%
\indextext{dynamic type|see{type, dynamic}}%
\indextext{static type|see{type, static}}%
\indextext{ill-formed program|see{program, ill-formed}}%
\indextext{well-formed program|see{program, well-formed}}%
\indextext{implementation-defined behavior|see{behavior, im\-plementation-defined}}%
\indextext{undefined behavior|see{behavior, undefined}}%
\indextext{unspecified behavior|see{behavior, unspecified}}%
\indextext{implementation limits|see{limits, implementation}}%
\indextext{locale-specific behavior|see{behavior, locale-spe\-cific}}%
\indextext{multibyte character|see{character, multibyte}}%
\indextext{object|seealso{object model}}%
\indextext{subobject|seealso{object model}}%
\indextext{derived class!most|see{most derived class}}%
\indextext{derived object!most|see{most derived object}}%
\indextext{program execution!as-if rule|see{as-if rule}}%
\indextext{observable behavior|see{behavior, observable}}%
\indextext{precedence of operator|see{operator, precedence of}}%
\indextext{order of evaluation in expression|see{expression, order of evaluation of}}%
\indextext{atomic operations|see{operation, atomic}}%
\indextext{multiple threads|see{threads, multiple}}%
\rSec1[intro.compliance]{Implementation compliance}
\pnum
\indextext{conformance requirements|(}%
\indextext{conformance requirements!general|(}%
The set of
\defn{diagnosable rules}
consists of all syntactic and semantic rules in this International
Standard except for those rules containing an explicit notation that
``no diagnostic is required'' or which are described as resulting in
``undefined behavior''.
\pnum
\indextext{conformance requirements!method of description}%
Although this International Standard states only requirements on \Cpp
implementations, those requirements are often easier to understand if
they are phrased as requirements on programs, parts of programs, or
execution of programs. Such requirements have the following meaning:
\begin{itemize}
\item
If a program contains no violations of the rules in this
International Standard, a conforming implementation shall,
within its resource limits, accept and correctly execute\footnote{``Correct execution'' can include undefined behavior, depending on
the data being processed; see Clause~\ref{intro.defs} and~\ref{intro.execution}.}
that program.
\item
\indextext{message!diagnostic}%
If a program contains a violation of any diagnosable rule or an occurrence
of a construct described in this International Standard as ``conditionally-supported'' when
the implementation does not support that construct, a conforming implementation
shall issue at least one diagnostic message.
\item
\indextext{behavior!undefined}%
If a program contains a violation of a rule for which no diagnostic
is required, this International Standard places no requirement on
implementations with respect to that program.
\end{itemize}
\begin{note}
During template argument deduction and substitution,
certain constructs that in other contexts require a diagnostic
are treated differently;
see~\ref{temp.deduct}.
\end{note}
\pnum
\indextext{conformance requirements!library|(}%
\indextext{conformance requirements!classes}%
\indextext{conformance requirements!class templates}%
For classes and class templates, the library Clauses specify partial
definitions. Private members~(Clause~\ref{class.access}) are not
specified, but each implementation shall supply them to complete the
definitions according to the description in the library Clauses.
\pnum
For functions, function templates, objects, and values, the library
Clauses specify declarations. Implementations shall supply definitions
consistent with the descriptions in the library Clauses.
\pnum
The names defined in the library have namespace
scope~(\ref{basic.namespace}). A \Cpp translation
unit~(\ref{lex.phases}) obtains access to these names by including the
appropriate standard library header~(\ref{cpp.include}).
\pnum
The templates, classes, functions, and objects in the library have
external linkage~(\ref{basic.link}). The implementation provides
definitions for standard library entities, as necessary, while combining
translation units to form a complete \Cpp program~(\ref{lex.phases}).%
\indextext{conformance requirements!library|)}
\pnum
Two kinds of implementations are defined: a \defn{hosted implementation} and a
\defn{freestanding implementation}. For a hosted implementation, this
International Standard defines the set of available libraries. A freestanding
implementation is one in which execution may take place without the benefit of
an operating system, and has an \impldef{required libraries for freestanding
implementation} set of libraries that includes certain language-support
libraries~(\ref{compliance}).
\pnum
A conforming implementation may have extensions (including
additional library functions), provided they do not alter the
behavior of any well-formed program.
Implementations are required to diagnose programs that use such
extensions that are ill-formed according to this International Standard.
Having done so, however, they can compile and execute such programs.
\pnum
Each implementation shall include documentation that identifies all
conditionally-supported constructs\indextext{behavior!conditionally-supported}
that it does not support and defines all locale-specific characteristics.\footnote{This documentation also defines implementation-defined behavior;
see~\ref{intro.execution}.}%
\indextext{conformance requirements!general|)}%
\indextext{conformance requirements|)}%
\rSec1[intro.structure]{Structure of this document}
\pnum
\indextext{standard!structure of|(}%
\indextext{standard!structure of}%
Clauses~\ref{lex} through~\ref{cpp} describe the \Cpp programming
language. That description includes detailed syntactic specifications in
a form described in~\ref{syntax}. For convenience, Annex~\ref{gram}
repeats all such syntactic specifications.
\pnum
Clauses~\ref{\firstlibchapter} through~\ref{\lastlibchapter} and Annex~\ref{depr}
(the \defn{library clauses}) describe the \Cpp standard library.
That description includes detailed descriptions of the
entities and macros
that constitute the library, in a form described in Clause~\ref{library}.
\pnum
Annex~\ref{implimits} recommends lower bounds on the capacity of conforming
implementations.
\pnum
Annex~\ref{diff} summarizes the evolution of \Cpp since its first
published description, and explains in detail the differences between
\Cpp and C\@. Certain features of \Cpp exist solely for compatibility
purposes; Annex~\ref{depr} describes those features.
\pnum
Throughout this document, each example is introduced by
``\noteintro{Example}'' and terminated by ``\noteoutro{example}''. Each note is
introduced by ``\noteintro{Note}'' and terminated by ``\noteoutro{note}''. Examples
and notes may be nested.%
\indextext{standard!structure of|)}
\rSec1[syntax]{Syntax notation}
\pnum
\indextext{notation!syntax|(}%
In the syntax notation used in this document, syntactic
categories are indicated by \grammarterm{italic} type, and literal words
and characters in \tcode{constant} \tcode{width} type. Alternatives are
listed on separate lines except in a few cases where a long set of
alternatives is marked by the phrase ``one of''. If the text of an alternative is too long to fit on a line, the text is continued on subsequent lines indented from the first one.
An optional terminal or non-terminal symbol is indicated by the subscript
``\opt'', so
\begin{ncbnf}
\terminal{\{} expression\opt{} \terminal{\}}
\end{ncbnf}
indicates an optional expression enclosed in braces.%
\pnum
Names for syntactic categories have generally been chosen according to
the following rules:
\begin{itemize}
\item \grammarterm{X-name} is a use of an identifier in a context that
determines its meaning (e.g., \grammarterm{class-name},
\grammarterm{typedef-name}).
\item \grammarterm{X-id} is an identifier with no context-dependent meaning
(e.g., \grammarterm{qualified-id}).
\item \grammarterm{X-seq} is one or more \grammarterm{X}'s without intervening
delimiters (e.g., \grammarterm{declaration-seq} is a sequence of
declarations).
\item \grammarterm{X-list} is one or more \grammarterm{X}'s separated by
intervening commas (e.g., \grammarterm{identifier-list} is a sequence of
identifiers separated by commas).
\end{itemize}%
\indextext{notation!syntax|)}
\rSec1[intro.memory]{The \Cpp memory model}
\pnum
\indextext{memory model|(}%
The fundamental storage unit in the \Cpp memory model is the
\defn{byte}.
A byte is at least large enough to contain any member of the basic
\indextext{character set!basic execution}%
execution character set~(\ref{lex.charset})
and the eight-bit code units of the Unicode UTF-8 encoding form
and is composed of a contiguous sequence of
bits,\footnote{The number of bits in a byte is reported by the macro
\tcode{CHAR_BIT} in the header \tcode{<climits>}.}
the number of which is \impldef{bits in a byte}. The least
significant bit is called the \defn{low-order bit}; the most
significant bit is called the \defn{high-order bit}. The memory
available to a \Cpp program consists of one or more sequences of
contiguous bytes. Every byte has a unique address.
\pnum
\begin{note} The representation of types is described
in~\ref{basic.types}. \end{note}
\pnum
A \defn{memory location} is either an object of scalar type or a maximal
sequence of adjacent bit-fields all having nonzero width. \begin{note} Various
features of the language, such as references and virtual functions, might
involve additional memory locations that are not accessible to programs but are
managed by the implementation. \end{note} Two or more threads of
execution~(\ref{intro.multithread}) can access separate memory
locations without interfering with each other.
\pnum
\begin{note} Thus a bit-field and an adjacent non-bit-field are in separate memory
locations, and therefore can be concurrently updated by two threads of execution
without interference. The same applies to two bit-fields, if one is declared
inside a nested struct declaration and the other is not, or if the two are
separated by a zero-length bit-field declaration, or if they are separated by a
non-bit-field declaration. It is not safe to concurrently update two bit-fields
in the same struct if all fields between them are also bit-fields of nonzero
width. \end{note}
\pnum
\begin{example} A structure declared as
\begin{codeblock}
struct {
char a;
int b:5,
c:11,
:0,
d:8;
struct {int ee:8;} e;
}
\end{codeblock}
contains four separate memory locations: The field \tcode{a} and bit-fields
\tcode{d} and \tcode{e.ee} are each separate memory locations, and can be
modified concurrently without interfering with each other. The bit-fields
\tcode{b} and \tcode{c} together constitute the fourth memory location. The
bit-fields \tcode{b} and \tcode{c} cannot be concurrently modified, but
\tcode{b} and \tcode{a}, for example, can be. \end{example}%
\indextext{memory model|)}
\rSec1[intro.object]{The \Cpp object model}
\pnum
\indextext{object model|(}%
The constructs in a \Cpp program create, destroy, refer to, access, and
manipulate objects.
An \defn{object} is created
by a definition~(\ref{basic.def}),
by a \grammarterm{new-expression}~(\ref{expr.new}),
when implicitly changing the active member of a union~(\ref{class.union}),
or
when a temporary object is created~(\ref{conv.rval}, \ref{class.temporary}).
An object occupies a region of storage
in its period of construction~(\ref{class.cdtor}),
throughout its lifetime~(\ref{basic.life}),
and
in its period of destruction~(\ref{class.cdtor}).
\begin{note} A function is not an object, regardless of whether or not it
occupies storage in the way that objects do. \end{note}
The properties of an
object are determined when the object is created. An object can have a
name~(Clause~\ref{basic}). An object has a storage
duration~(\ref{basic.stc}) which influences its
lifetime~(\ref{basic.life}). An object has a
type~(\ref{basic.types}).
Some objects are
polymorphic~(\ref{class.virtual}); the implementation
generates information associated with each such object that makes it
possible to determine that object's type during program execution. For
other objects, the interpretation of the values found therein is
determined by the type of the \grammarterm{expression}{s} (Clause~\ref{expr})
used to access them.
\pnum
\indextext{subobject}%
Objects can contain other objects, called \defnx{subobjects}{subobject}.
A subobject can be
a \defn{member subobject}~(\ref{class.mem}), a \defn{base class subobject}
(Clause~\ref{class.derived}), or an array element.
\indextext{object!complete}%
An object that is not a subobject of any other object is called a \defn{complete
object}.
If an object is created
in storage associated with a member subobject or array element \placeholder{e}
(which may or may not be within its lifetime),
the created object
is a subobject of \placeholder{e}'s containing object if:
\begin{itemize}
\item
the lifetime of \placeholder{e}'s containing object has begun and not ended, and
\item
the storage for the new object exactly overlays the storage location associated with \placeholder{e}, and
\item
the new object is of the same type as \placeholder{e} (ignoring cv-qualification).
\end{itemize}
\begin{note}
If the subobject contains a reference member or a \tcode{const} subobject,
the name of the original subobject cannot be used to access the new object~(\ref{basic.life}).
\end{note}
\begin{example}
\begin{codeblock}
struct X { const int n; };
union U { X x; float f; };
void tong() {
U u = {{ 1 }};
u.f = 5.f; // OK, creates new subobject of \tcode{u} (\ref{class.union})
X *p = new (&u.x) X {2}; // OK, creates new subobject of \tcode{u}
assert(p->n == 2); // OK
assert(*std::launder(&u.x.n) == 2); // OK
assert(u.x.n == 2); // undefined behavior, \tcode{u.x} does not name new subobject
}
\end{codeblock}
\end{example}
\pnum
\indextext{object!providing storage for}%
If a complete object is created~(\ref{expr.new})
in storage associated with another object \placeholder{e}
of type ``array of $N$ \tcode{unsigned char}'' or
of type ``array of $N$ \tcode{std::byte}''~(\ref{cstddef.syn}),
that array \defn{provides storage}
for the created object if:
\begin{itemize}
\item
the lifetime of \placeholder{e} has begun and not ended, and
\item
the storage for the new object fits entirely within \placeholder{e}, and
\item
there is no smaller array object that satisfies these constraints.
\end{itemize}
\begin{note}
If that portion of the array
previously provided storage for another object,
the lifetime of that object ends
because its storage was reused~(\ref{basic.life}).
\end{note}
\begin{example}
\begin{codeblock}
template<typename ...T>
struct AlignedUnion {
alignas(T...) unsigned char data[max(sizeof(T)...)];
};
int f() {
AlignedUnion<int, char> au;
int *p = new (au.data) int; // OK, \tcode{au.data} provides storage
char *c = new (au.data) char(); // OK, ends lifetime of \tcode{*p}
char *d = new (au.data + 1) char();
return *c + *d; // OK
}
struct A { unsigned char a[32]; };
struct B { unsigned char b[16]; };
A a;
B *b = new (a.a + 8) B; // \tcode{a.a} provides storage for \tcode{*b}
int *p = new (b->b + 4) int; // \tcode{b->b} provides storage for \tcode{*p}
// \tcode{a.a} does not provide storage for \tcode{*p} (directly),
// but \tcode{*p} is nested within \tcode{a} (see below)
\end{codeblock}
\end{example}
\pnum
\indextext{object!nested within}%
An object \placeholder{a} is \defn{nested within} another object \placeholder{b} if:
\begin{itemize}
\item
\placeholder{a} is a subobject of \placeholder{b}, or
\item
\placeholder{b} provides storage for \placeholder{a}, or
\item
there exists an object \placeholder{c}
where \placeholder{a} is nested within \placeholder{c},
and \placeholder{c} is nested within \placeholder{b}.
\end{itemize}
\pnum
For every object \tcode{x}, there is some object called the
\defn{complete object of} \tcode{x}, determined as follows:
\begin{itemize}
\item
If \tcode{x} is a complete object, then the complete object
of \tcode{x} is itself.
\item
Otherwise, the complete object of \tcode{x} is the complete object
of the (unique) object that contains \tcode{x}.
\end{itemize}
\pnum
If a complete object, a data member~(\ref{class.mem}), or an array element is of
class type, its type is considered the \defn{most derived
class}, to distinguish it from the class type of any base class subobject;
an object of a most derived class type or of a non-class type is called a
\defn{most derived object}.
\pnum
\indextext{most derived object!bit-field}%
Unless it is a bit-field~(\ref{class.bit}), a most derived object shall have a
nonzero size and shall occupy one or more bytes of storage. Base class
subobjects may have zero size. An object of trivially copyable or
standard-layout type~(\ref{basic.types}) shall occupy contiguous bytes of
storage.
\pnum
\indextext{most derived object!bit-field}%
\indextext{most derived object!zero size subobject}%
Unless an object is a bit-field or a base class subobject of zero size, the
address of that object is the address of the first byte it occupies.
Two objects \placeholder{a} and \placeholder{b}
with overlapping lifetimes
that are not bit-fields
may have the same address
if one is nested within the other,
or
if at least one is a base class subobject of zero size
and they are of different types;
otherwise, they have distinct addresses.\footnote{Under the ``as-if'' rule an
implementation is allowed to store two objects at the same machine address or
not store an object at all if the program cannot observe the
difference~(\ref{intro.execution}).}
\begin{example}
\begin{codeblock}
static const char test1 = 'x';
static const char test2 = 'x';
const bool b = &test1 != &test2; // always \tcode{true}
\end{codeblock}
\end{example}
\pnum
\begin{note}
\Cpp provides a variety of fundamental types and several ways of composing
new types from existing types~(\ref{basic.types}).
\end{note}%
\indextext{object model|)}
\rSec1[intro.execution]{Program execution}
\pnum
\indextext{program execution|(}%
\indextext{program execution!abstract machine}%
The semantic descriptions in this International Standard define a
parameterized nondeterministic abstract machine. This International
Standard places no requirement on the structure of conforming
implementations. In particular, they need not copy or emulate the
structure of the abstract machine.
\indextext{as-if rule}%
\indextext{behavior!observable}%
Rather, conforming implementations are required to emulate (only) the observable
behavior of the abstract machine as explained below.\footnote{This provision is
sometimes called the ``as-if'' rule, because an implementation is free to
disregard any requirement of this International Standard as long as the result
is \emph{as if} the requirement had been obeyed, as far as can be determined
from the observable behavior of the program. For instance, an actual
implementation need not evaluate part of an expression if it can deduce that its
value is not used and that no
\indextext{side effects}%
side effects affecting the
observable behavior of the program are produced.}
\indextext{behavior!implementation-defined}%
\pnum
Certain aspects and operations of the abstract machine are described in this
International Standard as implementation-defined (for example,
\tcode{sizeof(int)}). These constitute the parameters of the abstract machine.
Each implementation shall include documentation describing its characteristics
and behavior in these respects.\footnote{This documentation also includes
conditionally-supported constructs and locale-specific behavior.
See~\ref{intro.compliance}.} Such documentation shall define the instance of the
abstract machine that corresponds to that implementation (referred to as the
``corresponding instance'' below).
\indextext{behavior!unspecified}%
\pnum
Certain other aspects and operations of the abstract machine are
described in this International Standard as unspecified (for example,
evaluation of expressions in a \grammarterm{new-initializer} if the allocation
function fails to allocate memory~(\ref{expr.new})). Where possible, this
International Standard defines a set of allowable behaviors. These
define the nondeterministic aspects of the abstract machine. An instance
of the abstract machine can thus have more than one possible execution
for a given program and a given input.
\indextext{behavior!undefined}%
\pnum
Certain other operations are described in this International Standard as
undefined (for example, the effect of
attempting to modify a \tcode{const} object).
\begin{note} This International Standard imposes no requirements on the
behavior of programs that contain undefined behavior. \end{note}
\indextext{program!well-formed}%
\indextext{behavior!observable}%
\pnum
A conforming implementation executing a well-formed program shall
produce the same observable behavior as one of the possible executions
of the corresponding instance of the abstract machine with the
same program and the same input.
\indextext{behavior!undefined}%
However, if any such execution contains an undefined operation, this International Standard places no
requirement on the implementation executing that program with that input
(not even with regard to operations preceding the first undefined
operation).
\pnum
An instance of each object with automatic storage
duration~(\ref{basic.stc.auto}) is associated with each entry into its
block. Such an object exists and retains its last-stored value during
the execution of the block and while the block is suspended (by a call
of a function or receipt of a signal).
\indextext{conformance requirements}
\pnum
The least requirements on a conforming implementation are:
\begin{itemize}
\item
Accesses through volatile glvalues are evaluated strictly according to the
rules of the abstract machine.
\item
At program termination, all data written into files shall be
identical to one of the possible results that execution of the program
according to the abstract semantics would have produced.
\item
The input and output dynamics of interactive devices shall take
place in such a fashion that prompting output is actually delivered before a program waits for input. What constitutes an interactive device is
\impldef{interactive device}.
\end{itemize}
These collectively are referred to as the
\defnx{observable behavior}{behavior!observable} of the program.
\begin{note} More stringent correspondences between abstract and actual
semantics may be defined by each implementation. \end{note}
\pnum
\indextext{operator!precedence of}%
\indextext{expression!order of evaluation of}%
\begin{note} Operators can be regrouped according to the usual
mathematical rules only where the operators really are associative or
commutative.\footnote{Overloaded operators are never assumed to be associative or
commutative. }
For example, in the following fragment
\begin{codeblock}
int a, b;
@\commentellip@
a = a + 32760 + b + 5;
\end{codeblock}
the expression statement behaves exactly the same as
\begin{codeblock}
a = (((a + 32760) + b) + 5);
\end{codeblock}
due to the associativity and precedence of these operators. Thus, the
result of the sum \tcode{(a + 32760)} is next added to \tcode{b}, and
that result is then added to 5 which results in the value assigned to
\tcode{a}. On a machine in which overflows produce an exception and in
which the range of values representable by an \tcode{int} is
\crange{-32768}{+32767}, the implementation cannot rewrite this
expression as
\begin{codeblock}
a = ((a + b) + 32765);
\end{codeblock}
since if the values for \tcode{a} and \tcode{b} were, respectively,
-32754 and -15, the sum \tcode{a + b} would produce an exception while
the original expression would not; nor can the expression be rewritten
either as
\begin{codeblock}
a = ((a + 32765) + b);
\end{codeblock}
or
\begin{codeblock}
a = (a + (b + 32765));
\end{codeblock}
since the values for \tcode{a} and \tcode{b} might have been,
respectively, 4 and -8 or -17 and 12. However on a machine in which
overflows do not produce an exception and in which the results of
overflows are reversible, the above expression statement can be
rewritten by the implementation in any of the above ways because the
same result will occur. \end{note}
\pnum
A \defn{constituent expression} is defined as follows:
\begin{itemize}
\item
The constituent expression of an expression is that expression.
\item
The constituent expressions of a \grammarterm{braced-init-list} or
of a (possibly parenthesized) \grammarterm{expression-list}
are the constituent expressions of the elements of the respective list.
\item
The constituent expressions of a \grammarterm{brace-or-equal-initializer}
of the form \tcode{=}~\grammarterm{initializer-clause}
are the constituent expressions of the \grammarterm{initializer-clause}.
\end{itemize}
\begin{example}
\begin{codeblock}
struct A { int x; };
struct B { int y; struct A a; };
B b = { 5, { 1+1 } };
\end{codeblock}
The constituent expressions of the \grammarterm{initializer}
used for the initialization of \tcode{b} are \tcode{5} and \tcode{1+1}.
\end{example}
\pnum
The \defnx{immediate subexpressions}{immediate subexpression} of an expression \tcode{e} are
\begin{itemize}
\item
the constituent expressions of \tcode{e}'s operands (Clause \ref{expr}),
\item
any function call that \tcode{e} implicitly invokes,
\item
if \tcode{e} is a \grammarterm{lambda-expression}~(\ref{expr.prim.lambda}),
the initialization of the entities captured by copy and
the constituent expressions of the \grammarterm{initializer} of the \grammarterm{init-capture}{s},
\item
if \tcode{e} is a function call~(\ref{expr.call}) or implicitly invokes a function,
the constituent expressions of each default argument~(\ref{dcl.fct.default})
used in the call, or
\item
if \tcode{e} creates an aggregate object~(\ref{dcl.init.aggr}),
the constituent expressions of each default member initializer~(\ref{class.mem})
used in the initialization.
\end{itemize}
\pnum
A \defn{subexpression} of an expression \tcode{e} is
an immediate subexpression of \tcode{e} or
a subexpression of an immediate subexpression of \tcode{e}.
\begin{note}
Expressions appearing in the \grammarterm{compound-statement} of a \grammarterm{lambda-expression}
are not subexpressions of the \grammarterm{lambda-expression}.
\end{note}
\pnum
A \defn{full-expression} is
\begin{itemize}
\item
an unevaluated operand (Clause \ref{expr}),
\item
a \grammarterm{constant-expression}~(\ref{expr.const}),
\item
an \grammarterm{init-declarator} (Clause \ref{dcl.decl}) or
a \grammarterm{mem-initializer}~(\ref{class.base.init}),
including the constituent expressions of the initializer,
\item
an invocation of a destructor generated at the end of the lifetime
of an object other than a temporary object~(\ref{class.temporary}), or
\item
an expression that is not a subexpression of another expression and
that is not otherwise part of a full-expression.
\end{itemize}
If a language construct is defined to produce an implicit call of a function,
a use of the language construct is considered to be an expression
for the purposes of this definition.
Conversions applied to the result of an expression in order to satisfy the requirements
of the language construct in which the expression appears
are also considered to be part of the full-expression.
For an initializer, performing the initialization of the entity
(including evaluating default member initializers of an aggregate)
is also considered part of the full-expression.
\begin{example}
\begin{codeblock}
struct S {
S(int i): I(i) { } // full-expression is initialization of \tcode{I}
int& v() { return I; }
~S() noexcept(false) { }
private:
int I;
};
S s1(1); // full-expression is call of \tcode{S::S(int)}
void f() {
S s2 = 2; // full-expression is call of \tcode{S::S(int)}
if (S(3).v()) // full-expression includes lvalue-to-rvalue and
// \tcode{int} to \tcode{bool} conversions, performed before
// temporary is deleted at end of full-expression
{ }
bool b = noexcept(S()); // exception specification of destructor of \tcode{S}
// considered for \tcode{noexcept}
// full-expression is destruction of \tcode{s2} at end of block
}
struct B {
B(S = S(0));
};
B b[2] = { B(), B() }; // full-expression is the entire initialization
// including the destruction of temporaries
\end{codeblock}
\end{example}
\pnum