From Jason Turner

[basic.def.odr]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp8hsjkniz/{from.md → to.md} +87 -78
tmp/tmp8hsjkniz/{from.md → to.md} RENAMED
@@ -1,14 +1,23 @@
1
  ## One-definition rule <a id="basic.def.odr">[[basic.def.odr]]</a>
2
 
 
 
 
 
 
 
 
 
 
 
 
3
  No translation unit shall contain more than one definition of any
4
- variable, function, class type, enumeration type, template, default
5
- argument for a parameter (for a function in a given scope), or default
6
- template argument.
7
 
8
  An expression or conversion is *potentially evaluated* unless it is an
9
- unevaluated operand [[expr.prop]], a subexpression thereof, or a
10
  conversion in an initialization or conversion sequence in such a
11
  context. The set of *potential results* of an expression E is defined as
12
  follows:
13
 
14
  - If E is an *id-expression* [[expr.prim.id]], the set contains only E.
@@ -53,52 +62,55 @@ int n = b ? (1, S::x) // S::x is not odr-used here
53
  — *end note*]
54
 
55
  A function is *named by* an expression or conversion as follows:
56
 
57
  - A function is named by an expression or conversion if it is the
58
- selected member of an overload set ([[basic.lookup]], [[over.match]],
59
- [[over.over]]) in an overload resolution performed as part of forming
60
- that expression or conversion, unless it is a pure virtual function
61
- and either the expression is not an *id-expression* naming the
62
- function with an explicitly qualified name or the expression forms a
63
- pointer to member [[expr.unary.op]]. \[*Note 2*: This covers taking
64
- the address of functions ([[conv.func]], [[expr.unary.op]]), calls to
65
- named functions [[expr.call]], operator overloading [[over]],
66
- user-defined conversions [[class.conv.fct]], allocation functions for
67
- *new-expression*s [[expr.new]], as well as non-default initialization
68
- [[dcl.init]]. A constructor selected to copy or move an object of
69
- class type is considered to be named by an expression or conversion
70
- even if the call is actually elided by the implementation
 
71
  [[class.copy.elision]]. — *end note*]
72
  - A deallocation function for a class is named by a *new-expression* if
73
  it is the single matching deallocation function for the allocation
74
  function selected by overload resolution, as specified in 
75
  [[expr.new]].
76
  - A deallocation function for a class is named by a *delete-expression*
77
  if it is the selected usual deallocation function as specified in 
78
  [[expr.delete]] and  [[class.free]].
79
 
80
- A variable `x` whose name appears as a potentially-evaluated expression
81
- E is *odr-used* by E unless
 
82
 
83
  - `x` is a reference that is usable in constant expressions
84
  [[expr.const]], or
85
  - `x` is a variable of non-reference type that is usable in constant
86
  expressions and has no mutable subobjects, and E is an element of the
87
  set of potential results of an expression of non-volatile-qualified
88
  non-class type to which the lvalue-to-rvalue conversion [[conv.lval]]
89
  is applied, or
90
  - `x` is a variable of non-reference type, and E is an element of the
91
- set of potential results of a discarded-value expression [[expr.prop]]
92
- to which the lvalue-to-rvalue conversion is not applied.
 
93
 
94
  A structured binding is odr-used if it appears as a
95
  potentially-evaluated expression.
96
 
97
  `*this` is odr-used if `this` appears as a potentially-evaluated
98
  expression (including as the result of the implicit transformation in
99
- the body of a non-static member function ([[class.mfct.non-static]])).
100
 
101
  A virtual member function is odr-used if it is not pure. A function is
102
  odr-used if it is named by a potentially-evaluated expression or
103
  conversion. A non-placement allocation or deallocation function for a
104
  class is odr-used by the definition of a constructor of that class. A
@@ -111,29 +123,29 @@ An assignment operator function in a class is odr-used by an
111
  implicitly-defined copy-assignment or move-assignment function for
112
  another class as specified in  [[class.copy.assign]]. A constructor for
113
  a class is odr-used as specified in  [[dcl.init]]. A destructor for a
114
  class is odr-used if it is potentially invoked [[class.dtor]].
115
 
116
- A local entity [[basic.pre]] is *odr-usable* in a declarative region
117
- [[basic.scope.declarative]] if:
118
 
119
  - either the local entity is not `*this`, or an enclosing class or
120
  non-lambda function parameter scope exists and, if the innermost such
121
  scope is a function parameter scope, it corresponds to a non-static
