From Jason Turner

[class.ctor]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpzuvi5shr/{from.md → to.md} +60 -67
tmp/tmpzuvi5shr/{from.md → to.md} RENAMED
@@ -1,29 +1,32 @@
1
  ### Constructors <a id="class.ctor">[[class.ctor]]</a>
2
 
3
- A *constructor* is introduced by a declaration whose *declarator* is a
4
- function declarator [[dcl.fct]] of the form
 
 
5
 
6
  ``` bnf
7
  ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
8
  ```
9
 
10
  where the *ptr-declarator* consists solely of an *id-expression*, an
11
  optional *attribute-specifier-seq*, and optional surrounding
12
  parentheses, and the *id-expression* has one of the following forms:
13
 
14
- - in a *member-declaration* that belongs to the *member-specification*
15
- of a class or class template but is not a friend declaration
16
- [[class.friend]], the *id-expression* is the injected-class-name
17
- [[class.pre]] of the immediately-enclosing entity or
18
- - in a declaration at namespace scope or in a friend declaration, the
19
- *id-expression* is a *qualified-id* that names a constructor
20
- [[class.qual]].
 
21
 
22
  Constructors do not have names. In a constructor declaration, each
23
  *decl-specifier* in the optional *decl-specifier-seq* shall be `friend`,
24
- `inline`, `constexpr`, or an *explicit-specifier*.
25
 
26
  [*Example 1*:
27
 
28
  ``` cpp
29
  struct S {
@@ -33,18 +36,17 @@ struct S {
33
  S::S() { } // defines the constructor
34
  ```
35
 
36
  — *end example*]
37
 
38
- A constructor is used to initialize objects of its class type. Because
39
- constructors do not have names, they are never found during name lookup;
40
- however an explicit type conversion using the functional notation
41
- [[expr.type.conv]] will cause a constructor to be called to initialize
42
- an object.
43
 
44
- [*Note 1*: The syntax looks like an explicit call of the
45
- constructor. *end note*]
 
 
 
46
 
