From Jason Turner

[class.ctor]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpml7618ov/{from.md → to.md} +81 -44
tmp/tmpml7618ov/{from.md → to.md} RENAMED
@@ -1,12 +1,12 @@
1
  ## Constructors <a id="class.ctor">[[class.ctor]]</a>
2
 
3
- Constructors do not have names. A declaration of a constructor uses a
4
- function declarator ([[dcl.fct]]) of the form
5
 
6
  ``` bnf
7
- ptr-declarator '(' parameter-declaration-clause ')' exception-specificationₒₚₜ 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:
@@ -26,66 +26,78 @@ parentheses, and the *id-expression* has one of the following forms:
26
 
27
  The *class-name* shall not be a *typedef-name*. In a constructor
28
  declaration, each *decl-specifier* in the optional *decl-specifier-seq*
29
  shall be `friend`, `inline`, `explicit`, or `constexpr`.
30
 
 
 
31
  ``` cpp
32
  struct S {
33
  S(); // declares the constructor
34
  };
35
 
36
  S::S() { } // defines the constructor
37
  ```
38
 
 
 
39
  A constructor is used to initialize objects of its class type. Because
40
  constructors do not have names, they are never found during name lookup;
41
  however an explicit type conversion using the functional notation (
42
  [[expr.type.conv]]) will cause a constructor to be called to initialize
43
- an object. For initialization of objects of class type see 
44
- [[class.init]].
 
 
45
 
46
  A constructor can be invoked for a `const`, `volatile` or `const`
47
  `volatile` object. `const` and `volatile` semantics ([[dcl.type.cv]])
48
  are not applied on an object under construction. They come into effect
49
  when the constructor for the most derived object ([[intro.object]])
50
  ends.
51
 
52
  A *default* constructor for a class `X` is a constructor of class `X`
53
- that can be called without an argument. If there is no user-declared
54
- constructor for class `X`, a constructor having no parameters is
55
- implicitly declared as defaulted ([[dcl.fct.def]]). An
56
- implicitly-declared default constructor is an `inline` `public` member
57
- of its class. A defaulted default constructor for class `X` is defined
58
- as deleted if:
59
 
60
- - `X` is a union-like class that has a variant member with a non-trivial
61
- default constructor,
62
- - any non-static data member with no *brace-or-equal-initializer* is of
63
- reference type,
 
 
 
 
 
 
64
  - any non-variant non-static data member of const-qualified type (or
65
  array thereof) with no *brace-or-equal-initializer* does not have a
66
  user-provided default constructor,
67
  - `X` is a union and all of its variant members are of const-qualified
68
  type (or array thereof),
69
  - `X` is a non-union class and all members of any anonymous union member
70
  are of const-qualified type (or array thereof),
71
  - any potentially constructed subobject, except for a non-static data
72
  member with a *brace-or-equal-initializer*, has class type `M` (or
73
  array thereof) and either `M` has no default constructor or overload
74
- resolution ([[over.match]]) as applied to `M`’s default constructor
75
- results in an ambiguity or in a function that is deleted or
76
- inaccessible from the defaulted default constructor, or
77
  - any potentially constructed subobject has a type with a destructor
78
  that is deleted or inaccessible from the defaulted default
79
  constructor.
80
 
81
- A default constructor is trivial if it is not user-provided and if:
82
 
83
  - its class has no virtual functions ([[class.virtual]]) and no virtual
84
  base classes ([[class.mi]]), and
85
- - no non-static data member of its class has a
86
- *brace-or-equal-initializer*, and
87
  - all the direct base classes of its class have trivial default
88
  constructors, and
89
  - for all the non-static data members of its class that are of class
90
  type (or array thereof), each such class has a trivial default
91
  constructor.
@@ -99,19 +111,21 @@ defaulted after its first declaration. The implicitly-defined default
99
  constructor performs the set of initializations of the class that would
100
  be performed by a user-written default constructor for that class with
101
  no *ctor-initializer* ([[class.base.init]]) and an empty
102
  *compound-statement*. If that user-written default constructor would be
103
  ill-formed, the program is ill-formed. If that user-written default
104
- constructor would satisfy the requirements of a `constexpr`
105
- constructor ([[dcl.constexpr]]), the implicitly-defined default
106
- constructor is `constexpr`. Before the defaulted default constructor for
107
- a class is implicitly defined, all the non-user-provided default
108
- constructors for its base classes and its non-static data members shall
109
- have been implicitly defined. An implicitly-declared default constructor
110
- has an *exception-specification* ([[except.spec]]). An
111
- explicitly-defaulted definition might have an implicit
112
- *exception-specification,* see  [[dcl.fct.def]].
 
 
113
 
114
  Default constructors are called implicitly to create class objects of
115
  static, thread, or automatic storage duration ([[basic.stc.static]],
116
  [[basic.stc.thread]], [[basic.stc.auto]]) defined without an
117
  initializer ([[dcl.init]]), are called to create class objects of
@@ -120,37 +134,51 @@ dynamic storage duration ([[basic.stc.dynamic]]) created by a
120
  [[expr.new]]), or are called when the explicit type conversion syntax (
121
  [[expr.type.conv]]) is used. A program is ill-formed if the default
122
  constructor for an object is implicitly used and the constructor is not
123
  accessible (Clause  [[class.access]]).
124
 
125
- [[class.base.init]] describes the order in which constructors for base
126
- classes and non-static data members are called and describes how
127
- arguments can be specified for the calls to these constructors.
 
128
 
129
  A `return` statement in the body of a constructor shall not specify a
130
  return value. The address of a constructor shall not be taken.
131
 
132
  A functional notation type conversion ([[expr.type.conv]]) can be used
133
- to create new objects of its type. The syntax looks like an explicit
134
- call of the constructor.
 
 
 
 
135
 
136
  ``` cpp
