From Jason Turner

[dcl.type]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpbod9_l3r/{from.md → to.md} +372 -304
tmp/tmpbod9_l3r/{from.md → to.md} RENAMED
@@ -29,14 +29,14 @@ defining-type-specifier-seq:
29
  defining-type-specifier defining-type-specifier-seq
30
  ```
31
 
32
  The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
33
  *defining-type-specifier-seq* appertains to the type denoted by the
34
- preceding *type-specifier*s or *defining-type-specifier*s (
35
- [[dcl.meaning]]). The *attribute-specifier-seq* affects the type only
36
- for the declaration it appears in, not other declarations involving the
37
- same type.
38
 
39
  As a general rule, at most one *defining-type-specifier* is allowed in
40
  the complete *decl-specifier-seq* of a *declaration* or in a
41
  *defining-type-specifier-seq*, and at most one *type-specifier* is
42
  allowed in a *type-specifier-seq*. The only exceptions to this rule are
@@ -51,16 +51,16 @@ the following:
51
  - `long` can be combined with `long`.
52
 
53
  Except in a declaration of a constructor, destructor, or conversion
54
  function, at least one *defining-type-specifier* that is not a
55
  *cv-qualifier* shall appear in a complete *type-specifier-seq* or a
56
- complete *decl-specifier-seq*.[^3]
57
 
58
  [*Note 1*: *enum-specifier*s, *class-specifier*s, and
59
- *typename-specifier*s are discussed in [[dcl.enum]], Clause  [[class]],
60
- and [[temp.res]], respectively. The remaining *type-specifier*s are
61
- discussed in the rest of this section. — *end note*]
62
 
63
  #### The *cv-qualifier*s <a id="dcl.type.cv">[[dcl.type.cv]]</a>
64
 
65
  There are two *cv-qualifier*s, `const` and `volatile`. Each
66
  *cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
@@ -74,48 +74,47 @@ cv-qualifiers affect object and function types. — *end note*]
74
  Redundant cv-qualifications are ignored.
75
 
76
  [*Note 2*: For example, these could be introduced by
77
  typedefs. — *end note*]
78
 
79
- [*Note 3*: Declaring a variable `const` can affect its linkage (
80
- [[dcl.stc]]) and its usability in constant expressions (
81
- [[expr.const]]). As described in  [[dcl.init]], the definition of an
82
- object or subobject of const-qualified type must specify an initializer
83
- or be subject to default-initialization. — *end note*]
84
 
85
  A pointer or reference to a cv-qualified type need not actually point or
86
  refer to a cv-qualified object, but it is treated as if it does; a
87
  const-qualified access path cannot be used to modify an object even if
88
  the object referenced is a non-const object and can be modified through
89
  some other access path.
90
 
91
  [*Note 4*: Cv-qualifiers are supported by the type system so that they
92
- cannot be subverted without casting (
93
- [[expr.const.cast]]). — *end note*]
94
 
95
- Except that any class member declared `mutable` ([[dcl.stc]]) can be
96
- modified, any attempt to modify a `const` object during its lifetime (
97
- [[basic.life]]) results in undefined behavior.
98
 
99
  [*Example 1*:
100
 
101
  ``` cpp
102
  const int ci = 3; // cv-qualified (initialized as required)
103
- ci = 4; // ill-formed: attempt to modify const
104
 
105
  int i = 2; // not cv-qualified
106
  const int* cip; // pointer to const int
107
  cip = &i; // OK: cv-qualified access path to unqualified
108
- *cip = 4; // ill-formed: attempt to modify through ptr to const
109
 
110
  int* ip;
111
  ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
112
  *ip = 4; // defined: *ip points to i, a non-const object
113
 
114
  const int* ciq = new const int (3); // initialized as required
115
  int* iq = const_cast<int*>(ciq); // cast required
116
- *iq = 4; // undefined: modifies a const object
117
  ```
118
 
119
  For another example,
120
 
121
  ``` cpp
@@ -128,14 +127,14 @@ struct Y {
128
  Y();
129
  };
130
 
131
  const Y y;
132
  y.x.i++; // well-formed: mutable member can be modified
133
- y.x.j++; // ill-formed: const-qualified member modified
134
  Y* p = const_cast<Y*>(&y); // cast away const-ness of y
135
  p->x.i = 99; // well-formed: mutable member can be modified
136
- p->x.j = 99; // undefined: modifies a const member
137
  ```
138
 
139
  — *end example*]
140
 
141
  The semantics of an access through a volatile glvalue are
@@ -157,126 +156,226 @@ C. — *end note*]
157
  The simple type specifiers are
158
 
159
  ``` bnf
160
  simple-type-specifier:
161
  nested-name-specifierₒₚₜ type-name
162
- nested-name-specifier 'template' simple-template-id
163
- nested-name-specifierₒₚₜ template-name
164
- 'char'
165
- 'char16_t'
166
- 'char32_t'
167
- 'wchar_t'
168
- 'bool'
169
- 'short'
170
- 'int'
171
- 'long'
172
- 'signed'
173
- 'unsigned'
174
- 'float'
175
- 'double'
176
- 'void'
177
- 'auto'
178
  decltype-specifier
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  ```
180
 
181
  ``` bnf
182
  type-name:
183
  class-name
184
  enum-name
185
  typedef-name
186
- simple-template-id
187
  ```
188
 
 
 
 
 
 
 
 
 
 
189
  ``` bnf
