forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 4
/
basic.tex
7193 lines (6626 loc) · 260 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]{Basics}
\gramSec[gram.basic]{Basics}
\rSec1[basic.pre]{Preamble}
\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}%
An \defn{entity} is a value, object, reference,
structured binding,
function, enumerator, type,
class member, bit-field, template, template specialization, namespace, or
pack.
\pnum
A \defn{name} is an \grammarterm{identifier}\iref{lex.name},
\grammarterm{operator-function-id}\iref{over.oper},
\grammarterm{literal-operator-id}\iref{over.literal}, or
\grammarterm{conversion-function-id}\iref{class.conv.fct}.
\pnum
Every name is introduced by a \defn{declaration}, which is a
\begin{itemize}
\item
\grammarterm{name-declaration},
\grammarterm{block-declaration}, or
\grammarterm{member-declaration}\iref{dcl.pre,class.mem},
\item
\grammarterm{init-declarator}\iref{dcl.decl},
\item
\grammarterm{identifier}
in a structured binding declaration\iref{dcl.struct.bind},
\item
\grammarterm{init-capture}\iref{expr.prim.lambda.capture},
\item
\grammarterm{condition} with a \grammarterm{declarator}\iref{stmt.pre},
\item
\grammarterm{member-declarator}\iref{class.mem},
\item
\grammarterm{using-declarator}\iref{namespace.udecl},
\item
\grammarterm{parameter-declaration}\iref{dcl.fct},
\item
\grammarterm{type-parameter}\iref{temp.param},
\item
\grammarterm{elaborated-type-specifier}
that introduces a name\iref{dcl.type.elab},
\item
\grammarterm{class-specifier}\iref{class.pre},
\item
\grammarterm{enum-specifier} or
\grammarterm{enumerator-definition}\iref{dcl.enum},
\item
\grammarterm{exception-declaration}\iref{except.pre}, or
\item
implicit declaration of an injected-class-name\iref{class.pre}.
\end{itemize}
\begin{note}
The interpretation of a \grammarterm{for-range-declaration} produces
one or more of the above\iref{stmt.ranged}.
\end{note}
An entity $E$ is denoted by the name (if any)
that is introduced by a declaration of $E$ or
by a \grammarterm{typedef-name} introduced by a declaration specifying $E$.
\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
A \defnadj{local}{entity} is a variable with
\link{automatic storage duration}{basic.stc.auto},
a \link{structured binding}{dcl.struct.bind}
whose corresponding variable is such an entity,
or the \tcode{*\keyword{this}} object\iref{expr.prim.this}.
\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}\iref{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 equivalent\iref{temp.over.link} types, or
\item they are \grammarterm{literal-operator-id}{s}\iref{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
\link{linkage}{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 \link{declaration}{dcl.dcl} may (re)introduce
one or more names and/or entities into a translation
unit.
If so, the
declaration specifies the interpretation and semantic properties of these names.
A declaration of an entity or \grammarterm{typedef-name} $X$ is
a redeclaration of $X$
if another declaration of $X$ is reachable from it\iref{module.reach}.
A declaration may also have effects including:
\begin{itemize}
\item a static assertion\iref{dcl.pre},
\item controlling template instantiation\iref{temp.explicit},
\item guiding template argument deduction for constructors\iref{temp.deduct.guide},
\item use of \link{attributes}{dcl.attr}, and
\item nothing (in the case of an \grammarterm{empty-declaration}).
\end{itemize}
\pnum
\indextext{declaration!function}%
\indextext{definition}%
Each entity declared by a \grammarterm{declaration} is
also \defnx{defined}{define} by that declaration unless:
\begin{itemize}
\item
it declares a function
without specifying the function's body\iref{dcl.fct.def},
\item
it contains
the
\indextext{declaration!\idxcode{extern}}%
\keyword{extern} specifier\iref{dcl.stc} or a
\grammarterm{linkage-specification}
\begin{footnote}
Appearing inside the brace-enclosed
\grammarterm{declaration-seq} in a \grammarterm{linkage-specification} does
not affect whether a declaration is a definition.
\end{footnote}
\iref{dcl.link}
and neither an \grammarterm{initializer} nor a
\grammarterm{function-body},
\item
\indextext{declaration!static member@\tcode{static} member}%
it declares a non-inline static data member in a class
definition\iref{class.mem,class.static},
\item
it declares a static data member outside a class definition
and the variable was defined within the class with the \keyword{constexpr}
specifier\iref{class.static.data} (this usage is deprecated; see \ref{depr.static.constexpr}),
\item
\indextext{declaration!class name}%
it is an \grammarterm{elaborated-type-specifier}\iref{class.name},
\item
it is an
\indextext{declaration!opaque enum}%
\grammarterm{opaque-enum-declaration}\iref{dcl.enum},
\item
it is a
\indextext{parameter!template}\indextext{template parameter}%
\grammarterm{template-parameter}\iref{temp.param},
\item
it is a
\indextext{declaration!parameter}\indextext{parameter declaration}%
\grammarterm{parameter-declaration}\iref{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}}%
\keyword{typedef} declaration\iref{dcl.typedef},
\item it is
an \grammarterm{alias-declaration}\iref{dcl.typedef},
\item it is
a
\grammarterm{using-declaration}\iref{namespace.udecl},
\item it is
a \grammarterm{deduction-guide}\iref{temp.deduct.guide},
\item it is
a \grammarterm{static_assert-declaration}\iref{dcl.pre},
\item
it is an
\grammarterm{attribute-declaration}\iref{dcl.pre},
\item
it is an
\grammarterm{empty-declaration}\iref{dcl.pre},
\item it is
a \grammarterm{using-directive}\iref{namespace.udir},
\item it is
a \grammarterm{using-enum-declaration}\iref{enum.udecl},
\item it is
a \grammarterm{template-declaration}\iref{temp.pre}
whose \grammarterm{template-head} is not followed by
either a \grammarterm{concept-definition} or a \grammarterm{declaration}
that defines a function, a class, a variable, or a static data member,
\item it is
an explicit instantiation declaration\iref{temp.explicit}, or
\item it is
an \link{explicit specialization}{temp.expl.spec} whose
\grammarterm{declaration} is not a definition.
\end{itemize}
A declaration is said to be a \defn{definition} of each entity that it defines.
\begin{example}
All but one of the following are definitions:
\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:
\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\iref{class.default.ctor},
copy constructor, move constructor\iref{class.copy.ctor},
copy assignment operator, move assignment operator\iref{class.copy.assign},
or \link{destructor}{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\iref{string.classes}
};
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)) { }
@\rlap{\textnormal{\textit{//}}}@ : 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; }
@\rlap{\textnormal{\textit{//}}}@ { 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}\iref{dcl.type.elab}.
\end{note}
\pnum
\indextext{type!incomplete}%
In the definition of an object,
the type of that object shall not be
an incomplete type\iref{term.incomplete.type},
an abstract class type\iref{class.abstract}, or
a (possibly multidimensional) array thereof.
\rSec1[basic.def.odr]{One-definition rule}%
\indextext{object!definition}%
\indextext{function!definition}%
\indextext{class!definition}%
\indextext{enumerator!definition}%
\indextext{one-definition rule|(}%
\pnum
Each of the following is termed a \defnadj{definable}{item}:
\begin{itemize}
\item a class type\iref{class},
\item an enumeration type\iref{dcl.enum},
\item a function\iref{dcl.fct},
\item a variable\iref{basic.pre},
\item a templated entity\iref{temp.pre},
\item a default argument for a parameter
(for a function in a given scope)\iref{dcl.fct.default}, or
\item a default template argument\iref{temp.param}.
\end{itemize}
\pnum
No translation unit shall contain more than one definition of any
definable item.
\pnum
\indextext{expression!potentially evaluated}%
An expression or conversion is \defn{potentially evaluated} unless it is
an unevaluated operand\iref{expr.context},
a subexpression thereof, or
a conversion in an initialization or conversion sequence in such a context.
The set of \defn{potential results} of an expression $E$ is
defined as follows:
\begin{itemize}
\item If $E$ is an
\grammarterm{id-expression}\iref{expr.prim.id}, the set
contains only $E$.
\item If $E$ is a \link{subscripting}{expr.sub} operation with
an array operand, the set contains the potential results of that operand.
\item If $E$ is a class member access
expression\iref{expr.ref} of the form
$E_1$ \tcode{.} \opt{\keyword{template}} $E_2$
naming a non-static data member,
the set contains the potential results of $E_1$.
\item If $E$ is a class member access expression
naming a static data member,
the set contains the \grammarterm{id-expression} designating the data member.
\item If $E$ is a \link{pointer-to-member expression}{expr.mptr.oper} of the form
$E_1$ \tcode{.*} $E_2$,
the set contains the potential results of $E_1$.
\item If $E$ has the form \tcode{($E_1$)}, the set contains the
potential results of $E_1$.
\item If $E$ is a glvalue \link{conditional expression}{expr.cond},
the set is the union of the sets of
potential results of the second and third operands.
\item If $E$ is a \link{comma expression}{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 $E$ or a subexpression of $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 function is \defnx{named by}{function!named by expression or conversion}
an expression or conversion as follows:
\begin{itemize}
\item
A function is named by an expression or conversion
if it is the selected member
of an overload set\iref{basic.lookup,over.match,over.over}
in an overload resolution performed
as part of forming that expression or conversion,
unless it is a pure virtual function and either
the expression is not an \grammarterm{id-expression} naming the function with
an explicitly qualified name or
the expression forms a pointer to member\iref{expr.unary.op}.
\begin{note}
This covers
taking the address of functions\iref{conv.func,expr.unary.op},
calls to named functions\iref{expr.call},
operator overloading\iref{over},
user-defined conversions\iref{class.conv.fct},
allocation functions for \grammarterm{new-expression}{s}\iref{expr.new}, as well as
non-default initialization\iref{dcl.init}.
A constructor selected to copy or move an object of class type
is considered to be named by an expression or conversion
even if the call is actually elided by the implementation\iref{class.copy.elision}.
\end{note}
\item
A deallocation function for a class
is named by a \grammarterm{new-expression}
if it is the single matching deallocation function
for the allocation function selected by overload resolution,
as specified in~\ref{expr.new}.
\item
A deallocation function for a class
is named by a \grammarterm{delete-expression}
if it is the selected usual deallocation function
as specified in~\ref{expr.delete} and~\ref{class.free}.
\end{itemize}
\pnum
\label{term.odr.use}%
A variable is named by an expression
if the expression is an \grammarterm{id-expression} that denotes it.
A variable \tcode{x} that is named by a
potentially-evaluated expression $E$
is \defnx{odr-used}{odr-use} by $E$ unless
\begin{itemize}
\item
\tcode{x} is a reference that is
usable in constant expressions\iref{expr.const}, or
\item
\tcode{x} is a variable of non-reference type that is
usable in constant expressions and has no mutable subobjects, and
$E$ is an element of the set of potential results of an expression
of non-volatile-qualified non-class type
to which the lvalue-to-rvalue conversion\iref{conv.lval} is applied, or
\item
\tcode{x} is a variable of non-reference type, and
$E$ is an element of the set of potential results
of a discarded-value expression\iref{expr.context}
to which the lvalue-to-rvalue conversion is not applied.
\end{itemize}
\pnum
A structured binding is odr-used if it appears as a potentially-evaluated expression.
\pnum
\tcode{*\keyword{this}} is odr-used if \keyword{this} appears as a potentially-evaluated expression
(including as the result of any implicit transformation to
a class member access expression\iref{expr.prim.id.general}).
\pnum
A virtual member
function is odr-used if it is not pure.
A function is odr-used if it is named by
a potentially-evaluated expression or conversion.
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\iref{class.dtor}.
\begin{footnote}
An implementation is not required
to call allocation and
deallocation functions from constructors or destructors; however, this
is a permissible implementation technique.
\end{footnote}
\pnum
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.assign}.
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
\deflink{potentially invoked}{class.dtor}.
\pnum
A local entity\iref{basic.pre}
is \defn{odr-usable} in a scope\iref{basic.scope.scope} if
\begin{itemize}
\item either the local entity is not \tcode{*\keyword{this}}, or
an enclosing class or non-lambda function parameter scope exists and,
if the innermost such scope is a function parameter scope,
it corresponds to a non-static member function, and
\item
for each intervening scope\iref{basic.scope.scope}
between the point at which the entity is introduced and the scope
(where \tcode{*\keyword{this}} is considered to be introduced
within the innermost enclosing class or non-lambda function definition scope),
either:
\begin{itemize}
\item the intervening scope is a block scope, or
\item the intervening scope is the function parameter scope of a \grammarterm{lambda-expression}, or
\item the intervening scope is the lambda scope of
a \grammarterm{lambda-expression}
that has a \grammarterm{simple-capture}
naming the entity or has a \grammarterm{capture-default}, and
the block scope of the \grammarterm{lambda-expression}
is also an intervening scope.
\end{itemize}
\end{itemize}
If a local entity is odr-used
in a scope in which it is not odr-usable,
the program is ill-formed.
\begin{example}
\begin{codeblock}
void f(int n) {
[] { n = 1; }; // error: \tcode{n} is not odr-usable due to intervening lambda-expression
struct A {
void f() { n = 2; } // error: \tcode{n} is not odr-usable due to intervening function definition scope
};
void g(int = n); // error: \tcode{n} is not odr-usable due to intervening function parameter scope
[=](int k = n) {}; // error: \tcode{n} is not odr-usable due to being
// outside the block scope of the \grammarterm{lambda-expression}
[&] { [n]{ return n; }; }; // OK
}
\end{codeblock}
\end{example}
\pnum
\begin{example}
\begin{codeblock}
void g() {
constexpr int x = 1;
auto lambda = [] <typename T, int = ((T)x, 0)> {}; // OK
lambda.operator()<int, 1>(); // OK, does not consider \tcode{x} at all
lambda.operator()<int>(); // OK, does not odr-use \tcode{x}
lambda.operator()<const int&>(); // error: odr-uses \tcode{x} from a context where \tcode{x} is not odr-usable
}
void h() {
constexpr int x = 1;
auto lambda = [] <typename T> { (T)x; }; // OK
lambda.operator()<int>(); // OK, does not odr-use \tcode{x}
lambda.operator()<void>(); // OK, does not odr-use \tcode{x}
lambda.operator()<const int&>(); // error: odr-uses \tcode{x} from a context where \tcode{x} is not odr-usable
}
\end{codeblock}
\end{example}
\pnum
Every program shall contain at least one definition of every
function or variable that is odr-used in that program
outside of a \deflink{discarded statement}{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.default.ctor}, \ref{class.copy.ctor},
\ref{class.dtor}, and \ref{class.copy.assign}).
\begin{example}
\begin{codeblock}
auto f() {
struct A {};
return A{};
}
decltype(f()) g();
auto x = g();
\end{codeblock}
A program containing this translation unit is ill-formed
because \tcode{g} is odr-used but not defined,
and cannot be defined in any other translation unit
because the local class \tcode{A} cannot be named outside this
translation unit.
\end{example}
\pnum
A \defn{definition domain} is
a \grammarterm{private-module-fragment} or
the portion of a translation unit
excluding its \grammarterm{private-module-fragment} (if any).
A definition of an inline function or variable shall be reachable
from the end of every definition domain
in which it is odr-used outside of a discarded statement.
\pnum
\indextext{type!incomplete}%
A definition of a class shall be reachable in every context in which
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 \link{defined}{basic.def}, or
\item a non-static class \deflink{data member}{class.mem} of
type \tcode{T} is declared, or
\item \tcode{T} is used as the allocated type or array element type in a
\grammarterm{new-expression}\iref{expr.new}, or
\item an lvalue-to-rvalue conversion is applied to
a glvalue referring
to an object of type \tcode{T}\iref{conv.lval}, or
\item an expression is converted (either implicitly or explicitly) to
type \tcode{T}\iref{conv,expr.type.conv,
expr.dynamic.cast,expr.static.cast,expr.cast}, or
\item an expression that is not a null pointer constant, and has type
other than \cv{}~\tcode{\keyword{void}*}, is converted to the type pointer to \tcode{T}
or reference to \tcode{T} using a standard conversion\iref{conv},
a \keyword{dynamic_cast}\iref{expr.dynamic.cast} or
a \keyword{static_cast}\iref{expr.static.cast}, or
\item a class member access operator is applied to an expression of type
\tcode{T}\iref{expr.ref}, or
\item the \keyword{typeid} operator\iref{expr.typeid} or the
\keyword{sizeof} operator\iref{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\iref{basic.def} or \link{called}{expr.call}, or
\item a class with a base class of type \tcode{T} is
defined\iref{class.derived}, or
\item an lvalue of type \tcode{T} is \link{assigned to}{expr.ass}, or
\item the type \tcode{T} is the subject of an
\keyword{alignof} expression\iref{expr.alignof}, or
\item an \grammarterm{exception-declaration} has type \tcode{T}, reference to
\tcode{T}, or pointer to \tcode{T}\iref{except.handle}.
\end{itemize}
\end{note}
\pnum
For any definable item \tcode{D} with definitions in multiple translation units,
\begin{itemize}
\item
if \tcode{D} is a non-inline non-templated function or variable, or
\item
if the definitions in different translation units
do not satisfy the following requirements,
\end{itemize}
the program is ill-formed;
a diagnostic is required only
if the definable item is attached to a named module and
a prior definition is reachable at the point where a later definition occurs.
Given such an item,
for all definitions of \tcode{D}, or,
if \tcode{D} is an unnamed enumeration,
for all definitions of \tcode{D} that are reachable at any given program point,
the following requirements shall be satisfied.
\begin{itemize}
\item Each such definition
shall not be attached to a named module\iref{module.unit}.
\item Each such definition shall consist of
the same sequence of tokens,
where the definition of a closure type
is considered to consist of the sequence of tokens of
the corresponding \grammarterm{lambda-expression}.
\item In each such definition, corresponding names, looked up
according to~\ref{basic.lookup}, shall refer to the same entity, after
overload resolution\iref{over.match} and after matching of partial
template specialization\iref{temp.over}, except that a name can refer to
\begin{itemize}
\item
a non-volatile 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 \link{constant expression}{expr.const},
\item is not odr-used in any definition of \tcode{D}, 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}
\item In each such definition, except within
the default arguments and default template arguments of \tcode{D},
corresponding \grammarterm{lambda-expression}{s} shall have
the same closure type (see below).
\item In each such definition, corresponding entities shall have the
same language linkage.
\item In each such definition,
const objects with static or thread storage duration
shall be constant-initialized if
the object is constant-initialized in any such definition.
\item In each such definition,
corresponding manifestly constant-evaluated expressions
that are not value-dependent
shall have the same value\iref{expr.const,temp.dep.constexpr}.
\item In each such definition, 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.
\item In each such definition,
a default argument used by an (implicit or explicit) function call or
a default template argument used by an (implicit or explicit)
\grammarterm{template-id} or \grammarterm{simple-template-id}
is treated as if its token sequence
were present in the definition of \tcode{D};
that is, the default argument or default template argument
is subject to the requirements described in this paragraph (recursively).
\end{itemize}
\pnum
For the purposes of the preceding requirements:
\begin{itemize}
\item If \tcode{D} is a class with an implicitly-declared
constructor\iref{class.default.ctor,class.copy.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 d1; // \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;
};
D d2; // \tcode{X(int, int, int)} called by \tcode{D()};
// \tcode{D()}'s implicit definition violates the ODR
\end{codeblock}
\end{example}
\item If \tcode{D} is a class with
a defaulted three-way comparison operator function\iref{class.spaceship},
it is as if the operator was
implicitly defined in every translation unit where it is odr-used, and the
implicit definition in every translation unit shall call the same
comparison operators for each subobject of \tcode{D}.
\item
If \tcode{D} is a template and is defined in more than one
translation unit, the requirements
apply both to names from the template's enclosing scope used in the
template definition, and also to dependent names at
the point of instantiation\iref{temp.dep}.
\end{itemize}
\pnum
These requirements also apply to corresponding entities
defined within each definition of \tcode{D}
(including the closure types of \grammarterm{lambda-expression}{s},
but excluding entities defined within default arguments or
default template arguments of either \tcode{D} or
an entity not defined within \tcode{D}).
For each such entity and for \tcode{D} itself,
the behavior is as if there is a single entity with a single definition,
including in the application of these requirements to other entities.
\begin{note}
The entity is still declared in multiple translation units, and \ref{basic.link}
still applies to these declarations. In particular,
\grammarterm{lambda-expression}{s}\iref{expr.prim.lambda}
appearing in the type of \tcode{D} can result
in the different declarations having distinct types, and
\grammarterm{lambda-expression}{s} appearing in a default argument of \tcode{D}
might still denote different types in different translation units.
\end{note}
\pnum
\begin{example}
\begin{codeblock}
inline void f(bool cond, void (*p)()) {
if (cond) f(false, []{});
}
inline void g(bool cond, void (*p)() = []{}) {
if (cond) g(false);
}
struct X {
void h(bool cond, void (*p)() = []{}) {
if (cond) h(false);
}
};
\end{codeblock}
If the definition of \tcode{f} appears in multiple translation units,
the behavior of the program is as if
there is only one definition of \tcode{f}.
If the definition of \tcode{g} appears in multiple translation units,
the program is ill-formed (no diagnostic required) because
each such definition uses a default argument that
refers to a distinct \grammarterm{lambda-expression} closure type.
The definition of \tcode{X} can appear
in multiple translation units of a valid program;
the \grammarterm{lambda-expression}{s} defined within
the default argument of \tcode{X::h} within the definition of \tcode{X}
denote the same closure type in each translation unit.
\end{example}
\pnum
If, at any point in the program,
there is more than one
reachable unnamed enumeration definition in the same scope
that have the same first enumerator name and
do not have typedef names for linkage purposes\iref{dcl.enum},
those unnamed enumeration types shall be the same; no diagnostic required.
\indextext{one-definition rule|)}
\rSec1[basic.scope]{Scope}%
\indextext{scope|(}
\rSec2[basic.scope.scope]{General}
\pnum
The declarations in a program appear in a number of \defnx{scopes}{scope}
that are in general discontiguous.
The \defnadj{global}{scope} contains the entire program;
every other scope $S$ is introduced by a
declaration,
\grammarterm{parameter-declaration-clause},
\grammarterm{statement}, or \grammarterm{handler}
(as described in the following subclauses of \ref{basic.scope})
appearing in another scope which thereby contains $S$.
An \defnadj{enclosing}{scope} at a program point is any scope that contains it;
the smallest such scope is said to be the \defnadj{immediate}{scope}
at that point.
A scope \defnx{intervenes}{scope!intervene}
between a program point $P$ and a scope $S$
(that does not contain $P$) if it is or contains $S$ but does not contain $P$.
\pnum
Unless otherwise specified:
\begin{itemize}
\item
The smallest scope that contains a scope $S$ is
the \defnadj{parent}{scope} of $S$.
\item
No two declarations (re)introduce the same entity.
\item
A declaration \defnx{inhabits}{scope!inhabit}
the immediate scope at its locus\iref{basic.scope.pdecl}.
\item
A declaration's \defnadj{target}{scope} is the scope it inhabits.
\item
Any names (re)introduced by a declaration are \defnx{bound}{name!bound} to it
in its target scope.
\end{itemize}
An entity \defnx{belongs}{entity!belong} to a scope $S$
if $S$ is the target scope of a declaration of the entity.
\begin{note}
Special cases include that:
\begin{itemize}
\item
Template parameter scopes are parents
only to other template parameter scopes\iref{basic.scope.temp}.
\item
Corresponding declarations with appropriate linkage
declare the same entity\iref{basic.link}.
\item
The declaration in a \grammarterm{template-declaration}
inhabits the same scope as the \grammarterm{template-declaration}.
\item
Friend declarations and
declarations of template specializations do not bind names\iref{dcl.meaning};
those with qualified names target a specified scope, and
other friend declarations and
certain \grammarterm{elaborated-type-specifier}s\iref{dcl.type.elab}
target a larger enclosing scope.
\item
Block-scope extern declarations target a larger enclosing scope
but bind a name in their immediate scope.
\item
The names of unscoped enumerators are bound
in the two innermost enclosing scopes\iref{dcl.enum}.
\item
A class's name is also bound in its own scope\iref{class.pre}.
\item
The names of the members of an anonymous union are bound in
the union's parent scope\iref{class.union.anon}.
\end{itemize}
\end{note}
\pnum
Two non-static member functions have
\defnadjx{corresponding}{object parameters}{object parameter} if
\begin{itemize}
\item
exactly one is an implicit object member function
with no \grammarterm{ref-qualifier} and
the types of their object parameters\iref{dcl.fct},
after removing references,
are the same, or
\item
their object parameters have the same type.
\end{itemize}
\indextext{template!function!corresponding object parameter}%
Two non-static member function templates have
\defnadjx{corresponding}{object parameters}{object parameter} if
\begin{itemize}
\item
exactly one is an implicit object member function
with no \grammarterm{ref-qualifier} and
the types of their object parameters,
after removing any references,
are equivalent, or
\item
the types of their object parameters are equivalent.
\end{itemize}
\indextext{template!function!corresponding signature}%
Two function templates have
\defnadjx{corresponding}{signatures}{signature} if
their \grammarterm{template-parameter-list}{s}
have the same length,
their corresponding \grammarterm{template-parameter}{s} are equivalent,
they have equivalent non-object-parameter-type-lists and return types (if any), and,
if both are non-static members, they have corresponding object parameters.
\pnum
Two declarations \defn{correspond}
if they (re)introduce the same name,
both declare constructors, or
both declare destructors,
unless
\begin{itemize}
\item
either is a \grammarterm{using-declarator}, or
\item
one declares a type (not a \grammarterm{typedef-name}) and the other declares a
variable,
non-static data member other than of an anonymous union\iref{class.union.anon},
enumerator,
function, or
function template, or
\item
each declares a function or function template
and they do not declare corresponding overloads.
\end{itemize}
Two function or function template declarations declare
\defn{corresponding overloads} if
\begin{itemize}
\item
both declare functions with the same non-object-parameter-type-list,
\begin{footnote}
An implicit object parameter\iref{over.match.funcs}
is not part of the parameter-type-list.
\end{footnote}
equivalent\iref{temp.over.link} trailing \grammarterm{requires-clause}s
(if any, except as specified in \ref{temp.friend}), and,
if both are non-static members,
they have corresponding object parameters, or
\item
both declare function templates with corresponding signatures and equivalent
\grammarterm{template-head}s and
trailing \grammarterm{requires-clause}s (if any).
\end{itemize}
\begin{note}
Declarations can correspond even if neither binds a name.
\begin{example}
\begin{codeblock}
struct A {
friend void f(); // \#1
};
struct B {
friend void f() {} // corresponds to, and defines, \#1
};
\end{codeblock}
\end{example}
\end{note}
\begin{example}
\begin{codeblock}
typedef int Int;
enum E : int { a };
void f(int); // \#1
void f(Int) {} // defines \#1
void f(E) {} // OK, another overload