122
  member function, and
123
- - for each intervening declarative region [[basic.scope.declarative]]
124
- between the point at which the entity is introduced and the region
125
- (where `*this` is considered to be introduced within the innermost
126
- enclosing class or non-lambda function definition scope), either:
127
- - the intervening declarative region is a block scope, or
128
- - the intervening declarative region is the function parameter scope
129
- of a *lambda-expression* that has a *simple-capture* naming the
130
- entity or has a *capture-default*, and the block scope of the
131
- *lambda-expression* is also an intervening declarative region.
132
 
133
- If a local entity is odr-used in a declarative region in which it is not
134
- odr-usable, the program is ill-formed.
135
 
136
  [*Example 2*:
137
 
138
  ``` cpp
139
  void f(int n) {
@@ -148,15 +160,15 @@ void f(int n) {
148
  }
149
  ```
150
 
151
  — *end example*]
152
 
153
- Every program shall contain exactly one definition of every non-inline
154
- function or variable that is odr-used in that program outside of a
155
- discarded statement [[stmt.if]]; no diagnostic required. The definition
156
- can appear explicitly in the program, it can be found in the standard or
157
- a user-defined library, or (when appropriate) it is implicitly defined
158
  (see  [[class.default.ctor]], [[class.copy.ctor]], [[class.dtor]], and
159
  [[class.copy.assign]]).
160
 
