From Jason Turner

[basic.def.odr]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpx1tyugmc/{from.md → to.md} +105 -64
tmp/tmpx1tyugmc/{from.md → to.md} RENAMED
@@ -1,16 +1,18 @@
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, or template.
5
 
6
  An expression is *potentially evaluated* unless it is an unevaluated
7
  operand (Clause  [[expr]]) or a subexpression thereof. The set of
8
  *potential results* of an expression `e` is defined as follows:
9
 
10
- - If `e` is an *id-expression* ([[expr.prim.general]]), the set
11
- contains only `e`.
 
 
12
  - If `e` is a class member access expression ([[expr.ref]]), the set
13
  contains the potential results of the object expression.
14
  - If `e` is a pointer-to-member expression ([[expr.mptr.oper]]) whose
15
  second operand is a constant expression, the set contains the
16
  potential results of the object expression.
@@ -21,23 +23,32 @@ operand (Clause  [[expr]]) or a subexpression thereof. The set of
21
  operands.
22
  - If `e` is a comma expression ([[expr.comma]]), the set contains the
23
  potential results of the right operand.
24
  - Otherwise, the set is empty.
25
 
 
 
26
  This set is a (possibly-empty) set of *id-expression*s, each of which is
27
- either `e` or a subexpression of `e`. In the following example, the set
28
- of potential results of the initializer of `n` contains the first `S::x`
29
- subexpression, but not the second `S::x` subexpression.
 
 
 
 
30
 
31
  ``` cpp
32
  struct S { static const int x = 0; };
33
  const int &f(const int &r);
34
  int n = b ? (1, S::x) // S::x is not odr-used here
35
- : f(S::x); // S::x is odr-used here, so
36
- // a definition is required
37
  ```
38
 
 
 
 
 
39
  A variable `x` whose name appears as a potentially-evaluated expression
40
  `ex` is *odr-used* by `ex` unless applying the lvalue-to-rvalue
41
  conversion ([[conv.lval]]) to `x` yields a constant expression (
42
  [[expr.const]]) that does not invoke any non-trivial functions and, if
43
  `x` is an object, `ex` is an element of the set of potential results of
@@ -49,70 +60,82 @@ implicit transformation in the body of a non-static member function (
49
  [[class.mfct.non-static]])). A virtual member function is odr-used if it
50
  is not pure. A function whose name appears as a potentially-evaluated
51
  expression is odr-used if it is the unique lookup result or the selected
52
  member of a set of overloaded functions ([[basic.lookup]],
53
  [[over.match]], [[over.over]]), unless it is a pure virtual function and
54
- its name is not explicitly qualified. This covers calls to named
55
- functions ([[expr.call]]), operator overloading (Clause  [[over]]),
56
- user-defined conversions ([[class.conv.fct]]), allocation function for
57
- placement new ([[expr.new]]), as well as non-default initialization (
58
- [[dcl.init]]). A constructor selected to copy or move an object of class
59
- type is odr-used even if the call is actually elided by the
60
- implementation ([[class.copy]]). An allocation or deallocation function
61
- for a class is odr-used by a new expression appearing in a
62
- potentially-evaluated expression as specified in  [[expr.new]] and 
63
- [[class.free]]. A deallocation function for a class is odr-used by a
64
- delete expression appearing in a potentially-evaluated expression as
65
- specified in  [[expr.delete]] and  [[class.free]]. A non-placement
66
- allocation or deallocation function for a class is odr-used by the
67
- definition of a constructor of that class. A non-placement deallocation
68
- function for a class is odr-used by the definition of the destructor of
69
- that class, or by being selected by the lookup at the point of
70
- definition of a virtual destructor ([[class.dtor]]).[^2] An assignment
71
- operator function in a class is odr-used by an implicitly-defined
72
- copy-assignment or move-assignment function for another class as
73
- specified in  [[class.copy]]. A default constructor for a class is
74
- odr-used by default initialization or value initialization as specified
75
- in  [[dcl.init]]. A constructor for a class is odr-used as specified in 
76
- [[dcl.init]]. A destructor for a class is odr-used if it is potentially
77
- invoked ([[class.dtor]]).
 
 
 