190
- decltype-specifier:
191
- 'decltype' '(' expression ')'
192
- 'decltype' '(' 'auto' ')'
193
  ```
194
 
195
- The *simple-type-specifier* `auto` is a placeholder for a type to be
196
- deduced ([[dcl.spec.auto]]). A *type-specifier* of the form
197
- `typename`ₒₚₜ *nested-name-specifier*ₒₚₜ *template-name* is a
198
- placeholder for a deduced class type ([[dcl.type.class.deduct]]). The
199
- *template-name* shall name a class template that is not an
200
- injected-class-name. The other *simple-type-specifier*s specify either a
201
- previously-declared type, a type determined from an expression, or one
202
- of the fundamental types ([[basic.fundamental]]). Table 
203
- [[tab:simple.type.specifiers]] summarizes the valid combinations of
204
- *simple-type-specifier*s and the types they specify.
205
 
206
- **Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
 
 
 
 
 
 
 
 
 
207
 
208
  | Specifier(s) | Type |
209
- | ---------------------- | -------------------------------------- |
210
  | *type-name* | the type named |
211
  | *simple-template-id* | the type as defined in~ [[temp.names]] |
212
- | *template-name* | placeholder for a type to be deduced |
213
- | char | ``char'' |
214
- | unsigned char | ``unsigned char'' |
215
- | signed char | ``signed char'' |
216
- | char16_t | ``char16_t'' |
217
- | char32_t | ``char32_t'' |
218
- | bool | ``bool'' |
219
- | unsigned | ``unsigned int'' |
220
- | unsigned int | ``unsigned int'' |
221
- | signed | ``int'' |
222
- | signed int | ``int'' |
223
- | int | ``int'' |
224
- | unsigned short int | ``unsigned short int'' |
225
- | unsigned short | ``unsigned short int'' |
226
- | unsigned long int | ``unsigned long int'' |
227
- | unsigned long | ``unsigned long int'' |
228
- | unsigned long long int | ``unsigned long long int'' |
229
- | unsigned long long | ``unsigned long long int'' |
230
- | signed long int | ``long int'' |
231
- | signed long | ``long int'' |
232
- | signed long long int | ``long long int'' |
233
- | signed long long | ``long long int'' |
234
- | long long int | ``long long int'' |
235
- | long long | ``long long int'' |
236
- | long int | ``long int'' |
237
- | long | ``long int'' |
238
- | signed short int | ``short int'' |
239
- | signed short | ``short int'' |
240
- | short int | ``short int'' |
241
- | short | ``short int'' |
242
- | wchar_t | ``wchar_t'' |
243
- | float | ``float'' |
244
- | double | ``double'' |
245
- | long double | ``long double'' |
246
- | void | ``void'' |
247
- | auto | placeholder for a type to be deduced |
248
- | decltype(auto) | placeholder for a type to be deduced |
249
- | decltype(*expression*) | the type as defined below |
250
 
251
 
252
  When multiple *simple-type-specifier*s are allowed, they can be freely
253
  intermixed with other *decl-specifier*s in any order.
254
 
255
- [*Note 1*: It is *implementation-defined* whether objects of `char`
256
  type are represented as signed or unsigned quantities. The `signed`
257
  specifier forces `char` objects to be signed; it is redundant in other
258
  contexts. — *end note*]
259
 
260
- For an expression `e`, the type denoted by `decltype(e)` is defined as
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  follows:
262
 
263
- - if `e` is an unparenthesized *id-expression* naming a structured
264
- binding ([[dcl.struct.bind]]), `decltype(e)` is the referenced type
265
- as given in the specification of the structured binding declaration;
266
- - otherwise, if `e` is an unparenthesized *id-expression* or an
267
- unparenthesized class member access ([[expr.ref]]), `decltype(e)` is
268
- the type of the entity named by `e`. If there is no such entity, or if
269
- `e` names a set of overloaded functions, the program is ill-formed;
270
- - otherwise, if `e` is an xvalue, `decltype(e)` is `T&&`, where `T` is
271
- the type of `e`;
272
- - otherwise, if `e` is an lvalue, `decltype(e)` is `T&`, where `T` is
273
- the type of `e`;
274
- - otherwise, `decltype(e)` is the type of `e`.
 
 
 
 
275
 
276
  The operand of the `decltype` specifier is an unevaluated operand
277
- (Clause  [[expr]]).
278
 
279
  [*Example 1*:
280
 
281
  ``` cpp
282
  const int&& foo();
@@ -289,29 +388,30 @@ decltype(a->x) x3; // type is double
289
  decltype((a->x)) x4 = x3; // type is const double&
290
  ```
291
 
292
  — *end example*]
293
 
294
- [*Note 2*: The rules for determining types involving `decltype(auto)`
295
  are specified in  [[dcl.spec.auto]]. — *end note*]
296
 
297
- If the operand of a *decltype-specifier* is a prvalue, the temporary
298
- materialization conversion is not applied ([[conv.rval]]) and no result
299
- object is provided for the prvalue. The type of the prvalue may be
300
- incomplete.
 
301
 
302
- [*Note 3*: As a result, storage is not allocated for the prvalue and it
303
  is not destroyed. Thus, a class type is not instantiated as a result of
304
  being the type of a function call in this context. In this context, the
305
  common purpose of writing the expression is merely to refer to its type.
306
  In that sense, a *decltype-specifier* is analogous to a use of a
307
  *typedef-name*, so the usual reasons for requiring a complete type do
308
  not apply. In particular, it is not necessary to allocate storage for a
309
  temporary object or to enforce the semantic constraints associated with
310
  invoking the type’s destructor. — *end note*]
311
 
312
- [*Note 4*: Unlike the preceding rule, parentheses have no special
313
  meaning in this context. — *end note*]
314
 
315
  [*Example 2*:
316
 
317
  ``` cpp
@@ -326,149 +426,74 @@ template<class T> auto f(T) // #1
326
  // (A temporary is not introduced as a result of the use of i().)
327
  template<class T> auto f(T) // #2
328
  -> void;
329
  auto g() -> void {
330
  f(42); // OK: calls #2. (#1 is not a viable candidate: type deduction
331
- // fails~([temp.deduct]) because A<int>::~{A()} is implicitly used in its
332
  // decltype-specifier)
333
  }
334
  template<class T> auto q(T)
335
  -> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is not implicitly
336
  // used within the context of this decltype-specifier
337
  void r() {
338
- q(42); // Error: deduction against q succeeds, so overload resolution selects
339
- // the specialization ``q(T) -> decltype((h<T>())) [with T=int]''.
340
- // The return type is A<int>, so a temporary is introduced and its
341
- // destructor is used, so the program is ill-formed.
342
  }
343
  ```
344
 
345
  — *end example*]
346
 
347
- #### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
348
 
349
  ``` bnf
350
- elaborated-type-specifier:
351
- class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
352
- class-key simple-template-id
353
- class-key nested-name-specifier 'template'ₒₚₜ simple-template-id
354
- 'enum' nested-name-specifierₒₚₜ identifier
355
  ```
356
 
357
- An *attribute-specifier-seq* shall not appear in an
358
- *elaborated-type-specifier* unless the latter is the sole constituent of
359
- a declaration. If an *elaborated-type-specifier* is the sole constituent
360
- of a declaration, the declaration is ill-formed unless it is an explicit
361
- specialization ([[temp.expl.spec]]), an explicit instantiation (
362
- [[temp.explicit]]) or it has one of the following forms:
363
 
364
- ``` bnf
365
- class-key attribute-specifier-seqₒₚₜ identifier ';'
366
- 'friend' class-key '::'ₒₚₜ identifier ';'
367
- 'friend' class-key '::'ₒₚₜ simple-template-id ';'
368
- 'friend' class-key nested-name-specifier identifier ';'
369
- 'friend' class-key nested-name-specifier 'template'ₒₚₜ simple-template-id ';'
370
- ```
371
-
372
- In the first case, the *attribute-specifier-seq*, if any, appertains to
373
- the class being declared; the attributes in the
374
- *attribute-specifier-seq* are thereafter considered attributes of the
375
- class whenever it is named.
376
-
377
- [[basic.lookup.elab]] describes how name lookup proceeds for the
378
- *identifier* in an *elaborated-type-specifier*. If the *identifier*
379
- resolves to a *class-name* or *enum-name*, the
380
- *elaborated-type-specifier* introduces it into the declaration the same
381
- way a *simple-type-specifier* introduces its *type-name*. If the
382
- *identifier* resolves to a *typedef-name* or the *simple-template-id*
383
- resolves to an alias template specialization, the
384
- *elaborated-type-specifier* is ill-formed.
385
-
386
- [*Note 1*:
387
-
388
- This implies that, within a class template with a template
389
- *type-parameter* `T`, the declaration
390
-
391
- ``` cpp
392
- friend class T;
393
- ```
394
-
395
- is ill-formed. However, the similar declaration `friend T;` is allowed (
396
- [[class.friend]]).
397
-
398
- — *end note*]
399
-
400
- The *class-key* or `enum` keyword present in the
401
- *elaborated-type-specifier* shall agree in kind with the declaration to
402
- which the name in the *elaborated-type-specifier* refers. This rule also
403
- applies to the form of *elaborated-type-specifier* that declares a
404
- *class-name* or `friend` class since it can be construed as referring to
405
- the definition of the class. Thus, in any *elaborated-type-specifier*,
406
- the `enum` keyword shall be used to refer to an enumeration (
407
- [[dcl.enum]]), the `union` *class-key* shall be used to refer to a union
408
- (Clause  [[class]]), and either the `class` or `struct` *class-key*
409
- shall be used to refer to a class (Clause  [[class]]) declared using the
410
- `class` or `struct` *class-key*.
411
-
412
- [*Example 1*:
413
-
414
- ``` cpp
415
- enum class E { a, b };
416
- enum E x = E::a; // OK
417
- ```
418
-
419
- — *end example*]
420
-
421
- #### The `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
422
 
