forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.tex
4387 lines (3896 loc) · 176 KB
/
basic.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[basic]{Basic concepts}
\gramSec[gram.basic]{Basic concepts}
\pnum
\begin{note} This Clause presents the basic concepts of the \Cpp language.
It explains the difference between an object and a
name and how they relate to the value categories for expressions.
It introduces the concepts of a
declaration and a definition and presents \Cpp's
notion of type, scope, linkage, and
storage duration. The mechanisms for starting and
terminating a program are discussed. Finally, this Clause presents the
fundamental types of the language and lists the ways of constructing
compound types from these.\end{note}
\pnum
\begin{note} This Clause does not cover concepts that affect only a single
part of the language. Such concepts are discussed in the relevant
Clauses. \end{note}
\pnum
\indextext{type}%
\indextext{object}%
\indextext{storage~class}%
\indextext{scope}%
\indextext{linkage}%
\indextext{region!declarative}%
An \defn{entity} is a value, object, reference, function, enumerator, type,
class member, bit-field, template, template specialization, namespace, or
parameter pack.
\pnum
A \defn{name} is a use of an \grammarterm{identifier}~(\ref{lex.name}),
\grammarterm{operator-function-id}~(\ref{over.oper}),
\grammarterm{literal-operator-id}~(\ref{over.literal}),
\grammarterm{conversion-function-id}~(\ref{class.conv.fct}), or
\grammarterm{template-id}~(\ref{temp.names}) that denotes an entity or
\grammarterm{label}~(\ref{stmt.goto}, \ref{stmt.label}).
\pnum
Every name that denotes an entity is introduced by a
\defn{declaration}. Every name that denotes a label is introduced
either by a \tcode{goto} statement~(\ref{stmt.goto}) or a
\grammarterm{labeled-statement}~(\ref{stmt.label}).
\pnum
A \defn{variable} is introduced by the
declaration of
a reference other than a non-static data member or of
an object. The variable's name, if any, denotes the reference or object.
\pnum
Some names denote types or templates. In general,
whenever a name is encountered it is necessary to determine whether that name denotes
one of these entities before continuing to parse the program that contains it. The
process that determines this is called
\defnx{name lookup}{lookup!name}~(\ref{basic.lookup}).
\pnum
Two names are \defnx{the same}{name!same} if
\begin{itemize}
\item they are \grammarterm{identifier}{s} composed of the same character sequence, or
\item they are \grammarterm{operator-function-id}{s} formed with
the same operator, or
\item they are \grammarterm{conversion-function-id}{s} formed
with the same type, or
\item they are \grammarterm{template-id}{s} that refer to the same class,
function, or variable~(\ref{temp.type}), or
\item they are the names of literal operators~(\ref{over.literal}) formed with
the same literal suffix identifier.
\end{itemize}
\pnum
\indextext{translation unit!name and}%
\indextext{linkage}%
A name used in more than one translation unit can potentially
refer to the same entity in these translation units depending on the
linkage~(\ref{basic.link}) of the name specified in each
translation unit.
\rSec1[basic.def]{Declarations and definitions}
\pnum
\indextext{declaration!definition~versus}%
\indextext{declaration}%
\indextext{declaration!name}%
A declaration (Clause~\ref{dcl.dcl}) may introduce
one or more names into a translation
unit or redeclare names introduced by previous declarations.
If so, the
declaration specifies the interpretation and attributes of these names.
A declaration may also have effects including:
\begin{itemize}
\item a static assertion (Clause~\ref{dcl.dcl}),
\item controlling template instantiation~(\ref{temp.explicit}),
\item guiding template argument deduction for constructors~(\ref{temp.deduct.guide}),
\item use of attributes (Clause~\ref{dcl.dcl}), and
\item nothing (in the case of an \grammarterm{empty-declaration}).
\end{itemize}
\pnum
\indextext{declaration!function}%
\indextext{definition}%
A declaration is a \defn{definition} unless
\begin{itemize}
\item
it declares a function
without specifying the function's body~(\ref{dcl.fct.def}),
\item
it contains
the
\indextext{declaration!\idxcode{extern}}%
\tcode{extern} specifier~(\ref{dcl.stc}) or a
\grammarterm{linkage-specification}\footnote{Appearing inside the braced-enclosed
\grammarterm{declaration-seq} in a \grammarterm{linkage-specification} does
not affect whether a declaration is a definition.}
(\ref{dcl.link}) and neither an \grammarterm{initializer} nor a
\grammarterm{function-body},
\item
\indextext{declaration!\idxcode{static member}}%
it declares a non-inline static data member in a class
definition (\ref{class.mem},~\ref{class.static}),
\item
it declares a static data member outside a class definition
and the variable was defined within the class with the \tcode{constexpr}
specifier (this usage is deprecated; see \ref{depr.static_constexpr}),
\item
\indextext{declaration!class~name}%
it is a class name declaration~(\ref{class.name}),
\item
it is an
\indextext{declaration!opaque~enum}%
\grammarterm{opaque-enum-declaration}~(\ref{dcl.enum}),
\item
it is a
\indextext{parameter!template}\indextext{template parameter}%
\grammarterm{template-parameter}~(\ref{temp.param}),
\item
it is a
\indextext{declaration!parameter}\indextext{parameter declaration}%
\grammarterm{parameter-declaration}~(\ref{dcl.fct}) in a function
\indextext{declarator}%
declarator that is not the \grammarterm{declarator} of a
\grammarterm{function-definition},
\item
it is a
\indextext{declaration!\idxcode{typedef}}%
\tcode{typedef} declaration~(\ref{dcl.typedef}),
\item it is
an \grammarterm{alias-declaration}~(\ref{dcl.typedef}),
\item it is
a
\grammarterm{using-declaration}~(\ref{namespace.udecl}),
\item it is
a \grammarterm{deduction-guide}~(\ref{temp.deduct.guide}),
\item it is
a \grammarterm{static_assert-declaration} (Clause~\ref{dcl.dcl}),
\item
it is an
\grammarterm{attribute-declaration} (Clause~\ref{dcl.dcl}),
\item
it is an
\grammarterm{empty-declaration} (Clause~\ref{dcl.dcl}),
\item it is
a \grammarterm{using-directive}~(\ref{namespace.udir}),
\item it is
an explicit instantiation declaration~(\ref{temp.explicit}), or
\item it is
an explicit specialization~(\ref{temp.expl.spec}) whose
\grammarterm{declaration} is not a definition.
\end{itemize}
\begin{example} all but one of the following are definitions:
\indextext{example!definition}%
\begin{codeblock}
int a; // defines \tcode{a}
extern const int c = 1; // defines \tcode{c}
int f(int x) { return x+a; } // defines \tcode{f} and defines \tcode{x}
struct S { int a; int b; }; // defines \tcode{S}, \tcode{S::a}, and \tcode{S::b}
struct X { // defines \tcode{X}
int x; // defines non-static data member \tcode{x}
static int y; // declares static data member \tcode{y}
X(): x(0) { } // defines a constructor of \tcode{X}
};
int X::y = 1; // defines \tcode{X::y}
enum { up, down }; // defines \tcode{up} and \tcode{down}
namespace N { int d; } // defines \tcode{N} and \tcode{N::d}
namespace N1 = N; // defines \tcode{N1}
X anX; // defines \tcode{anX}
\end{codeblock}
whereas these are just declarations:
\indextext{example!declaration}%
\begin{codeblock}
extern int a; // declares \tcode{a}
extern const int c; // declares \tcode{c}
int f(int); // declares \tcode{f}
struct S; // declares \tcode{S}
typedef int Int; // declares \tcode{Int}
extern X anotherX; // declares \tcode{anotherX}
using N::d; // declares \tcode{d}
\end{codeblock}
\end{example}
\pnum
\begin{note}
\indextext{implementation-generated}%
In some circumstances, \Cpp implementations implicitly define the
default constructor~(\ref{class.ctor}),
copy constructor~(\ref{class.copy}),
move constructor~(\ref{class.copy}),
copy assignment operator~(\ref{class.copy}),
move assignment operator~(\ref{class.copy}),
or destructor~(\ref{class.dtor}) member functions. \end{note}
\begin{example} given
\begin{codeblock}
#include <string>
struct C {
std::string s; // \tcode{std::string} is the standard library class (Clause~\ref{strings})
};
int main() {
C a;
C b = a;
b = a;
}
\end{codeblock}
the implementation will implicitly define functions to make the
definition of \tcode{C} equivalent to
\begin{codeblock}
struct C {
std::string s;
C() : s() { }
C(const C& x): s(x.s) { }
C(C&& x): s(static_cast<std::string&&>(x.s)) { }
// \tcode{: s(std::move(x.s)) \{ \}}
C& operator=(const C& x) { s = x.s; return *this; }
C& operator=(C&& x) { s = static_cast<std::string&&>(x.s); return *this; }
// \tcode{\{ s = std::move(x.s); return *this; \}}
~C() { }
};
\end{codeblock}
\end{example}
\pnum
\begin{note} A class name can also be implicitly declared by an
\grammarterm{elaborated-type-specifier}~(\ref{dcl.type.elab}).
\end{note}
\pnum
\indextext{type!incomplete}%
A program is ill-formed if the definition of any object gives the object
an incomplete type~(\ref{basic.types}).
\indextext{object!definition}%
\indextext{function!definition}%
\indextext{class!definition}%
\indextext{enumerator!definition}%
\indextext{one-definition~rule|(}%
\rSec1[basic.def.odr]{One-definition rule}
\pnum
No translation unit shall contain more than one definition of any
variable, function, class type, enumeration type, or template.
\pnum
An expression is \defn{potentially evaluated} unless it is an
unevaluated operand (Clause~\ref{expr}) or a subexpression thereof.
The set of \defn{potential results} of an expression \tcode{e} is
defined as follows:
\begin{itemize}
\item If \tcode{e} is an
\grammarterm{id-expression}~(\ref{expr.prim.id}), the set
contains only \tcode{e}.
\item If \tcode{e} is a subscripting operation~(\ref{expr.sub}) with
an array operand, the set contains the potential results of that operand.
\item If \tcode{e} is a class member access
expression~(\ref{expr.ref}), the set contains the potential results of
the object expression.
\item If \tcode{e} is a pointer-to-member
expression~(\ref{expr.mptr.oper}) whose second operand is a constant
expression, the set contains the potential results of the object
expression.
\item If \tcode{e} has the form \tcode{(e1)}, the set contains the
potential results of \tcode{e1}.
\item If \tcode{e} is a glvalue conditional
expression~(\ref{expr.cond}), the set is the union of the sets of
potential results of the second and third operands.
\item If \tcode{e} is a comma expression~(\ref{expr.comma}), the set
contains the potential results of the right operand.
\item Otherwise, the set is empty.
\end{itemize}
\begin{note}
This set is a (possibly-empty) set of \grammarterm{id-expression}{s},
each of which is either \tcode{e} or a subexpression of \tcode{e}.
\begin{example}
In the following example, the set of potential results of the initializer
of \tcode{n} contains the first \tcode{S::x} subexpression, but not the second
\tcode{S::x} subexpression.
\begin{codeblock}
struct S { static const int x = 0; };
const int &f(const int &r);
int n = b ? (1, S::x) // \tcode{S::x} is not odr-used here
: f(S::x); // \tcode{S::x} is odr-used here, so
// a definition is required
\end{codeblock}
\end{example}
\end{note}
\pnum
A variable \tcode{x} whose name appears as a
potentially-evaluated expression \tcode{ex} is \defn{odr-used} by \tcode{ex} unless
applying the lvalue-to-rvalue conversion (\ref{conv.lval}) to \tcode{x} yields
a constant expression~(\ref{expr.const}) that does not invoke any non-trivial
functions
and, if \tcode{x} is an object, \tcode{ex} is an element of
the set of potential results of an expression \tcode{e}, where either the lvalue-to-rvalue
conversion~(\ref{conv.lval}) is applied to \tcode{e}, or \tcode{e} is
a discarded-value expression~(Clause \ref{expr}).
\tcode{this} is odr-used if it appears as a potentially-evaluated expression
(including as the result of the implicit transformation in the body of a non-static
member function~(\ref{class.mfct.non-static})).
A virtual member
function is odr-used if it is not pure.
A function whose name appears as a potentially-evaluated
expression is odr-used if it is the unique lookup result or the selected
member of a set of overloaded functions~(\ref{basic.lookup}, \ref{over.match}, \ref{over.over}),
unless it is a pure virtual function and either
its name is not explicitly qualified or
the expression forms a pointer to member~(\ref{expr.unary.op}).
\begin{note} This covers calls to named
functions~(\ref{expr.call}), operator overloading (Clause~\ref{over}),
user-defined conversions~(\ref{class.conv.fct}), allocation functions for
placement \grammarterm{new-expression}{s}~(\ref{expr.new}), as well as non-default
initialization~(\ref{dcl.init}). A constructor selected to copy or move an
object of class type is odr-used even if the
call is actually elided by the implementation~(\ref{class.copy}). \end{note} An allocation
or deallocation function for a class is odr-used by a \grammarterm{new-expression}
appearing in a potentially-evaluated expression as specified
in~\ref{expr.new} and~\ref{class.free}. A deallocation function for a
class is odr-used by a delete expression appearing in a
potentially-evaluated expression as specified in~\ref{expr.delete}
and~\ref{class.free}. A non-placement allocation or deallocation
function for a class is odr-used by the definition of a constructor of that
class. A non-placement deallocation function for a class is odr-used by the
definition of the destructor of that class, or by being selected by the
lookup at the point of definition of a virtual
destructor~(\ref{class.dtor}).\footnote{An implementation is not required
to call allocation and
deallocation functions from constructors or destructors; however, this
is a permissible implementation technique.}
An assignment operator function in a class is odr-used by an
implicitly-defined
copy-assignment or move-assignment function for another class as specified
in~\ref{class.copy}.
A constructor for a class is odr-used as specified
in~\ref{dcl.init}. A destructor for a class is odr-used if it is potentially
invoked~(\ref{class.dtor}).
\pnum
Every program shall contain exactly one definition of every non-inline
function or variable that is odr-used in that program
outside of a discarded statement~(\ref{stmt.if}); no diagnostic required.
The definition can appear explicitly in the program, it can be found in
the standard or a user-defined library, or (when appropriate) it is
implicitly defined (see~\ref{class.ctor}, \ref{class.dtor} and
\ref{class.copy}). An inline function or variable shall be defined in every
translation unit in which it is odr-used outside of a discarded statement.
\pnum
\indextext{type!incomplete}%
Exactly one definition of a class is required in a translation unit if
the class is used in a way that requires the class type to be complete.
\begin{example} the following complete translation unit is well-formed,
even though it never defines \tcode{X}:
\begin{codeblock}
struct X; // declare \tcode{X} as a struct type
struct X* x1; // use \tcode{X} in pointer formation
X* x2; // use \tcode{X} in pointer formation
\end{codeblock}
\end{example}
\begin{note} The rules for declarations and expressions
describe in which contexts complete class types are required. A class
type \tcode{T} must be complete if:
\begin{itemize}
\item an object of type \tcode{T} is defined~(\ref{basic.def}), or
\item a non-static class data member of type \tcode{T} is
declared~(\ref{class.mem}), or
\item \tcode{T} is used as the object type or array element type in a
\grammarterm{new-expression}~(\ref{expr.new}), or
\item an lvalue-to-rvalue conversion is applied to
a glvalue referring
to an object of type \tcode{T}~(\ref{conv.lval}), or
\item an expression is converted (either implicitly or explicitly) to
type \tcode{T} (Clause~\ref{conv}, \ref{expr.type.conv},
\ref{expr.dynamic.cast}, \ref{expr.static.cast}, \ref{expr.cast}), or
\item an expression that is not a null pointer constant, and has type
other than \cv{} \tcode{void*}, is converted to the type pointer to \tcode{T}
or reference to \tcode{T} using a standard conversion
(Clause~\ref{conv}), a \tcode{dynamic_cast}~(\ref{expr.dynamic.cast}) or
a \tcode{static_cast}~(\ref{expr.static.cast}), or
\item a class member access operator is applied to an expression of type
\tcode{T}~(\ref{expr.ref}), or
\item the \tcode{typeid} operator~(\ref{expr.typeid}) or the
\tcode{sizeof} operator~(\ref{expr.sizeof}) is applied to an operand of
type \tcode{T}, or
\item a function with a return type or argument type of type \tcode{T}
is defined~(\ref{basic.def}) or called~(\ref{expr.call}), or
\item a class with a base class of type \tcode{T} is
defined (Clause~\ref{class.derived}), or
\item an lvalue of type \tcode{T} is assigned to~(\ref{expr.ass}), or
\item the type \tcode{T} is the subject of an
\tcode{alignof} expression~(\ref{expr.alignof}), or
\item an \grammarterm{exception-declaration} has type \tcode{T}, reference to
\tcode{T}, or pointer to \tcode{T}~(\ref{except.handle}).
\end{itemize}
\end{note}
\pnum
There can be more than one definition of a class type
(Clause~\ref{class}), enumeration type~(\ref{dcl.enum}), inline function
with external linkage~(\ref{dcl.inline}), inline variable with external
linkage~(\ref{dcl.inline}), class template
(Clause~\ref{temp}), non-static function template~(\ref{temp.fct}),
static data member of a class template~(\ref{temp.static}), member
function of a class template~(\ref{temp.mem.func}), or template
specialization for which some template parameters are not
specified~(\ref{temp.spec}, \ref{temp.class.spec}) in a program provided
that each definition appears in a different translation unit, and
provided the definitions satisfy the following requirements. Given such
an entity named \tcode{D} defined in more than one translation unit,
then
\begin{itemize}
\item each definition of \tcode{D} shall consist of the same sequence of
tokens; and
\item in each definition of \tcode{D}, corresponding names, looked up
according to~\ref{basic.lookup}, shall refer to an entity defined within
the definition of \tcode{D}, or shall refer to the same entity, after
overload resolution~(\ref{over.match}) and after matching of partial
template specialization~(\ref{temp.over}), except that a name can refer to
\begin{itemize}
\item
a non-volatile \tcode{const} object with internal or no linkage if the object
\begin{itemize}
\item has the same literal type in all definitions of \tcode{D},
\item is initialized with a constant expression~(\ref{expr.const}),
\item is not odr-used, and
\item has the same value in all definitions of \tcode{D},
\end{itemize}
or
\item
a reference with internal or no linkage
initialized with a constant expression such that
the reference refers to the same entity in all definitions of \tcode{D};
\end{itemize}
and
\item in each definition of \tcode{D}, corresponding entities shall have the
same language linkage; and
\item in each definition of \tcode{D}, the overloaded operators referred
to, the implicit calls to conversion functions, constructors, operator
new functions and operator delete functions, shall refer to the same
function, or to a function defined within the definition of \tcode{D};
and
\item in each definition of \tcode{D}, a default argument used by an
(implicit or explicit) function call is treated as if its token sequence
were present in the definition of \tcode{D}; that is, the default
argument is subject to the requirements described in this paragraph (and, if
the default argument has sub-expressions with default arguments, this
requirement applies recursively).\footnote{\ref{dcl.fct.default}
describes how default argument names are looked up.}
\item if \tcode{D} is a class with an implicitly-declared
constructor~(\ref{class.ctor}), it is as if the constructor was
implicitly defined in every translation unit where it is odr-used, and the
implicit definition in every translation unit shall call the same
constructor for a subobject of \tcode{D}.
\begin{example}
\begin{codeblock}
// translation unit 1:
struct X {
X(int, int);
X(int, int, int);
};
X::X(int, int = 0) { }
class D {
X x = 0;
};
D d2; // \tcode{X(int, int)} called by \tcode{D()}
// translation unit 2:
struct X {
X(int, int);
X(int, int, int);
};
X::X(int, int = 0, int = 0) { }
class D {
X x = 0;
}; // \tcode{X(int, int, int)} called by \tcode{D()};
// \tcode{D()}'s implicit definition
// violates the ODR
\end{codeblock}
\end{example}
\end{itemize}
If \tcode{D} is a template and is defined in more than one
translation unit, then the preceding requirements
shall apply both to names from the template's enclosing scope used in the
template definition~(\ref{temp.nondep}), and also to dependent names at
the point of instantiation~(\ref{temp.dep}). If the definitions of
\tcode{D} satisfy all these requirements, then the behavior is
as if there were a single definition of \tcode{D}. If the definitions of
\tcode{D} do not satisfy these requirements, then the behavior is
undefined.%
\indextext{one-definition~rule|)}
\rSec1[basic.scope]{Scope}%
\indextext{scope|(}
\rSec2[basic.scope.declarative]{Declarative regions and scopes}%
\indextext{scope!declarations and|(}
\pnum
\indextext{name!scope~of}%
Every name is introduced in some portion of program text called a
\indextext{region!declarative}%
\indextext{scope!potential}%
\defn{declarative region}, which is the largest part of the program
in which that name is \defn{valid}, that is, in which that name may
be used as an unqualified name to refer to the same entity. In general,
each particular name is valid only within some possibly discontiguous
portion of program text called its \defn{scope}. To determine the
scope of a declaration, it is sometimes convenient to refer to the
\defn{potential scope} of a declaration. The scope of a declaration
is the same as its potential scope unless the potential scope contains
another declaration of the same name. In that case, the potential scope
of the declaration in the inner (contained) declarative region is
excluded from the scope of the declaration in the outer (containing)
declarative region.
\pnum
\begin{example}
in
\begin{codeblock}
int j = 24;
int main() {
int i = j, j;
j = 42;
}
\end{codeblock}
the identifier \tcode{j} is declared twice as a name (and used twice).
The declarative region of the first \tcode{j} includes the entire
example. The potential scope of the first \tcode{j} begins immediately
after that \tcode{j} and extends to the end of the program, but its
(actual) scope excludes the text between the \tcode{,} and the
\tcode{\}}. The declarative region of the second declaration of
\tcode{j} (the \tcode{j} immediately before the semicolon) includes all
the text between \tcode{\{} and \tcode{\}}, but its potential scope
excludes the declaration of \tcode{i}. The scope of the second
declaration of \tcode{j} is the same as its potential scope.
\end{example}
\pnum
The names declared by a declaration are introduced into the scope in
which the declaration occurs, except that the presence of a
\tcode{friend} specifier~(\ref{class.friend}), certain uses of the
\grammarterm{elaborated-type-specifier}~(\ref{dcl.type.elab}), and
\grammarterm{using-directive}{s}~(\ref{namespace.udir}) alter this general
behavior.
\pnum
Given a set of declarations in a single declarative region, each of
which specifies the same unqualified name,
\begin{itemize}
\item they shall all refer to the same entity, or all refer to functions
and function templates; or
\item exactly one declaration shall declare a class name or enumeration
name that is not a typedef name and the other declarations shall
all refer to the same variable, non-static data member, or enumerator,
or all refer to functions and function templates;
in this case the class name or enumeration name is
hidden~(\ref{basic.scope.hiding}). \begin{note} A namespace name or a
class template name must be unique in its declarative
region~(\ref{namespace.alias}, Clause~\ref{temp}). \end{note}
\end{itemize}
\begin{note} These restrictions apply to the declarative region into which
a name is introduced, which is not necessarily the same as the region in
which the declaration occurs. In particular,
\grammarterm{elaborated-type-specifier}{s}~(\ref{dcl.type.elab}) and
friend declarations~(\ref{class.friend}) may introduce a (possibly not
visible) name into an enclosing namespace; these restrictions apply to
that region. Local extern declarations~(\ref{basic.link}) may introduce
a name into the declarative region where the declaration appears and
also introduce a (possibly not visible) name into an enclosing
namespace; these restrictions apply to both regions. \end{note}
\pnum
\begin{note} The name lookup rules are summarized in~\ref{basic.lookup}.
\end{note}
\rSec2[basic.scope.pdecl]{Point of declaration}
\pnum
\indextext{name!point~of declaration}%
The \defn{point of declaration} for a name is immediately after its
complete declarator (Clause~\ref{dcl.decl}) and before its
\grammarterm{initializer} (if any), except as noted below. \begin{example}
\begin{codeblock}
unsigned char x = 12;
{ unsigned char x = x; }
\end{codeblock}
Here the second \tcode{x} is initialized with its own (indeterminate)
value. \end{example}
\pnum
\begin{note}
\indextext{name~hiding}%
a name from an outer scope remains visible up
to the point of declaration of the name that hides it. \begin{example}
\begin{codeblock}
const int i = 2;
{ int i[i]; }
\end{codeblock}
declares a block-scope array of two integers. \end{example} \end{note}
\pnum
The point of declaration for a class or class template first declared by a
\grammarterm{class-specifier} is immediately after the \grammarterm{identifier} or
\grammarterm{simple-template-id} (if any) in its \grammarterm{class-head}
(Clause~\ref{class}). The point of declaration for an enumeration is
immediately after the \grammarterm{identifier} (if any) in either its
\grammarterm{enum-specifier}~(\ref{dcl.enum}) or its first
\grammarterm{opaque-enum-declaration}~(\ref{dcl.enum}), whichever comes first.
The point of declaration of an alias or alias template immediately
follows the \grammarterm{type-id} to which the
alias refers.
\pnum
The point of declaration of a \grammarterm{using-declarator} that does not name a
constructor is immediately after the \grammarterm{using-declarator}~(\ref{namespace.udecl}).
\pnum
\indextext{declaration!enumerator point~of}%
The point of declaration for an enumerator is immediately after its
\grammarterm{enumerator-definition}. \begin{example}
\begin{codeblock}
const int x = 12;
{ enum { x = x }; }
\end{codeblock}
Here, the enumerator \tcode{x} is initialized with the value of the
constant \tcode{x}, namely 12. \end{example}
\pnum
After the point of declaration of a class member, the member name can be
looked up in the scope of its class. \begin{note}
\indextext{type!incomplete}%
this is true even if the class is an incomplete class. For example,
\begin{codeblock}
struct X {
enum E { z = 16 };
int b[X::z]; // OK
};
\end{codeblock}
\end{note}
\pnum
The point of declaration of a class first declared in an
\grammarterm{elaborated-type-specifier} is as follows:
\begin{itemize}
\item for a declaration of the form
\begin{ncbnf}
class-key attribute-specifier-seq\opt identifier \terminal{;}
\end{ncbnf}
the \grammarterm{identifier} is declared to be a
\grammarterm{class-name} in the scope that contains the declaration,
otherwise
\item for an \grammarterm{elaborated-type-specifier} of the form
\begin{ncbnf}
class-key identifier
\end{ncbnf}
if the
\grammarterm{elaborated-type-specifier} is used in the
\grammarterm{decl-specifier-seq} or \grammarterm{parameter-declaration-clause}
of a function defined in namespace scope, the \grammarterm{identifier} is
declared as a \grammarterm{class-name} in the namespace that contains the
declaration; otherwise, except as a friend declaration, the
\grammarterm{identifier} is declared in the smallest namespace or block
scope that contains the declaration. \begin{note}
These rules also apply within templates. \end{note} \begin{note} Other
forms of \grammarterm{elaborated-type-specifier} do not declare a new name,
and therefore must refer to an existing \grammarterm{type-name}.
See~\ref{basic.lookup.elab} and~\ref{dcl.type.elab}. \end{note}
\end{itemize}
\pnum
The point of declaration for an
\grammarterm{injected-class-name} (Clause~\ref{class}) is immediately following
the opening brace of the class definition.
\pnum
The point of declaration for a function-local predefined
variable~(\ref{dcl.fct.def}) is immediately before the
\grammarterm{function-body} of a function definition.
\pnum
The point of declaration for a template parameter is immediately after its complete
\grammarterm{template-parameter}. \begin{example}
\begin{codeblock}
typedef unsigned char T;
template<class T
= T // lookup finds the typedef name of \tcode{unsigned char}
, T // lookup finds the template parameter
N = 0> struct A { };
\end{codeblock}
\end{example}
\pnum
\begin{note} Friend declarations refer to functions or classes that are
members of the nearest enclosing namespace, but they do not introduce
new names into that namespace~(\ref{namespace.memdef}). Function
declarations at block scope and variable declarations with the
\tcode{extern} specifier at block scope refer to declarations that are
members of an enclosing namespace, but they do not introduce new names
into that scope.
\end{note}
\pnum
\begin{note} For point of instantiation of a template,
see~\ref{temp.point}.\end{note}%
\indextext{scope!declarations and|)}
\rSec2[basic.scope.block]{Block scope}
\pnum
\indextext{scope!block}%
\indextext{local~scope|see{block scope}}%
A name declared in a block~(\ref{stmt.block}) is local to that block; it has
\defn{block scope}.
Its potential scope begins at its point of
declaration~(\ref{basic.scope.pdecl}) and ends at the end of its block.
A variable declared at block scope is a \defn{local variable}.
\pnum
\indextext{parameter!scope~of}%
The potential scope of a function parameter name
(including one appearing in a
\grammarterm{lambda-declarator})
or of a function-local predefined variable
in a function
definition~(\ref{dcl.fct.def}) begins at its point of declaration. If
the function has a \grammarterm{function-try-block} the potential scope of
a parameter
or of a function-local predefined variable
ends at the end of the last associated handler, otherwise it ends
at the end of the outermost block of the function definition. A
parameter name shall not be redeclared in the outermost block of the
function definition nor in the outermost block of any handler associated
with a \grammarterm{function-try-block}.
\pnum
\indextext{scope!exception~declaration}%
The name declared in an \grammarterm{exception-declaration}
is local to the
\grammarterm{handler} and shall not be redeclared in the outermost block of the
\grammarterm{handler}.
\pnum
Names declared in the \grammarterm{init-statement}, the \grammarterm{for-range-declaration}, and in the
\grammarterm{condition} of \tcode{if}, \tcode{while}, \tcode{for}, and
\tcode{switch} statements are local to the \tcode{if}, \tcode{while},
\tcode{for}, or \tcode{switch} statement (including the controlled
statement), and shall not be redeclared in a subsequent condition of
that statement nor in the outermost block (or, for the \tcode{if}
statement, any of the outermost blocks) of the controlled statement;
see~\ref{stmt.select}.
\rSec2[basic.scope.proto]{Function prototype scope}
\pnum
\indextext{scope!function~prototype}%
\indextext{function~prototype}%
In a function declaration, or in any function declarator except the
declarator of a function definition~(\ref{dcl.fct.def}), names of
parameters (if supplied) have function prototype scope, which terminates
at the end of the nearest enclosing function declarator.
\rSec2[basic.funscope]{Function scope}
\pnum
\indextext{label!scope~of}%
Labels~(\ref{stmt.label}) have \defnx{function scope}{scope!function} and
may be used anywhere in the function in which they are declared. Only
labels have function scope.
\rSec2[basic.scope.namespace]{Namespace scope}
\pnum
\indextext{scope!namespace}%
The declarative region of a \grammarterm{namespace-definition} is its
\grammarterm{namespace-body}. Entities declared in a
\grammarterm{namespace-body} are said to be \defn{members} of the
namespace, and names introduced by these declarations into the
declarative region of the namespace are said to be \defn{member
names} of the namespace. A namespace member name has namespace scope.
Its potential scope includes its namespace from the name's point of
declaration~(\ref{basic.scope.pdecl}) onwards; and for each
\grammarterm{using-directive}~(\ref{namespace.udir}) that nominates the
member's namespace, the member's potential scope includes that portion
of the potential scope of the \grammarterm{using-directive} that follows
the member's point of declaration. \begin{example}
\begin{codeblock}
namespace N {
int i;
int g(int a) { return a; }
int j();
void q();
}
namespace { int l=1; }
// the potential scope of \tcode{l} is from its point of declaration
// to the end of the translation unit
namespace N {
int g(char a) { // overloads \tcode{N::g(int)}
return l+a; // \tcode{l} is from unnamed namespace
}
int i; // error: duplicate definition
int j(); // OK: duplicate function declaration
int j() { // OK: definition of \tcode{N::j()}
return g(i); // calls \tcode{N::g(int)}
}
int q(); // error: different return type
}
\end{codeblock}
\end{example}
\pnum
A namespace member can also be referred to after the \tcode{::} scope
resolution operator~(\ref{expr.prim}) applied to the name of its
namespace or the name of a namespace which nominates the member's
namespace in a \grammarterm{using-directive}; see~\ref{namespace.qual}.
\pnum
\indextext{scope!global namespace}%
\indextext{scope!global}%
The outermost declarative region of a translation unit is also a
namespace, called the \defn{global namespace}. A name declared in
the global namespace has \defn{global namespace scope} (also called
\defn{global scope}). The potential scope of such a name begins at
its point of declaration~(\ref{basic.scope.pdecl}) and ends at the end
of the translation unit that is its declarative region.
\indextext{name!global}%
A name with global namespace scope is said to be a
\defnx{global name}{global}.
\rSec2[basic.scope.class]{Class scope}
\pnum
\indextext{scope!class}%
The following rules describe the scope of names declared in classes.
\begin{enumerate}
\item The potential scope of a name declared in a class consists not
only of the declarative region following the name's point of
declaration, but also of all function bodies, default arguments,
\grammarterm{noexcept-specifier}{s}, and
\grammarterm{brace-or-equal-initializers} of non-static data members
in that class (including such
things in nested classes).
\item A name \tcode{N} used in a class \tcode{S} shall refer to the same
declaration in its context and when re-evaluated in the completed scope
of \tcode{S}. No diagnostic is required for a violation of this rule.
\item A name declared within a member function hides a declaration of
the same name whose scope extends to or past the end of the member
function's class.
\item The potential scope of a declaration that extends to or past the
end of a class definition also extends to the regions defined by its
member definitions, even if the members are defined lexically outside
the class (this includes static data member definitions, nested class
definitions, and member function definitions, including the member function
body and any portion of the
declarator part of such definitions which follows the \grammarterm{declarator-id},
including a \grammarterm{parameter-declaration-clause} and any default
arguments~(\ref{dcl.fct.default})). \begin{example}
\begin{codeblock}
typedef int c;
enum { i = 1 };
class X {
char v[i]; // error: \tcode{i} refers to \tcode{::i}
// but when reevaluated is \tcode{X::i}
int f() { return sizeof(c); } // OK: \tcode{X::c}
char c;
enum { i = 2 };
};
typedef char* T;
struct Y {
T a; // error: \tcode{T} refers to \tcode{::T}
// but when reevaluated is \tcode{Y::T}
typedef long T;
T b;
};
typedef int I;
class D {
typedef I I; // error, even though no reordering involved
};
\end{codeblock}
\end{example}
\end{enumerate}
\pnum
The name of a class member shall only be used as follows:
\begin{itemize}
\item in the scope of its class (as described above) or a class derived
(Clause~\ref{class.derived}) from its class,
\item after the \tcode{.} operator applied to an expression of the type
of its class~(\ref{expr.ref}) or a class derived from its class,
\item after the \tcode{->} operator applied to a pointer to an object of
its class~(\ref{expr.ref}) or a class derived from its class,
\item after the \tcode{::} scope resolution operator~(\ref{expr.prim})
applied to the name of its class or a class derived from its class.
\end{itemize}
\rSec2[basic.scope.enum]{Enumeration scope}%
\indextext{enumeration scope}%
\indextext{scope!enumeration}
\pnum
The name of a scoped enumerator~(\ref{dcl.enum}) has
\defn{enumeration scope}. Its potential scope begins at
its point of declaration and terminates at the end of the
\grammarterm{enum-specifier}.
\rSec2[basic.scope.temp]{Template parameter scope}%
\indextext{template~parameter~scope}%
\indextext{scope!template~parameter}%
\pnum
The declarative region of the name of a template parameter of a template
\grammarterm{template-parameter} is the smallest \grammarterm{template-parameter-list}
in which the name was introduced.
\pnum
The declarative region of the name of a template parameter of a template is the smallest
\grammarterm{template-declaration} in which the name was introduced. Only template
parameter names belong to this declarative region; any other kind of name introduced by
the \grammarterm{declaration} of a \grammarterm{template-declaration} is instead
introduced into the same declarative region where it would be introduced as a result of
a non-template declaration of the same name. \begin{example}
\begin{codeblock}
namespace N {
template<class T> struct A { }; // \#1
template<class U> void f(U) { } // \#2
struct B {
template<class V> friend int g(struct C*); // \#3
};
}
\end{codeblock}
The declarative regions of \tcode{T}, \tcode{U} and \tcode{V} are the
\grammarterm{template-declaration}{s} on lines \tcode{\#1}, \tcode{\#2} and \tcode{\#3},
respectively. But the names \tcode{A}, \tcode{f}, \tcode{g} and \tcode{C} all belong to
the same declarative region --- namely, the \grammarterm{namespace-body} of \tcode{N}.
(\tcode{g} is still considered to belong to this declarative region in spite of its
being hidden during qualified and unqualified name lookup.)
\end{example}
\pnum
The potential scope of a template parameter name begins at its point of
declaration~(\ref{basic.scope.pdecl}) and ends at the end of its declarative region.
\begin{note} This implies that a \grammarterm{template-parameter} can be used in the
declaration of subsequent \grammarterm{template-parameter}{s} and their default