137
  complex zz = complex(1,2.3);
138
  cprint( complex(7.8,1.2) );
139
  ```
140
 
141
- An object created in this way is unnamed. [[class.temporary]] describes
142
- the lifetime of temporary objects. Explicit constructor calls do not
143
- yield lvalues, see  [[basic.lval]].
144
 
145
- some language constructs have special semantics when used during
146
- construction; see  [[class.base.init]] and  [[class.cdtor]].
147
 
148
- During the construction of a `const` object, if the value of the object
149
- or any of its subobjects is accessed through a glvalue that is not
150
- obtained, directly or indirectly, from the constructor’s `this` pointer,
151
- the value of the object or subobject thus obtained is unspecified.
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  ``` cpp
154
  struct C;
155
  void no_opt(C*);
156
 
@@ -165,7 +193,16 @@ void no_opt(C* cptr) {
165
  int i = cobj.c * 100; // value of cobj.c is unspecified
166
  cptr->c = 1;
167
  cout << cobj.c * 100 // value of cobj.c is unspecified
168
  << '\n';
169
  }
 
 
 
 
 
 
 
170
  ```
171
 
 
 
 
1
  ## Constructors <a id="class.ctor">[[class.ctor]]</a>
2
 
3
+ Constructors do not have names. In a declaration of a constructor, the
4
+ *declarator* is a 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:
 
26
 
27
  The *class-name* shall not be a *typedef-name*. In a constructor
28
  declaration, each *decl-specifier* in the optional *decl-specifier-seq*
29
  shall be `friend`, `inline`, `explicit`, or `constexpr`.
30
 
31
+ [*Example 1*:
32
+
33
  ``` cpp
34
  struct S {
35
  S(); // declares the constructor
36
  };
37
 
38
  S::S() { } // defines the constructor