78
 
79
  Every program shall contain exactly one definition of every non-inline
80
- function or variable that is odr-used in that program; no diagnostic
81
- required. The definition can appear explicitly in the program, it can be
82
- found in the standard or a user-defined library, or (when appropriate)
83
- it is implicitly defined (see  [[class.ctor]], [[class.dtor]] and
84
- [[class.copy]]). An inline function shall be defined in every
85
- translation unit in which it is odr-used.
 
 
86
 
87
  Exactly one definition of a class is required in a translation unit if
88
  the class is used in a way that requires the class type to be complete.
89
- the following complete translation unit is well-formed, even though it
 
 
 
90
  never defines `X`:
91
 
92
  ``` cpp
93
  struct X; // declare X as a struct type
94
  struct X* x1; // use X in pointer formation
95
  X* x2; // use X in pointer formation
96
  ```
97
 
 
 
 
 
98
  The rules for declarations and expressions describe in which contexts
99
  complete class types are required. A class type `T` must be complete if:
100
 
101
  - an object of type `T` is defined ([[basic.def]]), or
102
  - a non-static class data member of type `T` is declared (
103
  [[class.mem]]), or
104
- - `T` is used as the object type or array element type in a
105
  *new-expression* ([[expr.new]]), or
106
  - an lvalue-to-rvalue conversion is applied to a glvalue referring to an
107
  object of type `T` ([[conv.lval]]), or
108
  - an expression is converted (either implicitly or explicitly) to type
109
  `T` (Clause  [[conv]], [[expr.type.conv]], [[expr.dynamic.cast]],
110
  [[expr.static.cast]], [[expr.cast]]), or
111
  - an expression that is not a null pointer constant, and has type other
112
- than *cv* `void*`, is converted to the type pointer to `T` or
113
- reference to `T` using a standard conversion (Clause  [[conv]]), a
114
  `dynamic_cast` ([[expr.dynamic.cast]]) or a `static_cast` (
115
  [[expr.static.cast]]), or
116
  - a class member access operator is applied to an expression of type
117
  `T` ([[expr.ref]]), or