423
- The `auto` and `decltype(auto)` *type-specifier*s are used to designate
424
- a placeholder type that will be replaced later by deduction from an
425
- initializer. The `auto` *type-specifier* is also used to introduce a
426
- function type having a *trailing-return-type* or to signify that a
427
- lambda is a generic lambda ([[expr.prim.lambda.closure]]). The `auto`
428
- *type-specifier* is also used to introduce a structured binding
429
- declaration ([[dcl.struct.bind]]).
430
 
431
  The placeholder type can appear with a function declarator in the
432
  *decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
433
  *trailing-return-type*, in any context where such a declarator is valid.
434
- If the function declarator includes a *trailing-return-type* (
435
- [[dcl.fct]]), that *trailing-return-type* specifies the declared return
436
  type of the function. Otherwise, the function declarator shall declare a
437
  function. If the declared return type of the function contains a
438
  placeholder type, the return type of the function is deduced from
439
- non-discarded `return` statements, if any, in the body of the function (
440
- [[stmt.if]]).
441
-
442
- If the `auto` *type-specifier* appears as one of the *decl-specifier*s
443
- in the *decl-specifier-seq* of a *parameter-declaration* of a
444
- *lambda-expression*, the lambda is a *generic lambda* (
445
- [[expr.prim.lambda.closure]]).
446
-
447
- [*Example 1*:
448
-
449
- ``` cpp
450
- auto glambda = [](int i, auto a) { return i; }; // OK: a generic lambda
451
- ```
452
-
453
- — *end example*]
454
-
455
- The type of a variable declared using `auto` or `decltype(auto)` is
456
- deduced from its initializer. This use is allowed in an initializing
457
- declaration ([[dcl.init]]) of a variable. `auto` or `decltype(auto)`
458
- shall appear as one of the *decl-specifier*s in the *decl-specifier-seq*
459
- and the *decl-specifier-seq* shall be followed by one or more
460
- *declarator*s, each of which shall be followed by a non-empty
461
- *initializer*. In an *initializer* of the form
462
 
463
  ``` cpp
464
  ( expression-list )
465
  ```
466
 
467
  the *expression-list* shall be a single *assignment-expression*.
468
 
469
- [*Example 2*:
470
 
471
  ``` cpp
472
  auto x = 5; // OK: x has type int
473
  const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
474
  static auto y = 0.0; // OK: y has type double
@@ -478,25 +503,28 @@ auto g() { return 0.0; } // OK: g returns double
478
  auto h(); // OK: h's return type will be deduced when it is defined
479
  ```
480
 
481
  — *end example*]
482
 
 
 
 
483
  A placeholder type can also be used in the *type-specifier-seq* in the
484
- *new-type-id* or *type-id* of a *new-expression* ([[expr.new]]) and as
485
- a *decl-specifier* of the *parameter-declaration*'s *decl-specifier-seq*
486
- in a *template-parameter* ([[temp.param]]).
487
 
488
- A program that uses `auto` or `decltype(auto)` in a context not
489
- explicitly allowed in this section is ill-formed.
490
 
491
  If the *init-declarator-list* contains more than one *init-declarator*,
492
  they shall all form declarations of variables. The type of each declared
493
- variable is determined by placeholder type deduction (
494
- [[dcl.type.auto.deduct]]), and if the type that replaces the placeholder
495
  type is not the same in each deduction, the program is ill-formed.
496
 