39
  ```
40
 
41
+ — *end example*]
42
+
43
  A constructor is used to initialize objects of its class type. Because
44
  constructors do not have names, they are never found during name lookup;
45
  however an explicit type conversion using the functional notation (
46
  [[expr.type.conv]]) will cause a constructor to be called to initialize
47
+ an object.
48
+
49
+ [*Note 1*: For initialization of objects of class type see 
50
+ [[class.init]]. — *end note*]
51
 
52
  A constructor can be invoked for a `const`, `volatile` or `const`
53
  `volatile` object. `const` and `volatile` semantics ([[dcl.type.cv]])
54
  are not applied on an object under construction. They come into effect
55
  when the constructor for the most derived object ([[intro.object]])
56
  ends.
57
 
58
  A *default* constructor for a class `X` is a constructor of class `X`
59
+ for which each parameter that is not a function parameter pack has a
60
+ default argument (including the case of a constructor with no
61
+ parameters). If there is no user-declared constructor for class `X`, a
62
+ non-explicit constructor having no parameters is implicitly declared as
63
+ defaulted ([[dcl.fct.def]]). An implicitly-declared default constructor
64
+ is an `inline` `public` member of its class.
65
 
66
+ A defaulted default constructor for class `X` is defined as deleted if:
67
+
68
+ - `X` is a union that has a variant member with a non-trivial default
69
+ constructor and no variant member of `X` has a default member
70
+ initializer,
71
+ - `X` is a non-union class that has a variant member `M` with a
72
+ non-trivial default constructor and no variant member of the anonymous
73
+ union containing `M` has a default member initializer,
74
+ - any non-static data member with no default member initializer (
75
+ [[class.mem]]) is of reference type,
76
  - any non-variant non-static data member of const-qualified type (or
77
  array thereof) with no *brace-or-equal-initializer* does not have a
78
  user-provided default constructor,
79
  - `X` is a union and all of its variant members are of const-qualified
80
  type (or array thereof),
81
  - `X` is a non-union class and all members of any anonymous union member
82
  are of const-qualified type (or array thereof),
83
  - any potentially constructed subobject, except for a non-static data
84
  member with a *brace-or-equal-initializer*, has class type `M` (or
85
  array thereof) and either `M` has no default constructor or overload
86
+ resolution ([[over.match]]) as applied to find `M`’s corresponding
87
+ constructor results in an ambiguity or in a function that is deleted
88
+ or inaccessible from the defaulted default constructor, or
89
  - any potentially constructed subobject has a type with a destructor
90
  that is deleted or inaccessible from the defaulted default
91
  constructor.
92
 
93
+ A default constructor is *trivial* if it is not user-provided and if:
94
 
95
  - its class has no virtual functions ([[class.virtual]]) and no virtual
96
  base classes ([[class.mi]]), and
97
+ - no non-static data member of its class has a default member
98
+ initializer ([[class.mem]]), and
99
  - all the direct base classes of its class have trivial default
100
  constructors, and
101
  - for all the non-static data members of its class that are of class
102
  type (or array thereof), each such class has a trivial default
103
  constructor.
 
111
  constructor performs the set of initializations of the class that would
112
  be performed by a user-written default constructor for that class with
113
  no *ctor-initializer* ([[class.base.init]]) and an empty
114
  *compound-statement*. If that user-written default constructor would be
115
  ill-formed, the program is ill-formed. If that user-written default
116
+ constructor would satisfy the requirements of a constexpr constructor (
117
+ [[dcl.constexpr]]), the implicitly-defined default constructor is
118
+ `constexpr`. Before the defaulted default constructor for a class is
119
+ implicitly defined, all the non-user-provided default constructors for
120
+ its base classes and its non-static data members shall have been
121
+ implicitly defined.
122
+
123
+ [*Note 2*: An implicitly-declared default constructor has an exception
124
+ specification ([[except.spec]]). An explicitly-defaulted definition
125
+ might have an implicit exception specification, see 
126
+ [[dcl.fct.def]]. — *end note*]
127
 
128
  Default constructors are called implicitly to create class objects of
129
  static, thread, or automatic storage duration ([[basic.stc.static]],
130
  [[basic.stc.thread]], [[basic.stc.auto]]) defined without an
131
  initializer ([[dcl.init]]), are called to create class objects of
 
134
  [[expr.new]]), or are called when the explicit type conversion syntax (
135
  [[expr.type.conv]]) is used. A program is ill-formed if the default
136
  constructor for an object is implicitly used and the constructor is not
137
  accessible (Clause  [[class.access]]).
138
 
139
+ [*Note 3*: [[class.base.init]] describes the order in which
140
+ constructors for base classes and non-static data members are called and
141
+ describes how arguments can be specified for the calls to these
142
+ constructors. — *end note*]
143
 
144
  A `return` statement in the body of a constructor shall not specify a
145
  return value. The address of a constructor shall not be taken.
146
 
147
  A functional notation type conversion ([[expr.type.conv]]) can be used
148
+ to create new objects of its type.
149
+
150
+ [*Note 4*: The syntax looks like an explicit call of the
151
+ constructor. — *end note*]
152
+
153
+ [*Example 2*:
154
 
155
  ``` cpp
156
  complex zz = complex(1,2.3);
157
  cprint( complex(7.8,1.2) );
158
  ```
159
 
160
+ *end example*]
 
 
161
 
162
+ An object created in this way is unnamed.
 
163
 
164
+ [*Note 5*: [[class.temporary]] describes the lifetime of temporary
165
+ objects. *end note*]
166
+
167
+ [*Note 6*: Explicit constructor calls do not yield lvalues, see 
168
+ [[basic.lval]]. — *end note*]
169
+
170
+ [*Note 7*: Some language constructs have special semantics when used
171
+ during construction; see  [[class.base.init]] and 
172
+ [[class.cdtor]]. — *end note*]
173
+
174
+ During the construction of an object, if the value of the object or any
175
+ of its subobjects is accessed through a glvalue that is not obtained,
176
+ directly or indirectly, from the constructor’s `this` pointer, the value
177
+ of the object or subobject thus obtained is unspecified.
178
+
179
+ [*Example 3*:
180
 
181
  ``` cpp
182
  struct C;
183
  void no_opt(C*);
184
 
 
193
  int i = cobj.c * 100; // value of cobj.c is unspecified
194
  cptr->c = 1;
195
  cout << cobj.c * 100 // value of cobj.c is unspecified
196
  << '\n';
197
  }
198
+
199
+ extern struct D d;
200
+ struct D {
201
+ D(int a) : a(a), b(d.a) {}
202
+ int a, b;
203
+ };
204
+ D d = D(1); // value of d.b is unspecified
205
  ```
206
 
207
+ — *end example*]
208
+