118
  - the `typeid` operator ([[expr.typeid]]) or the `sizeof` operator (
@@ -125,15 +148,18 @@ complete class types are required. A class type `T` must be complete if:
125
  - the type `T` is the subject of an `alignof` expression (
126
  [[expr.alignof]]), or
127
  - an *exception-declaration* has type `T`, reference to `T`, or pointer
128
  to `T` ([[except.handle]]).
129
 
 
 
130
  There can be more than one definition of a class type (Clause 
131
  [[class]]), enumeration type ([[dcl.enum]]), inline function with
132
- external linkage ([[dcl.fct.spec]]), class template (Clause  [[temp]]),
133
- non-static function template ([[temp.fct]]), static data member of a
134
- class template ([[temp.static]]), member function of a class template (
 
135
  [[temp.mem.func]]), or template specialization for which some template
136
  parameters are not specified ([[temp.spec]], [[temp.class.spec]]) in a
137
  program provided that each definition appears in a different translation
138
  unit, and provided the definitions satisfy the following requirements.
139
  Given such an entity named `D` defined in more than one translation
@@ -143,54 +169,69 @@ unit, then
143
  and
144
  - in each definition of `D`, corresponding names, looked up according
145
  to  [[basic.lookup]], shall refer to an entity defined within the
146
  definition of `D`, or shall refer to the same entity, after overload
147
  resolution ([[over.match]]) and after matching of partial template
148
- specialization ([[temp.over]]), except that a name can refer to a
149
- non-volatile `const` object with internal or no linkage if the object
150
- has the same literal type in all definitions of `D`, and the object is
151
- initialized with a constant expression ([[expr.const]]), and the
152
- object is not odr-used, and the object has the same value in all
153
- definitions of `D`; and
 
 
 
 
 
 
 
 
154
  - in each definition of `D`, corresponding entities shall have the same
155
  language linkage; and
156
  - in each definition of `D`, the overloaded operators referred to, the
157
  implicit calls to conversion functions, constructors, operator new
158
  functions and operator delete functions, shall refer to the same
159
  function, or to a function defined within the definition of `D`; and
160
  - in each definition of `D`, a default argument used by an (implicit or
161
  explicit) function call is treated as if its token sequence were
162
  present in the definition of `D`; that is, the default argument is
163
- subject to the three requirements described above (and, if the default
164
- argument has sub-expressions with default arguments, this requirement
165
- applies recursively).[^3]
166
  - if `D` is a class with an implicitly-declared constructor (
167
  [[class.ctor]]), it is as if the constructor was implicitly defined in
168
  every translation unit where it is odr-used, and the implicit
169
  definition in every translation unit shall call the same constructor
170
- for a base class or a class member of `D`.
 
171
  ``` cpp
172
  // translation unit 1:
173
  struct X {
174
- X(int);
175
  X(int, int);
 
176
  };
177
- X::X(int = 0) { }
178
- class D: public X { };
179
- D d2; // X(int) called by D()
 
 
180
 
181
  // translation unit 2:
182
  struct X {
183
- X(int);
184
  X(int, int);
 
185
  };
186
- X::X(int = 0, int = 0) { }
187
- class D: public X { }; // X(int, int) called by D();
188
- // D()'s implicit definition
189
- // violates the ODR
 
 
190
  ```
191
 
 
 
192
  If `D` is a template and is defined in more than one translation unit,
193
  then the preceding requirements shall apply both to names from the
194
  template’s enclosing scope used in the template definition (
195
  [[temp.nondep]]), and also to dependent names at the point of
196
  instantiation ([[temp.dep]]). If the definitions of `D` satisfy all
 
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, or template.
5
 
6
  An expression is *potentially evaluated* unless it is an unevaluated
7
  operand (Clause  [[expr]]) or a subexpression thereof. The set of
8
  *potential results* of an expression `e` is defined as follows:
9
 
10
+ - If `e` is an *id-expression* ([[expr.prim.id]]), the set contains
11
+ only `e`.
12
+ - If `e` is a subscripting operation ([[expr.sub]]) with an array
13
+ operand, the set contains the potential results of that operand.
14
  - If `e` is a class member access expression ([[expr.ref]]), the set
15
  contains the potential results of the object expression.
16
  - If `e` is a pointer-to-member expression ([[expr.mptr.oper]]) whose
17
  second operand is a constant expression, the set contains the
18
  potential results of the object expression.
 
23
  operands.
24
  - If `e` is a comma expression ([[expr.comma]]), the set contains the
25
  potential results of the right operand.
26
  - Otherwise, the set is empty.
27
 
28
+ [*Note 1*:
29
+
30
  This set is a (possibly-empty) set of *id-expression*s, each of which is
31
+ either `e` or a subexpression of `e`.
32
+
33
+ [*Example 1*:
34
+
35
+ In the following example, the set of potential results of the
36
+ initializer of `n` contains the first `S::x` subexpression, but not the
37
+ second `S::x` subexpression.
38
 
39
  ``` cpp
40
  struct S { static const int x = 0; };
41
  const int &f(const int &r);
42
  int n = b ? (1, S::x) // S::x is not odr-used here
43
+ : f(S::x); // S::x is odr-used here, so a definition is required
 
44
  ```
45
 
46
+ — *end example*]
47
+
48
+ — *end note*]
49
+
50
  A variable `x` whose name appears as a potentially-evaluated expression
51
  `ex` is *odr-used* by `ex` unless applying the lvalue-to-rvalue
52
  conversion ([[conv.lval]]) to `x` yields a constant expression (
53
  [[expr.const]]) that does not invoke any non-trivial functions and, if
54
  `x` is an object, `ex` is an element of the set of potential results of
 
60
  [[class.mfct.non-static]])). A virtual member function is odr-used if it
61
  is not pure. A function whose name appears as a potentially-evaluated
62
  expression is odr-used if it is the unique lookup result or the selected