161
  [*Example 3*:
162
 
@@ -180,13 +192,12 @@ A *definition domain* is a *private-module-fragment* or the portion of a
180
  translation unit excluding its *private-module-fragment* (if any). A
181
  definition of an inline function or variable shall be reachable from the
182
  end of every definition domain in which it is odr-used outside of a
183
  discarded statement.
184
 
185
- A definition of a class is required to be reachable in every context in
186
- which the class is used in a way that requires the class type to be
187
- complete.
188
 
189
  [*Example 4*:
190
 
191
  The following complete translation unit is well-formed, even though it
192
  never defines `X`:
@@ -210,12 +221,13 @@ complete class types are required. A class type `T` must be complete if:
210
  - `T` is used as the allocated type or array element type in a
211
  *new-expression* [[expr.new]], or
212
  - an lvalue-to-rvalue conversion is applied to a glvalue referring to an
213
  object of type `T` [[conv.lval]], or
214
  - an expression is converted (either implicitly or explicitly) to type
215
- `T` ([[conv]], [[expr.type.conv]], [[expr.dynamic.cast]],
216
- [[expr.static.cast]], [[expr.cast]]), or
 
217
  - an expression that is not a null pointer constant, and has type other
218
  than cv `void*`, is converted to the type pointer to `T` or reference
219
  to `T` using a standard conversion [[conv]], a `dynamic_cast`
220
  [[expr.dynamic.cast]] or a `static_cast` [[expr.static.cast]], or
221
  - a class member access operator is applied to an expression of type `T`
@@ -231,26 +243,23 @@ complete class types are required. A class type `T` must be complete if:
231
  - an *exception-declaration* has type `T`, reference to `T`, or pointer
232
  to `T` [[except.handle]].
233
 
234
  — *end note*]
235
 
236
- There can be more than one definition of a
 
237
 
238
- - class type [[class]],
239
- - enumeration type [[dcl.enum]],
240
- - inline function or variable [[dcl.inline]],
241
- - templated entity [[temp.pre]],
242
- - default argument for a parameter (for a function in a given scope)
243
- [[dcl.fct.default]], or
244
- - default template argument [[temp.param]]
245
 
246
- in a program provided that each definition appears in a different
247
- translation unit and the definitions satisfy the following requirements.
248
- Given such an entity `D` defined in more than one translation unit, for
249
- all definitions of `D`, or, if `D` is an unnamed enumeration, for all
250
- definitions of `D` that are reachable at any given program point, the
251
- following requirements shall be satisfied.
252
 
253
  - Each such definition shall not be attached to a named module
254
  [[module.unit]].
255
  - Each such definition shall consist of the same sequence of tokens,
256
  where the definition of a closure type is considered to consist of the
@@ -273,22 +282,28 @@ following requirements shall be satisfied.
273
  - In each such definition, except within the default arguments and
274
  default template arguments of `D`, corresponding *lambda-expression*s
275
  shall have the same closure type (see below).
276
  - In each such definition, corresponding entities shall have the same
277
  language linkage.
 
 
 
 
 
 
278
  - In each such definition, the overloaded operators referred to, the
279
  implicit calls to conversion functions, constructors, operator new
280
  functions and operator delete functions, shall refer to the same
281
  function.
282
  - In each such definition, a default argument used by an (implicit or
283
  explicit) function call or a default template argument used by an
284
  (implicit or explicit) *template-id* or *simple-template-id* is
285
  treated as if its token sequence were present in the definition of
286
  `D`; that is, the default argument or default template argument is
287
  subject to the requirements described in this paragraph (recursively).
288
- - If `D` is a class with an implicitly-declared constructor (
289
- [[class.default.ctor]], [[class.copy.ctor]]), it is as if the
290
  constructor was implicitly defined in every translation unit where it
291
  is odr-used, and the implicit definition in every translation unit
292
  shall call the same constructor for a subobject of `D`.
293
  \[*Example 5*:
294
  ``` cpp
@@ -323,33 +338,27 @@ following requirements shall be satisfied.
323
  implicit definition in every translation unit shall call the same
324
  comparison operators for each subobject of `D`.
325
 
326
  If `D` is a template and is defined in more than one translation unit,
327
  then the preceding requirements shall apply both to names from the
328
- template’s enclosing scope used in the template definition
329
- [[temp.nondep]], and also to dependent names at the point of
330
- instantiation [[temp.dep]]. These requirements also apply to
331
- corresponding entities defined within each definition of `D` (including
332
- the closure types of *lambda-expression*s, but excluding entities
333
- defined within default arguments or default template arguments of either
334
- `D` or an entity not defined within `D`). For each such entity and for
335
- `D` itself, the behavior is as if there is a single entity with a single
336
- definition, including in the application of these requirements to other
337
- entities.
338
 
339
  [*Note 4*: The entity is still declared in multiple translation units,
340
  and [[basic.link]] still applies to these declarations. In particular,
341
  *lambda-expression*s [[expr.prim.lambda]] appearing in the type of `D`
342
- may result in the different declarations having distinct types, and
343
- *lambda-expression*s appearing in a default argument of `D` may still
344
  denote different types in different translation units. — *end note*]
345
 
346
- If these definitions do not satisfy these requirements, then the program
347
- is ill-formed; a diagnostic is required only if the entity is attached
348
- to a named module and a prior definition is reachable at the point where
349
- a later definition occurs.
350
-
351
  [*Example 6*:
352
 
353
  ``` cpp
354
  inline void f(bool cond, void (*p)()) {
355
  if (cond) f(false, []{});
 
1
  ## One-definition rule <a id="basic.def.odr">[[basic.def.odr]]</a>
2
 
3
+ Each of the following is termed a *definable item*:
4
+
5
+ - a class type [[class]],
6
+ - an enumeration type [[dcl.enum]],
7
+ - a function [[dcl.fct]],
8
+ - a variable [[basic.pre]],
9
+ - a templated entity [[temp.pre]],
10
+ - a default argument for a parameter (for a function in a given scope)
11
+ [[dcl.fct.default]], or
12
+ - a default template argument [[temp.param]].
13
+
14
  No translation unit shall contain more than one definition of any
15
+ definable item.
 
 
16
 
17
  An expression or conversion is *potentially evaluated* unless it is an
18
+ unevaluated operand [[expr.context]], a subexpression thereof, or a
19
  conversion in an initialization or conversion sequence in such a
20
  context. The set of *potential results* of an expression E is defined as
21
  follows:
22
 
23
  - If E is an *id-expression* [[expr.prim.id]], the set contains only E.
 
62
  — *end note*]
63
 
64
  A function is *named by* an expression or conversion as follows:
65
 
66
  - A function is named by an expression or conversion if it is the
67
+ selected member of an overload set
68
+ [[basic.lookup]], [[over.match]], [[over.over]] in an overload
69
+ resolution performed as part of forming that expression or conversion,
70
+ unless it is a pure virtual function and either the expression is not
71
+ an *id-expression* naming the function with an explicitly qualified
72
+ name or the expression forms a pointer to member [[expr.unary.op]].
73
+ \[*Note 2*: This covers taking the address of functions
74
+ [[conv.func]], [[expr.unary.op]], calls to named functions
75
+ [[expr.call]], operator overloading [[over]], user-defined conversions
76
+ [[class.conv.fct]], allocation functions for *new-expression*s
77
+ [[expr.new]], as well as non-default initialization [[dcl.init]]. A
78
+ constructor selected to copy or move an object of class type is
79
+ considered to be named by an expression or conversion even if the call
80
+ is actually elided by the implementation
81
  [[class.copy.elision]]. — *end note*]
82
  - A deallocation function for a class is named by a *new-expression* if
83
  it is the single matching deallocation function for the allocation
84
  function selected by overload resolution, as specified in 
85
  [[expr.new]].
86
  - A deallocation function for a class is named by a *delete-expression*
87
  if it is the selected usual deallocation function as specified in 
88
  [[expr.delete]] and  [[class.free]].
89
 
90
+ A variable is named by an expression if the expression is an
91
+ *id-expression* that denotes it. A variable `x` that is named by a
92
+ potentially-evaluated expression E is *odr-used* by E unless
93
 
94
  - `x` is a reference that is usable in constant expressions
95
  [[expr.const]], or
96
  - `x` is a variable of non-reference type that is usable in constant
97
  expressions and has no mutable subobjects, and E is an element of the
98
  set of potential results of an expression of non-volatile-qualified
99
  non-class type to which the lvalue-to-rvalue conversion [[conv.lval]]
100
  is applied, or
101
  - `x` is a variable of non-reference type, and E is an element of the
102
+ set of potential results of a discarded-value expression
103
+ [[expr.context]] to which the lvalue-to-rvalue conversion is not
104
+ applied.
105
 
106
  A structured binding is odr-used if it appears as a
107
  potentially-evaluated expression.
108
 
109
  `*this` is odr-used if `this` appears as a potentially-evaluated
110
  expression (including as the result of the implicit transformation in
111
+ the body of a non-static member function [[class.mfct.non.static]]).
112
 
113
  A virtual member function is odr-used if it is not pure. A function is
114
  odr-used if it is named by a potentially-evaluated expression or
115
  conversion. A non-placement allocation or deallocation function for a
116
  class is odr-used by the definition of a constructor of that class. A
 
123
  implicitly-defined copy-assignment or move-assignment function for
124
  another class as specified in  [[class.copy.assign]]. A constructor for
125
  a class is odr-used as specified in  [[dcl.init]]. A destructor for a
126
  class is odr-used if it is potentially invoked [[class.dtor]].
127
 
128
+ A local entity [[basic.pre]] is *odr-usable* in a scope
129
+ [[basic.scope.scope]] if:
130
 
131
  - either the local entity is not `*this`, or an enclosing class or
132
  non-lambda function parameter scope exists and, if the innermost such
133
  scope is a function parameter scope, it corresponds to a non-static
134
  member function, and
135
+ - for each intervening scope [[basic.scope.scope]] between the point at
136
+ which the entity is introduced and the scope (where `*this` is
137
+ considered to be introduced within the innermost enclosing class or
138
+ non-lambda function definition scope), either:
139
+ - the intervening scope is a block scope, or
140
+ - the intervening scope is the function parameter scope of a
141
+ *lambda-expression* that has a *simple-capture* naming the entity or
142
+ has a *capture-default*, and the block scope of the
143
+ *lambda-expression* is also an intervening scope.
144
 
145
+ If a local entity is odr-used in a scope in which it is not odr-usable,
146
+ the program is ill-formed.
147
 
148
  [*Example 2*:
149
 
150
  ``` cpp
151
  void f(int n) {
 
160
  }
161
  ```
162
 
163
  — *end example*]
164
 
165
+ Every program shall contain at least one definition of every function or
166
+ variable that is odr-used in that program outside of a discarded
167
+ statement [[stmt.if]]; no diagnostic required. The definition can appear
168
+ explicitly in the program, it can be found in the standard or a
169
+ user-defined library, or (when appropriate) it is implicitly defined
170
  (see  [[class.default.ctor]], [[class.copy.ctor]], [[class.dtor]], and
171
  [[class.copy.assign]]).
172
 
173
  [*Example 3*:
174
 
 
192
  translation unit excluding its *private-module-fragment* (if any). A
193
  definition of an inline function or variable shall be reachable from the
194
  end of every definition domain in which it is odr-used outside of a
195
  discarded statement.
196
 
197
+ A definition of a class shall be reachable in every context in which the
198
+ class is used in a way that requires the class type to be complete.
 
199
 
200
  [*Example 4*:
201
 
202
  The following complete translation unit is well-formed, even though it
203
  never defines `X`:
 
221
  - `T` is used as the allocated type or array element type in a
222
  *new-expression* [[expr.new]], or
223
  - an lvalue-to-rvalue conversion is applied to a glvalue referring to an
224
  object of type `T` [[conv.lval]], or
225
  - an expression is converted (either implicitly or explicitly) to type
226
+ `T`
227
+ [[conv]], [[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]], [[expr.cast]],
228
+ or
229
  - an expression that is not a null pointer constant, and has type other
230
  than cv `void*`, is converted to the type pointer to `T` or reference
231
  to `T` using a standard conversion [[conv]], a `dynamic_cast`
232
  [[expr.dynamic.cast]] or a `static_cast` [[expr.static.cast]], or
233
  - a class member access operator is applied to an expression of type `T`
 
243
  - an *exception-declaration* has type `T`, reference to `T`, or pointer
244
  to `T` [[except.handle]].
245
 
246
  — *end note*]
247
 
248
+ For any definable item `D` with definitions in multiple translation
249
+ units,
250
 
251
+ - if `D` is a non-inline non-templated function or variable, or
252
+ - if the definitions in different translation units do not satisfy the
253
+ following requirements,
 
 
 
 
254
 
255
+ the program is ill-formed; a diagnostic is required only if the
256
+ definable item is attached to a named module and a prior definition is
257
+ reachable at the point where a later definition occurs. Given such an
258
+ item, for all definitions of `D`, or, if `D` is an unnamed enumeration,
259
+ for all definitions of `D` that are reachable at any given program
260
+ point, the following requirements shall be satisfied.
261
 
262
  - Each such definition shall not be attached to a named module
263
  [[module.unit]].
264
  - Each such definition shall consist of the same sequence of tokens,
265
  where the definition of a closure type is considered to consist of the
 
282
  - In each such definition, except within the default arguments and
283
  default template arguments of `D`, corresponding *lambda-expression*s
284
  shall have the same closure type (see below).
285
  - In each such definition, corresponding entities shall have the same
286
  language linkage.
287
+ - In each such definition, const objects with static or thread storage
288
+ duration shall be constant-initialized if the object is
289
+ constant-initialized in any such definition.
290
+ - In each such definition, corresponding manifestly constant-evaluated
291
+ expressions that are not value-dependent shall have the same value
292
+ [[expr.const]], [[temp.dep.constexpr]].
293
  - In each such definition, the overloaded operators referred to, the
294
  implicit calls to conversion functions, constructors, operator new
295
  functions and operator delete functions, shall refer to the same
296
  function.
297
  - In each such definition, a default argument used by an (implicit or
298
  explicit) function call or a default template argument used by an
299
  (implicit or explicit) *template-id* or *simple-template-id* is
300
  treated as if its token sequence were present in the definition of
301
  `D`; that is, the default argument or default template argument is
302
  subject to the requirements described in this paragraph (recursively).
303
+ - If `D` is a class with an implicitly-declared constructor
304
+ [[class.default.ctor]], [[class.copy.ctor]], it is as if the
305
  constructor was implicitly defined in every translation unit where it
306
  is odr-used, and the implicit definition in every translation unit
307
  shall call the same constructor for a subobject of `D`.
308
  \[*Example 5*:
309
  ``` cpp
 
338
  implicit definition in every translation unit shall call the same
339
  comparison operators for each subobject of `D`.
340
 
341
  If `D` is a template and is defined in more than one translation unit,
342
  then the preceding requirements shall apply both to names from the
343
+ template’s enclosing scope used in the template definition, and also to
344
+ dependent names at the point of instantiation [[temp.dep]]. These
345
+ requirements also apply to corresponding entities defined within each
346
+ definition of `D` (including the closure types of *lambda-expression*s,
347
+ but excluding entities defined within default arguments or default
348
+ template arguments of either `D` or an entity not defined within `D`).
349
+ For each such entity and for `D` itself, the behavior is as if there is
350
+ a single entity with a single definition, including in the application
351
+ of these requirements to other entities.
 
352
 
353
  [*Note 4*: The entity is still declared in multiple translation units,
354
  and [[basic.link]] still applies to these declarations. In particular,
355
  *lambda-expression*s [[expr.prim.lambda]] appearing in the type of `D`
356
+ can result in the different declarations having distinct types, and
357
+ *lambda-expression*s appearing in a default argument of `D` might still
358
  denote different types in different translation units. — *end note*]
359
 
 
 
 
 
 
360
  [*Example 6*:
361
 
362
  ``` cpp
363
  inline void f(bool cond, void (*p)()) {
364
  if (cond) f(false, []{});