47
  [*Example 2*:
48
 
49
  ``` cpp
50
  complex zz = complex(1,2.3);
@@ -71,15 +73,19 @@ during construction; see  [[class.base.init]] and 
71
  A constructor can be invoked for a `const`, `volatile` or `const`
72
  `volatile` object. `const` and `volatile` semantics [[dcl.type.cv]] are
73
  not applied on an object under construction. They come into effect when
74
  the constructor for the most derived object [[intro.object]] ends.
75
 
76
- A `return` statement in the body of a constructor shall not specify a
77
- return value. The address of a constructor shall not be taken.
 
 
78
 
79
  A constructor shall not be a coroutine.
80
 
 
 
81
  #### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
82
 
83
  A *default constructor* for a class `X` is a constructor of class `X`
84
  for which each parameter that is not a function parameter pack has a
85
  default argument (including the case of a constructor with no
@@ -127,42 +133,33 @@ A default constructor is *trivial* if it is not user-provided and if:
127
  type (or array thereof), each such class has a trivial default
128
  constructor.
129
 
130
  Otherwise, the default constructor is *non-trivial*.
131
 
132
- A default constructor that is defaulted and not defined as deleted is
133
- *implicitly defined* when it is odr-used [[basic.def.odr]] to create an
134
- object of its class type [[intro.object]], when it is needed for
135
- constant evaluation [[expr.const]], or when it is explicitly defaulted
136
- after its first declaration. The implicitly-defined default constructor
137
  performs the set of initializations of the class that would be performed
138
  by a user-written default constructor for that class with no
139
  *ctor-initializer* [[class.base.init]] and an empty
140
  *compound-statement*. If that user-written default constructor would be
141
  ill-formed, the program is ill-formed. If that user-written default
142
- constructor would satisfy the requirements of a constexpr constructor
143
- [[dcl.constexpr]], the implicitly-defined default constructor is
144
- `constexpr`. Before the defaulted default constructor for a class is
145
- implicitly defined, all the non-user-provided default constructors for
146
- its base classes and its non-static data members are implicitly defined.
147
 
148
  [*Note 1*: An implicitly-declared default constructor has an exception
149
  specification [[except.spec]]. An explicitly-defaulted definition might
150
  have an implicit exception specification, see 
151
  [[dcl.fct.def]]. — *end note*]
152
 
153
- Default constructors are called implicitly to create class objects of
154
- static, thread, or automatic storage duration ([[basic.stc.static]],
155
- [[basic.stc.thread]], [[basic.stc.auto]]) defined without an initializer
156
- [[dcl.init]], are called to create class objects of dynamic storage
157
- duration [[basic.stc.dynamic]] created by a *new-expression* in which
158
- the *new-initializer* is omitted [[expr.new]], or are called when the
159
- explicit type conversion syntax [[expr.type.conv]] is used. A program is
160
- ill-formed if the default constructor for an object is implicitly used
161
- and the constructor is not accessible [[class.access]].
162
 
163
- [*Note 2*: [[class.base.init]] describes the order in which
164
  constructors for base classes and non-static data members are called and
165
  describes how arguments can be specified for the calls to these
166
  constructors. — *end note*]
167
 
168
  #### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
@@ -211,11 +208,11 @@ Y e = d; // calls Y(const Y&)
211
 
212
  — *end example*]
213
 
214
  [*Note 1*:
215
 
216
- All forms of copy/move constructor may be declared for a class.
217
 
218
  [*Example 3*:
219
 
220
  ``` cpp
221
  struct X {
@@ -277,25 +274,26 @@ void h() {
277
 
278
  If the class definition does not explicitly declare a copy constructor,
279
  a non-explicit one is declared *implicitly*. If the class definition
280
  declares a move constructor or move assignment operator, the implicitly
281
  declared copy constructor is defined as deleted; otherwise, it is
282
- defined as defaulted [[dcl.fct.def]]. The latter case is deprecated if
283
- the class has a user-declared copy assignment operator or a
284
- user-declared destructor [[depr.impldec]].
285
 
286
  The implicitly-declared copy constructor for a class `X` will have the
287
  form
288
 
289
  ``` cpp
290
  X::X(const X&)
291
  ```
292
 
293
  if each potentially constructed subobject of a class type `M` (or array
294
  thereof) has a copy constructor whose first parameter is of type `const`
295
- `M&` or `const` `volatile` `M&`.[^3] Otherwise, the implicitly-declared
296
- copy constructor will have the form
 
297
 
298
  ``` cpp
299
  X::X(X&)
300
  ```
301
 
@@ -308,11 +306,11 @@ if and only if
308
  - `X` does not have a user-declared move assignment operator, and
309
  - `X` does not have a user-declared destructor.
310
 
311
  [*Note 3*: When the move constructor is not implicitly declared or
312
  explicitly supplied, expressions that otherwise would have invoked the
313
- move constructor may instead invoke a copy constructor. — *end note*]
314
 
315
  The implicitly-declared move constructor for class `X` will have the
316
  form
317
 
318
  ``` cpp
@@ -321,24 +319,24 @@ X::X(X&&)
321
 
322
  An implicitly-declared copy/move constructor is an inline public member
323
  of its class. A defaulted copy/move constructor for a class `X` is
324
  defined as deleted [[dcl.fct.def.delete]] if `X` has:
325
 
326
- - a potentially constructed subobject type `M` (or array thereof) that
327
- cannot be copied/moved because overload resolution [[over.match]], as
328
- applied to find `M`’s corresponding constructor, results in an
329
- ambiguity or a function that is deleted or inaccessible from the
330
- defaulted constructor,
331
  - a variant member whose corresponding constructor as selected by
332
  overload resolution is non-trivial,
333
  - any potentially constructed subobject of a type with a destructor that
334
  is deleted or inaccessible from the defaulted constructor, or,
335
  - for the copy constructor, a non-static data member of rvalue reference
336
  type.
337
 
338
  [*Note 4*: A defaulted move constructor that is defined as deleted is
339
- ignored by overload resolution ([[over.match]], [[over.over]]). Such a
340
  constructor would otherwise interfere with initialization from an rvalue
341
  which can use the copy constructor instead. — *end note*]
342
 
343
  A copy/move constructor for class `X` is trivial if it is not
344
  user-provided and if:
@@ -351,22 +349,17 @@ user-provided and if:
351
  thereof), the constructor selected to copy/move that member is
352
  trivial;
353
 
354
  otherwise the copy/move constructor is *non-trivial*.
355
 
356
- A copy/move constructor that is defaulted and not defined as deleted is
357
- *implicitly defined* when it is odr-used [[basic.def.odr]], when it is
358
- needed for constant evaluation [[expr.const]], or when it is explicitly
359
- defaulted after its first declaration.
360
-
361
  [*Note 5*: The copy/move constructor is implicitly defined even if the
362
- implementation elided its odr-use ([[basic.def.odr]],
363
- [[class.temporary]]). — *end note*]
364
 
365
- If the implicitly-defined constructor would satisfy the requirements of
366
- a constexpr constructor [[dcl.constexpr]], the implicitly-defined
367
- constructor is `constexpr`.
368
 
369
  Before the defaulted copy/move constructor for a class is implicitly
370
  defined, all non-user-provided copy/move constructors for its
371
  potentially constructed subobjects are implicitly defined.
372
 
@@ -395,11 +388,11 @@ to its type:
395
 
396
  Virtual base class subobjects shall be initialized only once by the
397
  implicitly-defined copy/move constructor (see  [[class.base.init]]).
398
 
399
  The implicitly-defined copy/move constructor for a union `X` copies the
400
- object representation [[basic.types]] of `X`. For each object nested
401
- within [[intro.object]] the object that is the source of the copy, a
402
- corresponding object o nested within the destination is identified (if
403
- the object is a subobject) or created (otherwise), and the lifetime of o
404
- begins before the copy is performed.
405
 
 
1
  ### Constructors <a id="class.ctor">[[class.ctor]]</a>
2
 
3
+ #### General <a id="class.ctor.general">[[class.ctor.general]]</a>
4
+
5
+ A *declarator* declares a *constructor* if it is a function declarator
6
+ [[dcl.fct]] of the form
7
 
8
  ``` bnf
9
  ptr-declarator '(' parameter-declaration-clause ')' noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
10
  ```
11
 
12
  where the *ptr-declarator* consists solely of an *id-expression*, an
13
  optional *attribute-specifier-seq*, and optional surrounding
14
  parentheses, and the *id-expression* has one of the following forms:
15
 
16
+ - in a friend declaration [[class.friend]], the *id-expression* is a
17
+ *qualified-id* that names a constructor [[class.qual]];
18
+ - otherwise, in a *member-declaration* that belongs to the
19
+ *member-specification* of a class or class template, the
20
+ *id-expression* is the injected-class-name [[class.pre]] of the
21
+ immediately-enclosing entity;
22
+ - otherwise, the *id-expression* is a *qualified-id* whose
23
+ *unqualified-id* is the injected-class-name of its lookup context.
24
 
25
  Constructors do not have names. In a constructor declaration, each
26
  *decl-specifier* in the optional *decl-specifier-seq* shall be `friend`,
27
+ `inline`, `constexpr`, `consteval`, or an *explicit-specifier*.
28
 
29
  [*Example 1*:
30
 
31
  ``` cpp
32
  struct S {
 
36
  S::S() { } // defines the constructor
37
  ```
38
 
39
  — *end example*]
40
 
41
+ A constructor is used to initialize objects of its class type.
 
 
 
 
42
 
43
+ [*Note 1*: Because constructors do not have names, they are never found
44
+ during unqualified name lookup; however an explicit type conversion
45
+ using the functional notation [[expr.type.conv]] will cause a
46
+ constructor to be called to initialize an object. The syntax looks like
47
+ an explicit call of the constructor. — *end note*]
48
 
49
  [*Example 2*:
50
 
51
  ``` cpp
52
  complex zz = complex(1,2.3);
 
73
  A constructor can be invoked for a `const`, `volatile` or `const`
74
  `volatile` object. `const` and `volatile` semantics [[dcl.type.cv]] are
75
  not applied on an object under construction. They come into effect when
76
  the constructor for the most derived object [[intro.object]] ends.
77
 
78
+ The address of a constructor shall not be taken.
79
+
80
+ [*Note 6*: A `return` statement in the body of a constructor cannot
81
+ specify a return value [[stmt.return]]. — *end note*]
82
 
83
  A constructor shall not be a coroutine.
84
 
85
+ A constructor shall not have an explicit object parameter [[dcl.fct]].
86
+
87
  #### Default constructors <a id="class.default.ctor">[[class.default.ctor]]</a>
88
 
89
  A *default constructor* for a class `X` is a constructor of class `X`
90
  for which each parameter that is not a function parameter pack has a
91
  default argument (including the case of a constructor with no
 
133
  type (or array thereof), each such class has a trivial default
134
  constructor.
135
 
136
  Otherwise, the default constructor is *non-trivial*.
137
 
138
+ An implicitly-defined [[dcl.fct.def.default]] default constructor
 
 
 
 
139
  performs the set of initializations of the class that would be performed
140
  by a user-written default constructor for that class with no
141
  *ctor-initializer* [[class.base.init]] and an empty
142
  *compound-statement*. If that user-written default constructor would be
143
  ill-formed, the program is ill-formed. If that user-written default
144
+ constructor would be constexpr-suitable [[dcl.constexpr]], the
145
+ implicitly-defined default constructor is `constexpr`. Before the
146
+ defaulted default constructor for a class is implicitly defined, all the
147
+ non-user-provided default constructors for its base classes and its
148
+ non-static data members are implicitly defined.
149
 
150
  [*Note 1*: An implicitly-declared default constructor has an exception
151
  specification [[except.spec]]. An explicitly-defaulted definition might
152
  have an implicit exception specification, see 
153
  [[dcl.fct.def]]. — *end note*]
154
 
155
+ [*Note 2*: A default constructor is implicitly invoked to initialize a
156
+ class object when no initializer is specified [[dcl.init.general]]. Such
157
+ a default constructor is required to be accessible
158
+ [[class.access]]. *end note*]
 
 
 
 
 
159
 
160
+ [*Note 3*: [[class.base.init]] describes the order in which
161
  constructors for base classes and non-static data members are called and
162
  describes how arguments can be specified for the calls to these
163
  constructors. — *end note*]
164
 
165
  #### Copy/move constructors <a id="class.copy.ctor">[[class.copy.ctor]]</a>
 
208
 
209
  — *end example*]
210
 
211
  [*Note 1*:
212
 
213
+ All forms of copy/move constructor can be declared for a class.
214
 
215
  [*Example 3*:
216
 
217
  ``` cpp
218
  struct X {
 
274
 
275
  If the class definition does not explicitly declare a copy constructor,
276
  a non-explicit one is declared *implicitly*. If the class definition
277
  declares a move constructor or move assignment operator, the implicitly
278
  declared copy constructor is defined as deleted; otherwise, it is
279
+ defaulted [[dcl.fct.def]]. The latter case is deprecated if the class
280
+ has a user-declared copy assignment operator or a user-declared
281
+ destructor [[depr.impldec]].
282
 
283
  The implicitly-declared copy constructor for a class `X` will have the
284
  form
285
 
286
  ``` cpp
287
  X::X(const X&)
288
  ```
289
 
290
  if each potentially constructed subobject of a class type `M` (or array
291
  thereof) has a copy constructor whose first parameter is of type `const`
292
+ `M&` or `const` `volatile` `M&`.[^3]
293
+
294
+ Otherwise, the implicitly-declared copy constructor will have the form
295
 
296
  ``` cpp
297
  X::X(X&)
298
  ```
299
 
 
306
  - `X` does not have a user-declared move assignment operator, and
307
  - `X` does not have a user-declared destructor.
308
 
309
  [*Note 3*: When the move constructor is not implicitly declared or
310
  explicitly supplied, expressions that otherwise would have invoked the
311
+ move constructor might instead invoke a copy constructor. — *end note*]
312
 
313
  The implicitly-declared move constructor for class `X` will have the
314
  form
315
 
316
  ``` cpp
 
319
 
320
  An implicitly-declared copy/move constructor is an inline public member
321
  of its class. A defaulted copy/move constructor for a class `X` is
322
  defined as deleted [[dcl.fct.def.delete]] if `X` has:
323
 
324
+ - a potentially constructed subobject of type `M` (or array thereof)
325
+ that cannot be copied/moved because overload resolution
326
+ [[over.match]], as applied to find `M`’s corresponding constructor,
327
+ results in an ambiguity or a function that is deleted or inaccessible
328
+ from the defaulted constructor,
329
  - a variant member whose corresponding constructor as selected by
330
  overload resolution is non-trivial,
331
  - any potentially constructed subobject of a type with a destructor that
332
  is deleted or inaccessible from the defaulted constructor, or,
333
  - for the copy constructor, a non-static data member of rvalue reference
334
  type.
335
 
336
  [*Note 4*: A defaulted move constructor that is defined as deleted is
337
+ ignored by overload resolution [[over.match]], [[over.over]]. Such a
338
  constructor would otherwise interfere with initialization from an rvalue
339
  which can use the copy constructor instead. — *end note*]
340
 
341
  A copy/move constructor for class `X` is trivial if it is not
342
  user-provided and if:
 
349
  thereof), the constructor selected to copy/move that member is
350
  trivial;
351
 
352
  otherwise the copy/move constructor is *non-trivial*.
353
 
 
 
 
 
 
354
  [*Note 5*: The copy/move constructor is implicitly defined even if the
355
+ implementation elided its odr-use
356
+ [[term.odr.use]], [[class.temporary]]. — *end note*]
357
 
358
+ If an implicitly-defined [[dcl.fct.def.default]] constructor would be
359
+ constexpr-suitable [[dcl.constexpr]], the implicitly-defined constructor
360
+ is `constexpr`.
361
 
362
  Before the defaulted copy/move constructor for a class is implicitly
363
  defined, all non-user-provided copy/move constructors for its
364
  potentially constructed subobjects are implicitly defined.
365
 
 
388
 
389
  Virtual base class subobjects shall be initialized only once by the
390
  implicitly-defined copy/move constructor (see  [[class.base.init]]).
391
 
392
  The implicitly-defined copy/move constructor for a union `X` copies the
393
+ object representation [[term.object.representation]] of `X`. For each
394
+ object nested within [[intro.object]] the object that is the source of
395
+ the copy, a corresponding object o nested within the destination is
396
+ identified (if the object is a subobject) or created (otherwise), and
397
+ the lifetime of o begins before the copy is performed.
398