63
  member of a set of overloaded functions ([[basic.lookup]],
64
  [[over.match]], [[over.over]]), unless it is a pure virtual function and
65
+ either its name is not explicitly qualified or the expression forms a
66
+ pointer to member ([[expr.unary.op]]).
67
+
68
+ [*Note 2*: This covers calls to named functions ([[expr.call]]),
69
+ operator overloading (Clause  [[over]]), user-defined conversions (
70
+ [[class.conv.fct]]), allocation functions for placement
71
+ *new-expression*s ([[expr.new]]), as well as non-default
72
+ initialization ([[dcl.init]]). A constructor selected to copy or move
73
+ an object of class type is odr-used even if the call is actually elided
74
+ by the implementation ([[class.copy]]). *end note*]
75
+
76
+ An allocation or deallocation function for a class is odr-used by a
77
+ *new-expression* appearing in a potentially-evaluated expression as
78
+ specified in  [[expr.new]] and  [[class.free]]. A deallocation function
79
+ for a class is odr-used by a delete expression appearing in a
80
+ potentially-evaluated expression as specified in  [[expr.delete]] and 
81
+ [[class.free]]. A non-placement allocation or deallocation function for
82
+ a class is odr-used by the definition of a constructor of that class. A
83
+ non-placement deallocation function for a class is odr-used by the
84
+ definition of the destructor of that class, or by being selected by the
85
+ lookup at the point of definition of a virtual destructor (
86
+ [[class.dtor]]).[^2] An assignment operator function in a class is
87
+ odr-used by an implicitly-defined copy-assignment or move-assignment
88
+ function for another class as specified in  [[class.copy]]. A
89
+ constructor for a class is odr-used as specified in  [[dcl.init]]. A
90
+ destructor for a class is odr-used if it is potentially invoked (
91
+ [[class.dtor]]).
92
 
93
  Every program shall contain exactly one definition of every non-inline
94
+ function or variable that is odr-used in that program outside of a
95
+ discarded statement ([[stmt.if]]); no diagnostic required. The
96
+ definition can appear explicitly in the program, it can be found in the
97
+ standard or a user-defined library, or (when appropriate) it is
98
+ implicitly defined (see  [[class.ctor]], [[class.dtor]] and
99
+ [[class.copy]]). An inline function or variable shall be defined in
100
+ every translation unit in which it is odr-used outside of a discarded
101
+ statement.
102
 
103
  Exactly one definition of a class is required in a translation unit if
104
  the class is used in a way that requires the class type to be complete.