497
- [*Example 3*:
498
 
499
  ``` cpp
500
  auto x = 5, *y = &x; // OK: auto is int
501
  auto a = 5, b = { 1, 2 }; // error: different types for auto
502
  ```
@@ -511,53 +539,60 @@ same in each deduction, the program is ill-formed.
511
  If a function with a declared return type that uses a placeholder type
512
  has no non-discarded `return` statements, the return type is deduced as
513
  though from a `return` statement with no operand at the closing brace of
514
  the function body.
515
 
516
- [*Example 4*:
517
 
518
  ``` cpp
519
  auto f() { } // OK, return type is void
520
- auto* g() { } // error, cannot deduce auto* from void()
521
  ```
522
 
523
  — *end example*]
524
 
525
- If the type of an entity with an undeduced placeholder type is needed to
526
- determine the type of an expression, the program is ill-formed. Once a
527
- non-discarded `return` statement has been seen in a function, however,
528
- the return type deduced from that statement can be used in the rest of
529
- the function, including in other `return` statements.
530
 
531
- [*Example 5*:
 
 
 
 
 
 
 
 
 
532
 
533
  ``` cpp
534
- auto n = n; // error, n's type is unknown
535
  auto f();
536
- void g() { &f; } // error, f's return type is unknown
537
  auto sum(int i) {
538
  if (i == 1)
539
  return i; // sum's return type is int
540
  else
541
  return sum(i-1)+i; // OK, sum's return type has been deduced
542
  }
543
  ```
544
 
545
  — *end example*]
546
 
547
- Return type deduction for a function template with a placeholder in its
548
- declared type occurs when the definition is instantiated even if the
549
- function body contains a `return` statement with a non-type-dependent
550
- operand.
551
 
552
- [*Note 1*: Therefore, any use of a specialization of the function
553
  template will cause an implicit instantiation. Any errors that arise
554
  from this instantiation are not in the immediate context of the function
555
- type and can result in the program being ill-formed (
556
- [[temp.deduct]]). — *end note*]
557
 
558
- [*Example 6*:
559
 
560
  ``` cpp
561
  template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
562
  typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
563
  template<class T> auto f(T* t) { return *t; }
@@ -567,49 +602,61 @@ void g() { int (*p)(int*) = &f; } // instantiates both fs to deter
567
 
568
  — *end example*]
569
 
570
  Redeclarations or specializations of a function or function template
571
  with a declared return type that uses a placeholder type shall also use
572
- that placeholder, not a deduced type.
 
 
 
573
 
574
- [*Example 7*:
575
 
576
  ``` cpp
577
  auto f();
578
  auto f() { return 42; } // return type is int
579
  auto f(); // OK
580
- int f(); // error, cannot be overloaded with auto f()
581
- decltype(auto) f(); // error, auto and decltype(auto) don't match
582
 
583
  template <typename T> auto g(T t) { return t; } // #1
584
  template auto g(int); // OK, return type is int
585
- template char g(char); // error, no matching template
586
  template<> auto g(double); // OK, forward declaration with unknown return type
587
 
588
  template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1
589
  template char g(char); // OK, now there is a matching template
590
  template auto g(float); // still matches #1
591
 
592
- void h() { return g(42); } // error, ambiguous
593
 
594
  template <typename T> struct A {
595
  friend T frf(T);
596
  };
597
  auto frf(int i) { return i; } // not a friend of A<int>
 
 
 
 
 
 
598
  ```
599
 
600
  — *end example*]
601
 
602
  A function declared with a return type that uses a placeholder type
603
- shall not be `virtual` ([[class.virtual]]).
604
 
605
- An explicit instantiation declaration ([[temp.explicit]]) does not
606
- cause the instantiation of an entity declared using a placeholder type,
607
- but it also does not prevent that entity from being instantiated as
608
- needed to determine its type.
609
 
610
- [*Example 8*:
 
 
 
 
 
611
 
612
  ``` cpp
613
  template <typename T> auto f(T t) { return t; }
614
  extern template auto f(int); // does not instantiate f<int>
615
  int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
@@ -622,45 +669,46 @@ int (*p)(int) = f; // instantiates f<int> to determine its return t
622
 
623
  *Placeholder type deduction* is the process by which a type containing a
624
  placeholder type is replaced by a deduced type.
625
 
626
  A type `T` containing a placeholder type, and a corresponding
627
- initializer `e`, are determined as follows:
628
 
629
  - for a non-discarded `return` statement that occurs in a function
630
  declared with a return type that contains a placeholder type, `T` is
631
- the declared return type and `e` is the operand of the `return`
632
- statement. If the `return` statement has no operand, then `e` is
633
  `void()`;
634
  - for a variable declared with a type that contains a placeholder type,
635
- `T` is the declared type of the variable and `e` is the initializer.
636
- If the initialization is direct-list-initialization, the initializer
637
  shall be a *braced-init-list* containing only a single
638
- *assignment-expression* and `e` is the *assignment-expression*;
639
  - for a non-type template parameter declared with a type that contains a
640
  placeholder type, `T` is the declared type of the non-type template
641
- parameter and `e` is the corresponding template argument.
642
 
643
  In the case of a `return` statement with no operand or with an operand
644
- of type `void`, `T` shall be either `decltype(auto)` or cv `auto`.
 
645
 
646
- If the deduction is for a `return` statement and `e` is a
647
- *braced-init-list* ([[dcl.init.list]]), the program is ill-formed.
648
 
649
- If the placeholder is the `auto` *type-specifier*, the deduced type T'
650
- replacing `T` is determined using the rules for template argument
651
- deduction. Obtain `P` from `T` by replacing the occurrences of `auto`
652
- with either a new invented type template parameter `U` or, if the
653
- initialization is copy-list-initialization, with
654
- `std::initializer_list<U>`. Deduce a value for `U` using the rules of
655
- template argument deduction from a function call (
656
- [[temp.deduct.call]]), where `P` is a function template parameter type
657
- and the corresponding argument is `e`. If the deduction fails, the
658
- declaration is ill-formed. Otherwise, T' is obtained by substituting the
659
- deduced `U` into `P`.
660
 
661
- [*Example 9*:
662
 
663
  ``` cpp
664
  auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
665
  auto x2 = { 1, 2.0 }; // error: cannot deduce element type
666
  auto x3{ 1, 2 }; // error: not a single element
@@ -668,11 +716,11 @@ auto x4 = { 3 }; // decltype(x4) is std::initializer_list<int>
668
  auto x5{ 3 }; // decltype(x5) is int
669
  ```
670
 
671
  — *end example*]
672
 
673
- [*Example 10*:
674
 
675
  ``` cpp
676
  const auto &i = expr;
677
  ```
678
 
@@ -683,16 +731,16 @@ The type of `i` is the deduced type of the parameter `u` in the call
683
  template <class U> void f(const U& u);
684
  ```
685
 
686
  — *end example*]
687
 
688
- If the placeholder is the `decltype(auto)` *type-specifier*, `T` shall
689
- be the placeholder alone. The type deduced for `T` is determined as
690
- described in  [[dcl.type.simple]], as though `e` had been the operand of
691
- the `decltype`.
692
 
693
- [*Example 11*:
694
 
695
  ``` cpp
696
  int i;
697
  int&& f();
698
  auto x2a(i); // decltype(x2a) is int
@@ -702,36 +750,56 @@ decltype(auto) x3d = i; // decltype(x3d) is int
702
  auto x4a = (i); // decltype(x4a) is int
703
  decltype(auto) x4d = (i); // decltype(x4d) is int&
704
  auto x5a = f(); // decltype(x5a) is int
705
  decltype(auto) x5d = f(); // decltype(x5d) is int&&
706
  auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
707
- decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression
708
  auto *x7a = &i; // decltype(x7a) is int*
709
- decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)
710
  ```
711
 
712
  — *end example*]
713
 
 
 
 
 
714
  #### Deduced class template specialization types <a id="dcl.type.class.deduct">[[dcl.type.class.deduct]]</a>
715
 
716
  If a placeholder for a deduced class type appears as a *decl-specifier*
717
- in the *decl-specifier-seq* of an initializing declaration (
718
- [[dcl.init]]) of a variable, the placeholder is replaced by the return
719
- type of the function selected by overload resolution for class template
720
- deduction ([[over.match.class.deduct]]). If the *decl-specifier-seq* is
721
- followed by an *init-declarator-list* or *member-declarator-list*
722
- containing more than one *declarator*, the type that replaces the
723
- placeholder shall be the same in each deduction.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
724
 
725
  A placeholder for a deduced class type can also be used in the
726
  *type-specifier-seq* in the *new-type-id* or *type-id* of a
727
- *new-expression* ([[expr.new]]), or as the *simple-type-specifier* in
728
- an explicit type conversion (functional notation) ([[expr.type.conv]]).
729
- A placeholder for a deduced class type shall not appear in any other
730
- context.
 
731
 
732
- [*Example 1*:
733
 
734
  ``` cpp
735
  template<class T> struct container {
736
  container(T t) {}
737
  template<class Iter> container(Iter beg, Iter end);
@@ -740,10 +808,10 @@ template<class Iter>
740
  container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>;
741
  std::vector<double> v = { ... };
742
 
743
  container c(7); // OK, deduces int for T
744
  auto d = container(v.begin(), v.end()); // OK, deduces double for T
745
- container e{5, 6}; // error, int is not an iterator
746
  ```
747
 
748
  — *end example*]
749
 
 
29
  defining-type-specifier defining-type-specifier-seq
30
  ```
31
 
32
  The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
33
  *defining-type-specifier-seq* appertains to the type denoted by the
34
+ preceding *type-specifier*s or *defining-type-specifier*s
35
+ [[dcl.meaning]]. The *attribute-specifier-seq* affects the type only for
36
+ the declaration it appears in, not other declarations involving the same
37
+ type.
38
 
39
  As a general rule, at most one *defining-type-specifier* is allowed in
40
  the complete *decl-specifier-seq* of a *declaration* or in a
41
  *defining-type-specifier-seq*, and at most one *type-specifier* is
42
  allowed in a *type-specifier-seq*. The only exceptions to this rule are
 
51
  - `long` can be combined with `long`.
52
 
53
  Except in a declaration of a constructor, destructor, or conversion
54
  function, at least one *defining-type-specifier* that is not a
55
  *cv-qualifier* shall appear in a complete *type-specifier-seq* or a
56
+ complete *decl-specifier-seq*.[^1]
57
 
58
  [*Note 1*: *enum-specifier*s, *class-specifier*s, and
59
+ *typename-specifier*s are discussed in [[dcl.enum]], [[class]], and
60
+ [[temp.res]], respectively. The remaining *type-specifier*s are
61
+ discussed in the rest of this subclause. — *end note*]
62
 
63
  #### The *cv-qualifier*s <a id="dcl.type.cv">[[dcl.type.cv]]</a>
64
 
65
  There are two *cv-qualifier*s, `const` and `volatile`. Each
66
  *cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
 
74
  Redundant cv-qualifications are ignored.
75
 
76
  [*Note 2*: For example, these could be introduced by
77
  typedefs. — *end note*]
78
 
79
+ [*Note 3*: Declaring a variable `const` can affect its linkage
80
+ [[dcl.stc]] and its usability in constant expressions [[expr.const]]. As
81
+ described in  [[dcl.init]], the definition of an object or subobject of
82
+ const-qualified type must specify an initializer or be subject to
83
+ default-initialization. — *end note*]
84
 
85
  A pointer or reference to a cv-qualified type need not actually point or
86
  refer to a cv-qualified object, but it is treated as if it does; a
87
  const-qualified access path cannot be used to modify an object even if
88
  the object referenced is a non-const object and can be modified through
89
  some other access path.
90
 
91
  [*Note 4*: Cv-qualifiers are supported by the type system so that they
92
+ cannot be subverted without casting [[expr.const.cast]]. — *end note*]
 
93
 
94
+ Any attempt to modify ([[expr.ass]], [[expr.post.incr]],
95
+ [[expr.pre.incr]]) a const object [[basic.type.qualifier]] during its
96
+ lifetime [[basic.life]] results in undefined behavior.
97
 
98
  [*Example 1*:
99
 
100
  ``` cpp
101
  const int ci = 3; // cv-qualified (initialized as required)
102
+ ci = 4; // error: attempt to modify const
103
 
104
  int i = 2; // not cv-qualified
105
  const int* cip; // pointer to const int
106
  cip = &i; // OK: cv-qualified access path to unqualified
107
+ *cip = 4; // error: attempt to modify through ptr to const
108
 
109
  int* ip;
110
  ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
111
  *ip = 4; // defined: *ip points to i, a non-const object
112
 
113
  const int* ciq = new const int (3); // initialized as required
114
  int* iq = const_cast<int*>(ciq); // cast required
115
+ *iq = 4; // undefined behavior: modifies a const object
116
  ```
117
 
118
  For another example,
119
 
120
  ``` cpp
 
127
  Y();
128
  };
129
 
130
  const Y y;
131
  y.x.i++; // well-formed: mutable member can be modified
132
+ y.x.j++; // error: const-qualified member modified
133
  Y* p = const_cast<Y*>(&y); // cast away const-ness of y
134
  p->x.i = 99; // well-formed: mutable member can be modified
135
+ p->x.j = 99; // undefined behavior: modifies a const subobject
136
  ```
137
 
138
  — *end example*]
139
 
140
  The semantics of an access through a volatile glvalue are
 
156
  The simple type specifiers are
157
 
158
  ``` bnf
159
  simple-type-specifier:
160
  nested-name-specifierₒₚₜ type-name
161
+ nested-name-specifier template simple-template-id
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  decltype-specifier
163
+ placeholder-type-specifier
164
+ nested-name-specifierₒₚₜ template-name
165
+ char
166
+ char8_t
167
+ char16_t
168
+ char32_t
169
+ wchar_t
170
+ bool
171
+ short
172
+ int
173
+ long
174
+ signed
175
+ unsigned
176
+ float
177
+ double
178
+ void
179
  ```
180
 
181
  ``` bnf
182
  type-name:
183
  class-name
184
  enum-name
185
  typedef-name
 
186
  ```
187
 
188
+ A *placeholder-type-specifier* is a placeholder for a type to be deduced
189
+ [[dcl.spec.auto]]. A *type-specifier* of the form `typename`ₒₚₜ
190
+ *nested-name-specifier*ₒₚₜ *template-name* is a placeholder for a
191
+ deduced class type [[dcl.type.class.deduct]]. The
192
+ *nested-name-specifier*, if any, shall be non-dependent and the
193
+ *template-name* shall name a deducible template. A *deducible template*
194
+ is either a class template or is an alias template whose
195
+ *defining-type-id* is of the form
196
+
197
  ``` bnf
198
+ typenameₒₚₜ nested-name-specifierₒₚₜ templateₒₚₜ simple-template-id
 
 
199
  ```
200
 
201
+ where the *nested-name-specifier* (if any) is non-dependent and the
202
+ *template-name* of the *simple-template-id* names a deducible template.
 
 
 
 
 
 
 
 
203
 
204
+ [*Note 1*: An injected-class-name is never interpreted as a
205
+ *template-name* in contexts where class template argument deduction
206
+ would be performed [[temp.local]]. — *end note*]
207
+
208
+ The other *simple-type-specifier*s specify either a previously-declared
209
+ type, a type determined from an expression, or one of the fundamental
210
+ types [[basic.fundamental]]. [[dcl.type.simple]] summarizes the valid
211
+ combinations of *simple-type-specifier*s and the types they specify.
212
+
213
+ **Table: *simple-type-specifier*{s} and the types they specify** <a id="dcl.type.simple">[dcl.type.simple]</a>
214
 
215
  | Specifier(s) | Type |
216
+ | ---------------------------- | ------------------------------------------------- |
217
  | *type-name* | the type named |
218
  | *simple-template-id* | the type as defined in~ [[temp.names]] |
219
+ | *decltype-specifier* | the type as defined in~ [[dcl.type.decltype]] |
220
+ | *placeholder-type-specifier* | the type as defined in~ [[dcl.spec.auto]] |
221
+ | *template-name* | the type as defined in~ [[dcl.type.class.deduct]] |
222
+ | `char` | ```char`'' |
223
+ | `unsigned char` | ```unsigned char`'' |
224
+ | `signed char` | ```signed char`'' |
225
+ | `char8_t` | ```char8_t`'' |
226
+ | `char16_t` | ```char16_t`'' |
227
+ | `char32_t` | ```char32_t`'' |
228
+ | `bool` | ```bool`'' |
229
+ | `unsigned` | ```unsigned int`'' |
230
+ | `unsigned int` | ```unsigned int`'' |
231
+ | `signed` | ```int`'' |
232
+ | `signed int` | ```int`'' |
233
+ | `int` | ```int`'' |
234
+ | `unsigned short int` | ```unsigned short int`'' |
235
+ | `unsigned short` | ```unsigned short int`'' |
236
+ | `unsigned long int` | ```unsigned long int`'' |
237
+ | `unsigned long` | ```unsigned long int`'' |
238
+ | `unsigned long long int` | ```unsigned long long int`'' |
239
+ | `unsigned long long` | ```unsigned long long int`'' |
240
+ | `signed long int` | ```long int`'' |
241
+ | `signed long` | ```long int`'' |
242
+ | `signed long long int` | ```long long int`'' |
243
+ | `signed long long` | ```long long int`'' |
244
+ | `long long int` | ```long long int`'' |
245
+ | `long long` | ```long long int`'' |
246
+ | `long int` | ```long int`'' |
247
+ | `long` | ```long int`'' |
248
+ | `signed short int` | ```short int`'' |
249
+ | `signed short` | ```short int`'' |
250
+ | `short int` | ```short int`'' |
251
+ | `short` | ```short int`'' |
252
+ | `wchar_t` | ```wchar_t`'' |
253
+ | `float` | ```float`'' |
254
+ | `double` | ```double`'' |
255
+ | `long double` | ```long double`'' |
256
+ | `void` | ```void`'' |
257
 
258
 
259
  When multiple *simple-type-specifier*s are allowed, they can be freely
260
  intermixed with other *decl-specifier*s in any order.
261
 
262
+ [*Note 2*: It is *implementation-defined* whether objects of `char`
263
  type are represented as signed or unsigned quantities. The `signed`
264
  specifier forces `char` objects to be signed; it is redundant in other
265
  contexts. — *end note*]
266
 
267
+ #### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
268
+
269
+ ``` bnf
270
+ elaborated-type-specifier:
271
+ class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
272
+ class-key simple-template-id
273
+ class-key nested-name-specifier templateₒₚₜ simple-template-id
274
+ elaborated-enum-specifier
275
+ ```
276
+
277
+ ``` bnf
278
+ elaborated-enum-specifier:
279
+ enum nested-name-specifierₒₚₜ identifier
280
+ ```
281
+
282
+ An *attribute-specifier-seq* shall not appear in an
283
+ *elaborated-type-specifier* unless the latter is the sole constituent of
284
+ a declaration. If an *elaborated-type-specifier* is the sole constituent
285
+ of a declaration, the declaration is ill-formed unless it is an explicit
286
+ specialization [[temp.expl.spec]], an explicit instantiation
287
+ [[temp.explicit]] or it has one of the following forms:
288
+
289
+ ``` bnf
290
+ class-key attribute-specifier-seqₒₚₜ identifier ';'
291
+ friend class-key '::ₒₚₜ ' identifier ';'
292
+ friend class-key '::ₒₚₜ ' simple-template-id ';'
293
+ friend class-key nested-name-specifier identifier ';'
294
+ friend class-key nested-name-specifier templateₒₚₜ simple-template-id ';'
295
+ ```
296
+
297
+ In the first case, the *attribute-specifier-seq*, if any, appertains to
298
+ the class being declared; the attributes in the
299
+ *attribute-specifier-seq* are thereafter considered attributes of the
300
+ class whenever it is named.
301
+
302
+ [*Note 1*: [[basic.lookup.elab]] describes how name lookup proceeds
303
+ for the *identifier* in an *elaborated-type-specifier*. — *end note*]
304
+
305
+ If the *identifier* or *simple-template-id* resolves to a *class-name*
306
+ or *enum-name*, the *elaborated-type-specifier* introduces it into the
307
+ declaration the same way a *simple-type-specifier* introduces its
308
+ *type-name* [[dcl.type.simple]]. If the *identifier* or
309
+ *simple-template-id* resolves to a *typedef-name* ([[dcl.typedef]],
310
+ [[temp.names]]), the *elaborated-type-specifier* is ill-formed.
311
+
312
+ [*Note 2*:
313
+
314
+ This implies that, within a class template with a template
315
+ *type-parameter* `T`, the declaration
316
+
317
+ ``` cpp
318
+ friend class T;
319
+ ```
320
+
321
+ is ill-formed. However, the similar declaration `friend T;` is allowed
322
+ [[class.friend]].
323
+
324
+ — *end note*]
325
+
326
+ The *class-key* or `enum` keyword present in the
327
+ *elaborated-type-specifier* shall agree in kind with the declaration to
328
+ which the name in the *elaborated-type-specifier* refers. This rule also
329
+ applies to the form of *elaborated-type-specifier* that declares a
330
+ *class-name* or friend class since it can be construed as referring to
331
+ the definition of the class. Thus, in any *elaborated-type-specifier*,
332
+ the `enum` keyword shall be used to refer to an enumeration
333
+ [[dcl.enum]], the `union` *class-key* shall be used to refer to a union
334
+ [[class.union]], and either the `class` or `struct` *class-key* shall be
335
+ used to refer to a non-union class [[class.pre]].
336
+
337
+ [*Example 1*:
338
+
339
+ ``` cpp
340
+ enum class E { a, b };
341
+ enum E x = E::a; // OK
342
+ struct S { } s;
343
+ class S* p = &s; // OK
344
+ ```
345
+
346
+ — *end example*]
347
+
348
+ #### Decltype specifiers <a id="dcl.type.decltype">[[dcl.type.decltype]]</a>
349
+
350
+ ``` bnf
351
+ decltype-specifier:
352
+ decltype '(' expression ')'
353
+ ```
354
+
355
+ For an expression E, the type denoted by `decltype(E)` is defined as
356
  follows:
357
 
358
+ - if E is an unparenthesized *id-expression* naming a structured binding
359
+ [[dcl.struct.bind]], `decltype(E)` is the referenced type as given in
360
+ the specification of the structured binding declaration;
361
+ - otherwise, if E is an unparenthesized *id-expression* naming a
362
+ non-type *template-parameter* [[temp.param]], `decltype(E)` is the
363
+ type of the *template-parameter* after performing any necessary type
364
+ deduction ([[dcl.spec.auto]], [[dcl.type.class.deduct]]);
365
+ - otherwise, if E is an unparenthesized *id-expression* or an
366
+ unparenthesized class member access [[expr.ref]], `decltype(E)` is the
367
+ type of the entity named by E. If there is no such entity, or if E
368
+ names a set of overloaded functions, the program is ill-formed;
369
+ - otherwise, if E is an xvalue, `decltype(E)` is `T&&`, where `T` is the
370
+ type of E;
371
+ - otherwise, if E is an lvalue, `decltype(E)` is `T&`, where `T` is the
372
+ type of E;
373
+ - otherwise, `decltype(E)` is the type of E.
374
 
375
  The operand of the `decltype` specifier is an unevaluated operand
376
+ [[expr.prop]].
377
 
378
  [*Example 1*:
379
 
380
  ``` cpp
381
  const int&& foo();
 
388
  decltype((a->x)) x4 = x3; // type is const double&
389
  ```
390
 
391
  — *end example*]
392
 
393
+ [*Note 1*: The rules for determining types involving `decltype(auto)`
394
  are specified in  [[dcl.spec.auto]]. — *end note*]
395
 
396
+ If the operand of a *decltype-specifier* is a prvalue and is not a
397
+ (possibly parenthesized) immediate invocation [[expr.const]], the
398
+ temporary materialization conversion is not applied [[conv.rval]] and no
399
+ result object is provided for the prvalue. The type of the prvalue may
400
+ be incomplete or an abstract class type.
401
 
402
+ [*Note 2*: As a result, storage is not allocated for the prvalue and it
403
  is not destroyed. Thus, a class type is not instantiated as a result of
404
  being the type of a function call in this context. In this context, the
405
  common purpose of writing the expression is merely to refer to its type.
406
  In that sense, a *decltype-specifier* is analogous to a use of a
407
  *typedef-name*, so the usual reasons for requiring a complete type do
408
  not apply. In particular, it is not necessary to allocate storage for a
409
  temporary object or to enforce the semantic constraints associated with
410
  invoking the type’s destructor. — *end note*]
411
 
412
+ [*Note 3*: Unlike the preceding rule, parentheses have no special
413
  meaning in this context. — *end note*]
414
 
415
  [*Example 2*:
416
 
417
  ``` cpp
 
426
  // (A temporary is not introduced as a result of the use of i().)
427
  template<class T> auto f(T) // #2
428
  -> void;
429
  auto g() -> void {
430
  f(42); // OK: calls #2. (#1 is not a viable candidate: type deduction
431
+ // fails[temp.deduct] because A<int>::~A() is implicitly used in its
432
  // decltype-specifier)
433
  }
434
  template<class T> auto q(T)
435
  -> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is not implicitly
436
  // used within the context of this decltype-specifier
437
  void r() {
438
+ q(42); // error: deduction against q succeeds, so overload resolution selects
439
+ // the specialization ``q(T) -> decltype((h<T>()))'' with T=int;
440
+ // the return type is A<int>, so a temporary is introduced and its
441
+ // destructor is used, so the program is ill-formed
442
  }
443
  ```
444
 
445
  — *end example*]
446
 
447
+ #### Placeholder type specifiers <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
448
 
449
  ``` bnf
450
+ placeholder-type-specifier:
451
+ type-constraintₒₚₜ auto
452
+ type-constraintₒₚₜ decltype '(' auto ')'
 
 
453
  ```
454
 
455
+ A *placeholder-type-specifier* designates a placeholder type that will
456
+ be replaced later by deduction from an initializer.
 
 
 
 
457
 
458
+ A *placeholder-type-specifier* of the form *type-constraint*ₒₚₜ `auto`
459
+ can be used as a *decl-specifier* of the *decl-specifier-seq* of a
460
+ *parameter-declaration* of a function declaration or *lambda-expression*
461
+ and, if it is not the `auto` *type-specifier* introducing a
462
+ *trailing-return-type* (see below), is a *generic parameter type
463
+ placeholder* of the function declaration or *lambda-expression*.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
464
 
465
+ [*Note 1*: Having a generic parameter type placeholder signifies that
466
+ the function is an abbreviated function template [[dcl.fct]] or the
467
+ lambda is a generic lambda [[expr.prim.lambda]]. *end note*]
 
 
 
 
468
 
469
  The placeholder type can appear with a function declarator in the
470
  *decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
471
  *trailing-return-type*, in any context where such a declarator is valid.
472
+ If the function declarator includes a *trailing-return-type*
473
+ [[dcl.fct]], that *trailing-return-type* specifies the declared return
474
  type of the function. Otherwise, the function declarator shall declare a
475
  function. If the declared return type of the function contains a
476
  placeholder type, the return type of the function is deduced from
477
+ non-discarded `return` statements, if any, in the body of the function
478
+ [[stmt.if]].
479
+
480
+ The type of a variable declared using a placeholder type is deduced from
481
+ its initializer. This use is allowed in an initializing declaration
482
+ [[dcl.init]] of a variable. The placeholder type shall appear as one of
483
+ the *decl-specifier*s in the *decl-specifier-seq* and the
484
+ *decl-specifier-seq* shall be followed by one or more *declarator*s,
485
+ each of which shall be followed by a non-empty *initializer*. In an
486
+ *initializer* of the form
 
 
 
 
 
 
 
 
 
 
 
 
 
487
 
488
  ``` cpp
489
  ( expression-list )
490
  ```
491
 
492
  the *expression-list* shall be a single *assignment-expression*.
493
 
494
+ [*Example 1*:
495
 
496
  ``` cpp
497
  auto x = 5; // OK: x has type int
498
  const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
499
  static auto y = 0.0; // OK: y has type double
 
503
  auto h(); // OK: h's return type will be deduced when it is defined
504
  ```
505
 
506
  — *end example*]
507
 
508
+ The `auto` *type-specifier* can also be used to introduce a structured
509
+ binding declaration [[dcl.struct.bind]].
510
+
511
  A placeholder type can also be used in the *type-specifier-seq* in the
512
+ *new-type-id* or *type-id* of a *new-expression* [[expr.new]] and as a
513
+ *decl-specifier* of the *parameter-declaration*'s *decl-specifier-seq*
514
+ in a *template-parameter* [[temp.param]].
515
 
516
+ A program that uses a placeholder type in a context not explicitly
517
+ allowed in this subclause is ill-formed.
518
 
519
  If the *init-declarator-list* contains more than one *init-declarator*,
520
  they shall all form declarations of variables. The type of each declared
521
+ variable is determined by placeholder type deduction
522
+ [[dcl.type.auto.deduct]], and if the type that replaces the placeholder
523
  type is not the same in each deduction, the program is ill-formed.
524
 
525
+ [*Example 2*:
526
 
527
  ``` cpp
528
  auto x = 5, *y = &x; // OK: auto is int
529
  auto a = 5, b = { 1, 2 }; // error: different types for auto
530
  ```
 
539
  If a function with a declared return type that uses a placeholder type
540
  has no non-discarded `return` statements, the return type is deduced as
541
  though from a `return` statement with no operand at the closing brace of
542
  the function body.
543
 
544
+ [*Example 3*:
545
 
546
  ``` cpp
547
  auto f() { } // OK, return type is void
548
+ auto* g() { } // error: cannot deduce auto* from void()
549
  ```
550
 
551
  — *end example*]
552
 
553
+ An exported function with a declared return type that uses a placeholder
554
+ type shall be defined in the translation unit containing its exported
555
+ declaration, outside the *private-module-fragment* (if any).
 
 
556
 
557
+ [*Note 2*: The deduced return type cannot have a name with internal
558
+ linkage [[basic.link]]. — *end note*]
559
+
560
+ If the name of an entity with an undeduced placeholder type appears in
561
+ an expression, the program is ill-formed. Once a non-discarded `return`
562
+ statement has been seen in a function, however, the return type deduced
563
+ from that statement can be used in the rest of the function, including
564
+ in other `return` statements.
565
+
566
+ [*Example 4*:
567
 
568
  ``` cpp
569
+ auto n = n; // error: n's initializer refers to n
570
  auto f();
571
+ void g() { &f; } // error: f's return type is unknown
572
  auto sum(int i) {
573
  if (i == 1)
574
  return i; // sum's return type is int
575
  else
576
  return sum(i-1)+i; // OK, sum's return type has been deduced
577
  }
578
  ```
579
 
580
  — *end example*]
581
 
582
+ Return type deduction for a templated entity that is a function or
583
+ function template with a placeholder in its declared type occurs when
584
+ the definition is instantiated even if the function body contains a
585
+ `return` statement with a non-type-dependent operand.
586
 
587
+ [*Note 3*: Therefore, any use of a specialization of the function
588
  template will cause an implicit instantiation. Any errors that arise
589
  from this instantiation are not in the immediate context of the function
590
+ type and can result in the program being ill-formed
591
+ [[temp.deduct]]. — *end note*]
592
 
593
+ [*Example 5*:
594
 
595
  ``` cpp
596
  template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
597
  typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
598
  template<class T> auto f(T* t) { return *t; }
 
602
 
603
  — *end example*]
604
 
605
  Redeclarations or specializations of a function or function template
606
  with a declared return type that uses a placeholder type shall also use
607
+ that placeholder, not a deduced type. Similarly, redeclarations or
608
+ specializations of a function or function template with a declared
609
+ return type that does not use a placeholder type shall not use a
610
+ placeholder.
611
 
612
+ [*Example 6*:
613
 
614
  ``` cpp
615
  auto f();
616
  auto f() { return 42; } // return type is int
617
  auto f(); // OK
618
+ int f(); // error: cannot be overloaded with auto f()
619
+ decltype(auto) f(); // error: auto and decltype(auto) don't match
620
 
621
  template <typename T> auto g(T t) { return t; } // #1
622
  template auto g(int); // OK, return type is int
623
+ template char g(char); // error: no matching template
624
  template<> auto g(double); // OK, forward declaration with unknown return type
625
 
626
  template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1
627
  template char g(char); // OK, now there is a matching template
628
  template auto g(float); // still matches #1
629
 
630
+ void h() { return g(42); } // error: ambiguous
631
 
632
  template <typename T> struct A {
633
  friend T frf(T);
634
  };
635
  auto frf(int i) { return i; } // not a friend of A<int>
636
+ extern int v;
637
+ auto v = 17; // OK, redeclares v
638
+ struct S {
639
+ static int i;
640
+ };
641
+ auto S::i = 23; // OK
642
  ```
643
 
644
  — *end example*]
645
 
646
  A function declared with a return type that uses a placeholder type
647
+ shall not be `virtual` [[class.virtual]].
648
 
649
+ A function declared with a return type that uses a placeholder type
650
+ shall not be a coroutine [[dcl.fct.def.coroutine]].
 
 
651
 
652
+ An explicit instantiation declaration [[temp.explicit]] does not cause
653
+ the instantiation of an entity declared using a placeholder type, but it
654
+ also does not prevent that entity from being instantiated as needed to
655
+ determine its type.
656
+
657
+ [*Example 7*:
658
 
659
  ``` cpp
660
  template <typename T> auto f(T t) { return t; }
661
  extern template auto f(int); // does not instantiate f<int>
662
  int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
 
669
 
670
  *Placeholder type deduction* is the process by which a type containing a
671
  placeholder type is replaced by a deduced type.
672
 
673
  A type `T` containing a placeholder type, and a corresponding
674
+ initializer E, are determined as follows:
675
 
676
  - for a non-discarded `return` statement that occurs in a function
677
  declared with a return type that contains a placeholder type, `T` is
678
+ the declared return type and E is the operand of the `return`
679
+ statement. If the `return` statement has no operand, then E is
680
  `void()`;
681
  - for a variable declared with a type that contains a placeholder type,
682
+ `T` is the declared type of the variable and E is the initializer. If
683
+ the initialization is direct-list-initialization, the initializer
684
  shall be a *braced-init-list* containing only a single
685
+ *assignment-expression* and E is the *assignment-expression*;
686
  - for a non-type template parameter declared with a type that contains a
687
  placeholder type, `T` is the declared type of the non-type template
688
+ parameter and E is the corresponding template argument.
689
 
690
  In the case of a `return` statement with no operand or with an operand
691
+ of type `void`, `T` shall be either *type-constraint*ₒₚₜ
692
+ `decltype(auto)` or cv *type-constraint*ₒₚₜ `auto`.
693
 
694
+ If the deduction is for a `return` statement and E is a
695
+ *braced-init-list* [[dcl.init.list]], the program is ill-formed.
696
 
697
+ If the *placeholder-type-specifier* is of the form *type-constraint*ₒₚₜ
698
+ `auto`, the deduced type T' replacing `T` is determined using the rules
699
+ for template argument deduction. Obtain `P` from `T` by replacing the
700
+ occurrences of *type-constraint*ₒₚₜ `auto` either with a new invented
701
+ type template parameter `U` or, if the initialization is
702
+ copy-list-initialization, with `std::initializer_list<U>`. Deduce a
703
+ value for `U` using the rules of template argument deduction from a
704
+ function call [[temp.deduct.call]], where `P` is a function template
705
+ parameter type and the corresponding argument is E. If the deduction
706
+ fails, the declaration is ill-formed. Otherwise, T' is obtained by
707
+ substituting the deduced `U` into `P`.
708
 
709
+ [*Example 8*:
710
 
711
  ``` cpp
712
  auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
713
  auto x2 = { 1, 2.0 }; // error: cannot deduce element type
714
  auto x3{ 1, 2 }; // error: not a single element
 
716
  auto x5{ 3 }; // decltype(x5) is int
717
  ```
718
 
719
  — *end example*]
720
 
721
+ [*Example 9*:
722
 
723
  ``` cpp
724
  const auto &i = expr;
725
  ```
726
 
 
731
  template <class U> void f(const U& u);
732
  ```
733
 
734
  — *end example*]
735
 
736
+ If the *placeholder-type-specifier* is of the form *type-constraint*ₒₚₜ
737
+ `decltype(auto)`, `T` shall be the placeholder alone. The type deduced
738
+ for `T` is determined as described in  [[dcl.type.simple]], as though E
739
+ had been the operand of the `decltype`.
740
 
741
+ [*Example 10*:
742
 
743
  ``` cpp
744
  int i;
745
  int&& f();
746
  auto x2a(i); // decltype(x2a) is int
 
750
  auto x4a = (i); // decltype(x4a) is int
751
  decltype(auto) x4d = (i); // decltype(x4d) is int&
752
  auto x5a = f(); // decltype(x5a) is int
753
  decltype(auto) x5d = f(); // decltype(x5d) is int&&
754
  auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
755
+ decltype(auto) x6d = { 1, 2 }; // error: { 1, 2 } is not an expression
756
  auto *x7a = &i; // decltype(x7a) is int*
757
+ decltype(auto)*x7d = &i; // error: declared type is not plain decltype(auto)
758
  ```
759
 
760
  — *end example*]
761
 
762
+ For a *placeholder-type-specifier* with a *type-constraint*, the
763
+ immediately-declared constraint [[temp.param]] of the *type-constraint*
764
+ for the type deduced for the placeholder shall be satisfied.
765
+
766
  #### Deduced class template specialization types <a id="dcl.type.class.deduct">[[dcl.type.class.deduct]]</a>
767
 
768
  If a placeholder for a deduced class type appears as a *decl-specifier*
769
+ in the *decl-specifier-seq* of an initializing declaration [[dcl.init]]
770
+ of a variable, the declared type of the variable shall be cv `T`, where
771
+ `T` is the placeholder.
772
+
773
+ [*Example 1*:
774
+
775
+ ``` cpp
776
+ template <class ...T> struct A {
777
+ A(T...) {}
778
+ };
779
+ A x[29]{}; // error: no declarator operators allowed
780
+ const A& y{}; // error: no declarator operators allowed
781
+ ```
782
+
783
+ — *end example*]
784
+
785
+ The placeholder is replaced by the return type of the function selected
786
+ by overload resolution for class template deduction
787
+ [[over.match.class.deduct]]. If the *decl-specifier-seq* is followed by
788
+ an *init-declarator-list* or *member-declarator-list* containing more
789
+ than one *declarator*, the type that replaces the placeholder shall be
790
+ the same in each deduction.
791
 
792
  A placeholder for a deduced class type can also be used in the
793
  *type-specifier-seq* in the *new-type-id* or *type-id* of a
794
+ *new-expression* [[expr.new]], as the *simple-type-specifier* in an
795
+ explicit type conversion (functional notation) [[expr.type.conv]], or as
796
+ the *type-specifier* in the *parameter-declaration* of a
797
+ *template-parameter* [[temp.param]]. A placeholder for a deduced class
798
+ type shall not appear in any other context.
799
 
800
+ [*Example 2*:
801
 
802
  ``` cpp
803
  template<class T> struct container {
804
  container(T t) {}
805
  template<class Iter> container(Iter beg, Iter end);
 
808
  container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>;
809
  std::vector<double> v = { ... };
810
 
811
  container c(7); // OK, deduces int for T
812
  auto d = container(v.begin(), v.end()); // OK, deduces double for T
813
+ container e{5, 6}; // error: int is not an iterator
814
  ```
815
 
816
  — *end example*]
817