105
+
106
+ [*Example 2*:
107
+
108
+ The following complete translation unit is well-formed, even though it
109
  never defines `X`:
110
 
111
  ``` cpp
112
  struct X; // declare X as a struct type
113
  struct X* x1; // use X in pointer formation
114
  X* x2; // use X in pointer formation
115
  ```
116
 
117
+ — *end example*]
118
+
119
+ [*Note 3*:
120
+
121
  The rules for declarations and expressions describe in which contexts
122
  complete class types are required. A class type `T` must be complete if:
123
 
124
  - an object of type `T` is defined ([[basic.def]]), or
125
  - a non-static class data member of type `T` is declared (
126
  [[class.mem]]), or
127
+ - `T` is used as the allocated type or array element type in a
128
  *new-expression* ([[expr.new]]), or
129
  - an lvalue-to-rvalue conversion is applied to a glvalue referring to an
130
  object of type `T` ([[conv.lval]]), or
131
  - an expression is converted (either implicitly or explicitly) to type
132
  `T` (Clause  [[conv]], [[expr.type.conv]], [[expr.dynamic.cast]],
133
  [[expr.static.cast]], [[expr.cast]]), or
134
  - an expression that is not a null pointer constant, and has type other
135
+ than cv `void*`, is converted to the type pointer to `T` or reference
136
+ to `T` using a standard conversion (Clause  [[conv]]), a
137
  `dynamic_cast` ([[expr.dynamic.cast]]) or a `static_cast` (
138
  [[expr.static.cast]]), or
139
  - a class member access operator is applied to an expression of type
140
  `T` ([[expr.ref]]), or
141
  - the `typeid` operator ([[expr.typeid]]) or the `sizeof` operator (
 
148
  - the type `T` is the subject of an `alignof` expression (
149
  [[expr.alignof]]), or
150
  - an *exception-declaration* has type `T`, reference to `T`, or pointer
151
  to `T` ([[except.handle]]).
152
 
153
+ — *end note*]
154
+
155
  There can be more than one definition of a class type (Clause 
156
  [[class]]), enumeration type ([[dcl.enum]]), inline function with
157
+ external linkage ([[dcl.inline]]), inline variable with external
158
+ linkage ([[dcl.inline]]), class template (Clause  [[temp]]), non-static
159
+ function template ([[temp.fct]]), static data member of a class
160
+ template ([[temp.static]]), member function of a class template (
161
  [[temp.mem.func]]), or template specialization for which some template
162
  parameters are not specified ([[temp.spec]], [[temp.class.spec]]) in a
163
  program provided that each definition appears in a different translation
164
  unit, and provided the definitions satisfy the following requirements.
165
  Given such an entity named `D` defined in more than one translation
 
169
  and
170
  - in each definition of `D`, corresponding names, looked up according
171
  to  [[basic.lookup]], shall refer to an entity defined within the
172
  definition of `D`, or shall refer to the same entity, after overload
173
  resolution ([[over.match]]) and after matching of partial template
174
+ specialization ([[temp.over]]), except that a name can refer to
175
+ - a non-volatile `const` object with internal or no linkage if the
176
+ object
177
+ - has the same literal type in all definitions of `D`,
178
+ - is initialized with a constant expression ([[expr.const]]),
179
+ - is not odr-used in any definition of `D`, and
180
+ - has the same value in all definitions of `D`,
181
+
182
+ or
183
+ - a reference with internal or no linkage initialized with a constant
184
+ expression such that the reference refers to the same entity in all
185
+ definitions of `D`;
186
+
187
+ and
188
  - in each definition of `D`, corresponding entities shall have the same
189
  language linkage; and
190
  - in each definition of `D`, the overloaded operators referred to, the
191
  implicit calls to conversion functions, constructors, operator new
192
  functions and operator delete functions, shall refer to the same
193
  function, or to a function defined within the definition of `D`; and
194
  - in each definition of `D`, a default argument used by an (implicit or
195
  explicit) function call is treated as if its token sequence were
196
  present in the definition of `D`; that is, the default argument is
197
+ subject to the requirements described in this paragraph (and, if the
198
+ default argument has subexpressions with default arguments, this
199
+ requirement applies recursively).[^3]
200
  - if `D` is a class with an implicitly-declared constructor (
201
  [[class.ctor]]), it is as if the constructor was implicitly defined in
202
  every translation unit where it is odr-used, and the implicit
203
  definition in every translation unit shall call the same constructor
204
+ for a subobject of `D`.
205
+ \[*Example 3*:
206
  ``` cpp
207
  // translation unit 1:
208
  struct X {
 
209
  X(int, int);
210
+ X(int, int, int);
211
  };
212
+ X::X(int, int = 0) { }
213
+ class D {
214
+ X x = 0;
215
+ };
216
+ D d1; // X(int, int) called by D()
217
 
218
  // translation unit 2:
219
  struct X {
 
220
  X(int, int);
221
+ X(int, int, int);
222
  };
223
+ X::X(int, int = 0, int = 0) { }
224
+ class D {
225
+ X x = 0;
226
+ };
227
+ D d2; // X(int, int, int) called by D();
228
+ // D()'s implicit definition violates the ODR
229
  ```
230
 
231
+ — *end example*]
232
+
233
  If `D` is a template and is defined in more than one translation unit,
234
  then the preceding requirements shall apply both to names from the
235
  template’s enclosing scope used in the template definition (
236
  [[temp.nondep]]), and also to dependent names at the point of
237
  instantiation ([[temp.dep]]). If the definitions of `D` satisfy all