From Jason Turner

[dcl.spec]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpu_7iaep2/{from.md → to.md} +588 -326
tmp/tmpu_7iaep2/{from.md → to.md} RENAMED
@@ -3,15 +3,16 @@
3
  The specifiers that can be used in a declaration are
4
 
5
  ``` bnf
6
  decl-specifier:
7
  storage-class-specifier
8
- type-specifier
9
  function-specifier
10
  'friend'
11
  'typedef'
12
  'constexpr'
 
13
  ```
14
 
15
  ``` bnf
16
  decl-specifier-seq:
17
  decl-specifier attribute-specifier-seqₒₚₜ
@@ -22,15 +23,20 @@ The optional *attribute-specifier-seq* in a *decl-specifier-seq*
22
  appertains to the type determined by the preceding *decl-specifier*s (
23
  [[dcl.meaning]]). The *attribute-specifier-seq* affects the type only
24
  for the declaration it appears in, not other declarations involving the
25
  same type.
26
 
 
 
 
27
  If a *type-name* is encountered while parsing a *decl-specifier-seq*, it
28
  is interpreted as part of the *decl-specifier-seq* if and only if there
29
- is no previous *type-specifier* other than a *cv-qualifier* in the
30
- *decl-specifier-seq*. The sequence shall be self-consistent as described
31
- below.
 
 
32
 
33
  ``` cpp
34
  typedef char* Pc;
35
  static Pc; // error: name missing
36
  ```
@@ -45,26 +51,35 @@ For another example,
45
  ``` cpp
46
  void f(const Pc); // void f(char* const) (not const char*)
47
  void g(const int Pc); // void g(const int)
48
  ```
49
 
 
 
 
 
50
  Since `signed`, `unsigned`, `long`, and `short` by default imply `int`,
51
  a *type-name* appearing after one of those specifiers is treated as the
52
  name being (re)declared.
53
 
 
 
54
  ``` cpp
55
  void h(unsigned Pc); // void h(unsigned int)
56
  void k(unsigned int Pc); // void k(unsigned int)
57
  ```
58
 
 
 
 
 
59
  ### Storage class specifiers <a id="dcl.stc">[[dcl.stc]]</a>
60
 
61
  The storage class specifiers are
62
 
63
  ``` bnf
64
  storage-class-specifier:
65
- 'register'
66
  'static'
67
  'thread_local'
68
  'extern'
69
  'mutable'
70
  ```
@@ -73,70 +88,68 @@ At most one *storage-class-specifier* shall appear in a given
73
  *decl-specifier-seq*, except that `thread_local` may appear with
74
  `static` or `extern`. If `thread_local` appears in any declaration of a
75
  variable it shall be present in all declarations of that entity. If a
76
  *storage-class-specifier* appears in a *decl-specifier-seq*, there can
77
  be no `typedef` specifier in the same *decl-specifier-seq* and the
78
- *init-declarator-list* of the declaration shall not be empty (except for
79
- an anonymous union declared in a named namespace or in the global
80
- namespace, which shall be declared `static` ([[class.union]])). The
81
- *storage-class-specifier* applies to the name declared by each
82
- *init-declarator* in the list and not to any names declared by other
83
- specifiers. A *storage-class-specifier* shall not be specified in an
84
- explicit specialization ([[temp.expl.spec]]) or an explicit
85
- instantiation ([[temp.explicit]]) directive.
 
86
 
87
- The `register` specifier shall be applied only to names of variables
88
- declared in a block ([[stmt.block]]) or to function parameters (
89
- [[dcl.fct.def]]). It specifies that the named variable has automatic
90
- storage duration ([[basic.stc.auto]]). A variable declared without a
91
- *storage-class-specifier* at block scope or declared as a function
92
- parameter has automatic storage duration by default.
93
-
94
- A `register` specifier is a hint to the implementation that the variable
95
- so declared will be heavily used. The hint can be ignored and in most
96
- implementations it will be ignored if the address of the variable is
97
- taken. This use is deprecated (see  [[depr.register]]).
98
 
99
  The `thread_local` specifier indicates that the named entity has thread
100
  storage duration ([[basic.stc.thread]]). It shall be applied only to
101
  the names of variables of namespace or block scope and to the names of
102
  static data members. When `thread_local` is applied to a variable of
103
  block scope the *storage-class-specifier* `static` is implied if no
104
  other *storage-class-specifier* appears in the *decl-specifier-seq*.
105
 
106
  The `static` specifier can be applied only to names of variables and
107
- functions and to anonymous unions ([[class.union]]). There can be no
108
- `static` function declarations within a block, nor any `static` function
109
- parameters. A `static` specifier used in the declaration of a variable
110
- declares the variable to have static storage duration (
111
  [[basic.stc.static]]), unless accompanied by the `thread_local`
112
  specifier, which declares the variable to have thread storage duration (
113
  [[basic.stc.thread]]). A `static` specifier can be used in declarations
114
  of class members;  [[class.static]] describes its effect. For the
115
  linkage of a name declared with a `static` specifier, see 
116
  [[basic.link]].
117
 
118
  The `extern` specifier can be applied only to the names of variables and
119
  functions. The `extern` specifier cannot be used in the declaration of
120
  class members or function parameters. For the linkage of a name declared
121
- with an `extern` specifier, see  [[basic.link]]. The `extern` keyword
122
- can also be used in s and s, but it is not a in such contexts.
 
 
 
123
 
124
  The linkages implied by successive declarations for a given entity shall
125
  agree. That is, within a given scope, each declaration declaring the
126
  same variable name or the same overloading of a function name shall
127
  imply the same linkage. Each function in a given set of overloaded
128
  functions can have a different linkage, however.
129
 
 
 
130
  ``` cpp
131
  static char* f(); // f() has internal linkage
132
  char* f() // f() still has internal linkage
133
- { /* ... */ }
134
 
135
  char* g(); // g() has external linkage
136
  static char* g() // error: inconsistent linkage
137
- { /* ... */ }
138
 
139
  void h();
140
  inline void h(); // external linkage
141
 
142
  inline void l();
@@ -159,14 +172,18 @@ static int c; // error: inconsistent linkage
159
 
160
  extern int d; // d has external linkage
161
  static int d; // error: inconsistent linkage
162
  ```
163
 
 
 
164
  The name of a declared but undefined class can be used in an `extern`
165
  declaration. Such a declaration can only be used in ways that do not
166
  require a complete class type.
167
 
 
 
168
  ``` cpp
169
  struct S;
170
  extern S a;
171
  extern S f();
172
  extern void g(S);
@@ -175,21 +192,27 @@ void h() {
175
  g(a); // error: S is incomplete
176
  f(); // error: S is incomplete
177
  }
178
  ```
179
 
180
- The `mutable` specifier can be applied only to names of class data
181
- members ([[class.mem]]) and cannot be applied to names declared `const`
182
- or `static`, and cannot be applied to reference members.
 
 
 
 
183
 
184
  ``` cpp
185
  class X {
186
  mutable const int* p; // OK
187
  mutable int* const q; // ill-formed
188
  };
189
  ```
190
 
 
 
191
  The `mutable` specifier on a class data member nullifies a `const`
192
  specifier applied to the containing class object and permits
193
  modification of the mutable class member even though the rest of the
194
  object is `const` ([[dcl.type.cv]]).
195
 
@@ -197,48 +220,14 @@ object is `const` ([[dcl.type.cv]]).
197
 
198
  can be used only in function declarations.
199
 
200
  ``` bnf
201
  function-specifier:
202
- 'inline'
203
  'virtual'
204
  'explicit'
205
  ```
206
 
207
- A function declaration ([[dcl.fct]],  [[class.mfct]], [[class.friend]])
208
- with an `inline` specifier declares an *inline function*. The inline
209
- specifier indicates to the implementation that inline substitution of
210
- the function body at the point of call is to be preferred to the usual
211
- function call mechanism. An implementation is not required to perform
212
- this inline substitution at the point of call; however, even if this
213
- inline substitution is omitted, the other rules for inline functions
214
- defined by  [[dcl.fct.spec]] shall still be respected.
215
-
216
- A function defined within a class definition is an inline function. The
217
- `inline` specifier shall not appear on a block scope function
218
- declaration.[^2] If the `inline` specifier is used in a friend
219
- declaration, that declaration shall be a definition or the function
220
- shall have previously been declared inline.
221
-
222
- An inline function shall be defined in every translation unit in which
223
- it is odr-used and shall have exactly the same definition in every
224
- case ([[basic.def.odr]]). A call to the inline function may be
225
- encountered before its definition appears in the translation unit. If
226
- the definition of a function appears in a translation unit before its
227
- first declaration as inline, the program is ill-formed. If a function
228
- with external linkage is declared inline in one translation unit, it
229
- shall be declared inline in all translation units in which it appears;
230
- no diagnostic is required. An `inline` function with external linkage
231
- shall have the same address in all translation units. A `static` local
232
- variable in an `extern` `inline` function always refers to the same
233
- object. A string literal in the body of an `extern` `inline` function is
234
- the same object in different translation units. A string literal
235
- appearing in a default argument is not in the body of an inline function
236
- merely because the expression is used in a function call from that
237
- inline function. A type defined within the body of an `extern inline`
238
- function is the same type in every translation unit.
239
-
240
  The `virtual` specifier shall be used only in the initial declaration of
241
  a non-static class member function; see  [[class.virtual]].
242
 
243
  The `explicit` specifier shall be used only in the declaration of a
244
  constructor or conversion function within its class definition; see 
@@ -248,14 +237,16 @@ constructor or conversion function within its class definition; see 
248
 
249
  Declarations containing the *decl-specifier* `typedef` declare
250
  identifiers that can be used later for naming fundamental (
251
  [[basic.fundamental]]) or compound ([[basic.compound]]) types. The
252
  `typedef` specifier shall not be combined in a *decl-specifier-seq* with
253
- any other kind of specifier except a *type-specifier,* and it shall not
254
- be used in the *decl-specifier-seq* of a *parameter-declaration* (
255
- [[dcl.fct]]) nor in the *decl-specifier-seq* of a
256
- *function-definition* ([[dcl.fct.def]]).
 
 
257
 
258
  ``` bnf
259
  typedef-name:
260
  identifier
261
  ```
@@ -264,11 +255,15 @@ A name declared with the `typedef` specifier becomes a *typedef-name*.
264
  Within the scope of its declaration, a *typedef-name* is syntactically
265
  equivalent to a keyword and names the type associated with the
266
  identifier in the way described in Clause  [[dcl.decl]]. A
267
  *typedef-name* is thus a synonym for another type. A *typedef-name* does
268
  not introduce a new type the way a class declaration ([[class.name]])
269
- or enum declaration does. after
 
 
 
 
270
 
271
  ``` cpp
272
  typedef int MILES, *KLICKSP;
273
  ```
274
 
@@ -278,88 +273,121 @@ the constructions
278
  MILES distance;
279
  extern KLICKSP metricp;
280
  ```
281
 
282
  are all correct declarations; the type of `distance` is `int` and that
283
- of `metricp` is “pointer to `int`.
 
 
284
 
285
  A *typedef-name* can also be introduced by an *alias-declaration*. The
286
  *identifier* following the `using` keyword becomes a *typedef-name* and
287
  the optional *attribute-specifier-seq* following the *identifier*
288
- appertains to that *typedef-name*. It has the same semantics as if it
289
- were introduced by the `typedef` specifier. In particular, it does not
290
- define a new type and it shall not appear in the *type-id*.
 
 
291
 
292
  ``` cpp
293
  using handler_t = void (*)(int);
294
  extern handler_t ignore;
295
  extern void (*ignore)(int); // redeclare ignore
296
  using cell = pair<void*, cell*>; // ill-formed
297
  ```
298
 
 
 
 
 
 
 
299
  In a given non-class scope, a `typedef` specifier can be used to
300
  redefine the name of any type declared in that scope to refer to the
301
  type to which it already refers.
302
 
 
 
303
  ``` cpp
304
- typedef struct s { /* ... */ } s;
305
  typedef int I;
306
  typedef int I;
307
  typedef I I;
308
  ```
309
 
 
 
310
  In a given class scope, a `typedef` specifier can be used to redefine
311
  any *class-name* declared in that scope that is not also a
312
  *typedef-name* to refer to the type to which it already refers.
313
 
 
 
314
  ``` cpp
315
  struct S {
316
  typedef struct A { } A; // OK
317
  typedef struct B B; // OK
318
  typedef A A; // error
319
  };
320
  ```
321
 
 
 
322
  If a `typedef` specifier is used to redefine in a given scope an entity
323
  that can be referenced using an *elaborated-type-specifier*, the entity
324
  can continue to be referenced by an *elaborated-type-specifier* or as an
325
  enumeration or class name in an enumeration or class definition
326
  respectively.
327
 
 
 
328
  ``` cpp
329
  struct S;
330
  typedef struct S S;
331
  int main() {
332
  struct S* p; // OK
333
  }
334
  struct S { }; // OK
335
  ```
336
 
 
 
337
  In a given scope, a `typedef` specifier shall not be used to redefine
338
  the name of any type declared in that scope to refer to a different
339
  type.
340
 
 
 
341
  ``` cpp
342
- class complex { /* ... */ };
343
  typedef int complex; // error: redefinition
344
  ```
345
 
 
 
346
  Similarly, in a given scope, a class or enumeration shall not be
347
  declared with the same name as a *typedef-name* that is declared in that
348
  scope and refers to a type other than the class or enumeration itself.
349
 
 
 
350
  ``` cpp
351
  typedef int complex;
352
- class complex { /* ... */ }; // error: redefinition
353
  ```
354
 
355
- A *typedef-name* that names a class type, or a cv-qualified version
356
- thereof, is also a *class-name* ([[class.name]]). If a *typedef-name*
357
- is used to identify the subject of an *elaborated-type-specifier* (
358
- [[dcl.type.elab]]), a class definition (Clause  [[class]]), a
359
- constructor declaration ([[class.ctor]]), or a destructor declaration (
360
- [[class.dtor]]), the program is ill-formed.
 
 
 
 
 
361
 
362
  ``` cpp
363
  struct S {
364
  S();
365
  ~S();
@@ -369,35 +397,47 @@ typedef struct S T;
369
 
370
  S a = T(); // OK
371
  struct T * p; // error
372
  ```
373
 
 
 
374
  If the typedef declaration defines an unnamed class (or enum), the first
375
  *typedef-name* declared by the declaration to be that class type (or
376
  enum type) is used to denote the class type (or enum type) for linkage
377
  purposes only ([[basic.link]]).
378
 
 
 
379
  ``` cpp
380
  typedef struct { } *ps, S; // S is the class name for linkage purposes
381
  ```
382
 
 
 
383
  ### The `friend` specifier <a id="dcl.friend">[[dcl.friend]]</a>
384
 
385
  The `friend` specifier is used to specify access to class members; see 
386
  [[class.friend]].
387
 
388
  ### The `constexpr` specifier <a id="dcl.constexpr">[[dcl.constexpr]]</a>
389
 
390
  The `constexpr` specifier shall be applied only to the definition of a
391
- variable or variable template, the declaration of a function or function
392
- template, or the declaration of a static data member of a literal type (
393
- [[basic.types]]). If any declaration of a function, function template,
394
- or variable template has a `constexpr` specifier, then all its
395
- declarations shall contain the `constexpr` specifier. An explicit
396
- specialization can differ from the template declaration with respect to
397
- the `constexpr` specifier. Function parameters cannot be declared
398
- `constexpr`.
 
 
 
 
 
 
399
 
400
  ``` cpp
401
  constexpr void square(int &x); // OK: declaration
402
  constexpr int bufsz = 1024; // OK: definition
403
  constexpr struct pixel { // error: pixel is a type
@@ -419,31 +459,34 @@ int next(constexpr int x) { // error: not for parameters
419
  return x + 1;
420
  }
421
  extern constexpr int memsz; // error: not a definition
422
  ```
423
 
 
 
424
  A `constexpr` specifier used in the declaration of a function that is
425
  not a constructor declares that function to be a *constexpr function*.
426
  Similarly, a `constexpr` specifier used in a constructor declaration
427
- declares that constructor to be a *constexpr constructor*. `constexpr`
428
- functions and `constexpr` constructors are implicitly `inline` (
429
- [[dcl.fct.spec]]).
430
 
431
- The definition of a `constexpr` function shall satisfy the following
432
- constraints:
433
 
434
  - it shall not be virtual ([[class.virtual]]);
435
  - its return type shall be a literal type;
436
  - each of its parameter types shall be a literal type;
437
  - its *function-body* shall be `= delete`, `= default`, or a
438
  *compound-statement* that does not contain
439
  - an *asm-definition*,
440
  - a `goto` statement,
 
441
  - a *try-block*, or
442
  - a definition of a variable of non-literal type or of static or
443
  thread storage duration or for which no initialization is performed.
444
 
 
 
445
  ``` cpp
446
  constexpr int square(int x)
447
  { return x * x; } // OK
448
  constexpr long long_max()
449
  { return 2147483647; } // OK
@@ -467,51 +510,60 @@ constexpr int g(int x, int n) { // OK
467
  while (--n > 0) r *= x;
468
  return r;
469
  }
470
  ```
471
 
472
- The definition of a `constexpr` constructor shall satisfy the following
473
- constraints:
 
 
474
 
475
  - the class shall not have any virtual base classes;
476
  - each of the parameter types shall be a literal type;
477
- - its *function-body* shall not be a *function-try-block*;
478
 
479
  In addition, either its *function-body* shall be `= delete`, or it shall
480
- satisfy the following constraints:
481
 
482
  - either its *function-body* shall be `= default`, or the
483
  *compound-statement* of its *function-body* shall satisfy the
484
- constraints for a *function-body* of a `constexpr` function;
485
- - every non-variant non-static data member and base class sub-object
486
  shall be initialized ([[class.base.init]]);
487
  - if the class is a union having variant members ([[class.union]]),
488
  exactly one of them shall be initialized;
489
  - if the class is a union-like class, but is not a union, for each of
490
  its anonymous union members having variant members, exactly one of
491
  them shall be initialized;
492
  - for a non-delegating constructor, every constructor selected to
493
- initialize non-static data members and base class sub-objects shall be
494
- a `constexpr` constructor;
495
  - for a delegating constructor, the target constructor shall be a
496
- `constexpr` constructor.
 
 
497
 
498
  ``` cpp
499
  struct Length {
500
  constexpr explicit Length(int i = 0) : val(i) { }
501
  private:
502
  int val;
503
  };
504
  ```
505
 
506
- For a non-template, non-defaulted `constexpr` function or a
507
- non-template, non-defaulted, non-inheriting `constexpr` constructor, if
508
- no argument values exist such that an invocation of the function or
509
- constructor could be an evaluated subexpression of a core constant
510
- expression ([[expr.const]]), the program is ill-formed; no diagnostic
 
 
 
511
  required.
512
 
 
 
513
  ``` cpp
514
  constexpr int f(bool b)
515
  { return b ? throw 0 : 0; } // OK
516
  constexpr int f() { return f(true); } // ill-formed, no diagnostic required
517
 
@@ -526,66 +578,113 @@ struct D : B {
526
  constexpr D() : B(global) { } // ill-formed, no diagnostic required
527
  // lvalue-to-rvalue conversion on non-constant global
528
  };
529
  ```
530
 
531
- If the instantiated template specialization of a `constexpr` function
 
 
532
  template or member function of a class template would fail to satisfy
533
- the requirements for a `constexpr` function or `constexpr` constructor,
534
- that specialization is still a `constexpr` function or `constexpr`
535
- constructor, even though a call to such a function cannot appear in a
536
- constant expression. If no specialization of the template would satisfy
537
- the requirements for a `constexpr` function or `constexpr` constructor
538
- when considered as a non-template function or constructor, the template
539
- is ill-formed; no diagnostic required.
540
-
541
- A call to a `constexpr` function produces the same result as a call to
542
- an equivalent non-`constexpr` function in all respects except that a
543
- call to a `constexpr` function can appear in a constant expression.
544
-
545
- The `constexpr` specifier has no effect on the type of a `constexpr`
546
- function or a `constexpr` constructor.
 
 
 
 
 
547
 
548
  ``` cpp
549
  constexpr int bar(int x, int y) // OK
550
  { return x + y + x*y; }
551
  // ...
552
  int bar(int x, int y) // error: redefinition of bar
553
  { return x * 2 + 3 * y; }
554
  ```
555
 
 
 
556
  A `constexpr` specifier used in an object declaration declares the
557
  object as `const`. Such an object shall have literal type and shall be
558
- initialized. If it is initialized by a constructor call, that call shall
559
- be a constant expression ([[expr.const]]). Otherwise, or if a
560
- `constexpr` specifier is used in a reference declaration, every
561
- full-expression that appears in its initializer shall be a constant
562
- expression. Each implicit conversion used in converting the initializer
563
- expressions and each constructor call used for the initialization is
564
- part of such a full-expression.
565
 
566
  ``` cpp
567
  struct pixel {
568
  int x, y;
569
  };
570
  constexpr pixel ur = { 1294, 1024 }; // OK
571
  constexpr pixel origin; // error: initializer missing
572
  ```
573
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
574
  ### Type specifiers <a id="dcl.type">[[dcl.type]]</a>
575
 
576
  The type-specifiers are
577
 
578
  ``` bnf
579
  type-specifier:
580
- trailing-type-specifier
581
- class-specifier
582
- enum-specifier
583
- ```
584
-
585
- ``` bnf
586
- trailing-type-specifier:
587
  simple-type-specifier
588
  elaborated-type-specifier
589
  typename-specifier
590
  cv-qualifier
591
  ```
@@ -595,74 +694,91 @@ type-specifier-seq:
595
  type-specifier attribute-specifier-seqₒₚₜ
596
  type-specifier type-specifier-seq
597
  ```
598
 
599
  ``` bnf
600
- trailing-type-specifier-seq:
601
- trailing-type-specifier attribute-specifier-seqₒₚₜ
602
- trailing-type-specifier trailing-type-specifier-seq
 
 
 
 
 
 
 
603
  ```
604
 
605
  The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
606
- *trailing-type-specifier-seq* appertains to the type denoted by the
607
- preceding *type-specifier*s ([[dcl.meaning]]). The
608
- *attribute-specifier-seq* affects the type only for the declaration it
609
- appears in, not other declarations involving the same type.
 
610
 
611
- As a general rule, at most one *type-specifier* is allowed in the
612
- complete *decl-specifier-seq* of a *declaration* or in a
613
- *type-specifier-seq* or *trailing-type-specifier-seq*. The only
614
- exceptions to this rule are the following:
 
615
 
616
  - `const` can be combined with any type specifier except itself.
617
  - `volatile` can be combined with any type specifier except itself.
618
  - `signed` or `unsigned` can be combined with `char`, `long`, `short`,
619
  or `int`.
620
  - `short` or `long` can be combined with `int`.
621
  - `long` can be combined with `double`.
622
  - `long` can be combined with `long`.
623
 
624
  Except in a declaration of a constructor, destructor, or conversion
625
- function, at least one *type-specifier* that is not a *cv-qualifier*
626
- shall appear in a complete *type-specifier-seq* or a complete
627
- *decl-specifier-seq*.[^3] A *type-specifier-seq* shall not define a
628
- class or enumeration unless it appears in the *type-id* of an
629
- *alias-declaration* ([[dcl.typedef]]) that is not the *declaration* of
630
- a *template-declaration*.
631
 
632
- *enum-specifier*s, *class-specifier*s, and *typename-specifier*s are
633
- discussed in [[dcl.enum]], Clause  [[class]], and [[temp.res]],
634
- respectively. The remaining *type-specifier*s are discussed in the rest
635
- of this section.
636
 
637
- #### The *cv-qualifiers* <a id="dcl.type.cv">[[dcl.type.cv]]</a>
638
 
639
- There are two *cv-qualifiers*, `const` and `volatile`. Each
640
  *cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
641
  *cv-qualifier* appears in a *decl-specifier-seq*, the
642
- *init-declarator-list* of the declaration shall not be empty.
643
- [[basic.type.qualifier]] and [[dcl.fct]] describe how cv-qualifiers
644
- affect object and function types. Redundant cv-qualifications are
645
- ignored. For example, these could be introduced by typedefs.
646
-
647
- Declaring a variable `const` can affect its linkage ([[dcl.stc]]) and
648
- its usability in constant expressions ([[expr.const]]). As described
649
- in  [[dcl.init]], the definition of an object or subobject of
650
- const-qualified type must specify an initializer or be subject to
651
- default-initialization.
 
 
 
 
 
 
652
 
653
  A pointer or reference to a cv-qualified type need not actually point or
654
  refer to a cv-qualified object, but it is treated as if it does; a
655
  const-qualified access path cannot be used to modify an object even if
656
  the object referenced is a non-const object and can be modified through
657
- some other access path. Cv-qualifiers are supported by the type system
658
- so that they cannot be subverted without casting ([[expr.const.cast]]).
 
 
 
659
 
660
  Except that any class member declared `mutable` ([[dcl.stc]]) can be
661
  modified, any attempt to modify a `const` object during its lifetime (
662
  [[basic.life]]) results in undefined behavior.
663
 
 
 
664
  ``` cpp
665
  const int ci = 3; // cv-qualified (initialized as required)
666
  ci = 4; // ill-formed: attempt to modify const
667
 
668
  int i = 2; // not cv-qualified
@@ -677,11 +793,11 @@ ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
677
  const int* ciq = new const int (3); // initialized as required
678
  int* iq = const_cast<int*>(ciq); // cast required
679
  *iq = 4; // undefined: modifies a const object
680
  ```
681
 
682
- For another example
683
 
684
  ``` cpp
685
  struct X {
686
  mutable int i;
687
  int j;
@@ -697,31 +813,35 @@ y.x.j++; // ill-formed: const-qualified member modified
697
  Y* p = const_cast<Y*>(&y); // cast away const-ness of y
698
  p->x.i = 99; // well-formed: mutable member can be modified
699
  p->x.j = 99; // undefined: modifies a const member
700
  ```
701
 
702
- What constitutes an access to an object that has volatile-qualified type
703
- is implementation-defined. If an attempt is made to refer to an object
704
- defined with a volatile-qualified type through the use of a glvalue with
705
- a non-volatile-qualified type, the program behavior is undefined.
706
 
707
- `volatile` is a hint to the implementation to avoid aggressive
708
- optimization involving the object because the value of the object might
709
- be changed by means undetectable by an implementation. Furthermore, for
710
- some implementations, `volatile` might indicate that special hardware
711
- instructions are required to access the object. See  [[intro.execution]]
712
- for detailed semantics. In general, the semantics of `volatile` are
713
- intended to be the same in C++as they are in C.
 
 
 
 
 
 
714
 
715
  #### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
716
 
717
  The simple type specifiers are
718
 
719
  ``` bnf
720
  simple-type-specifier:
721
  nested-name-specifierₒₚₜ type-name
722
  nested-name-specifier 'template' simple-template-id
 
723
  'char'
724
  'char16_t'
725
  'char32_t'
726
  'wchar_t'
727
  'bool'
@@ -749,23 +869,28 @@ type-name:
749
  decltype-specifier:
750
  'decltype' '(' expression ')'
751
  'decltype' '(' 'auto' ')'
752
  ```
753
 
754
- The `auto` specifier is a placeholder for a type to be deduced (
755
- [[dcl.spec.auto]]). The other *simple-type-specifier*s specify either a
 
 
 
 
756
  previously-declared type, a type determined from an expression, or one
757
  of the fundamental types ([[basic.fundamental]]). Table 
758
  [[tab:simple.type.specifiers]] summarizes the valid combinations of
759
  *simple-type-specifier*s and the types they specify.
760
 
761
  **Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
762
 
763
- | | |
764
  | ---------------------- | -------------------------------------- |
765
  | *type-name* | the type named |
766
  | *simple-template-id* | the type as defined in~ [[temp.names]] |
 
767
  | char | ``char'' |
768
  | unsigned char | ``unsigned char'' |
769
  | signed char | ``signed char'' |
770
  | char16_t | ``char16_t'' |
771
  | char32_t | ``char32_t'' |
@@ -797,90 +922,109 @@ of the fundamental types ([[basic.fundamental]]). Table 
797
  | float | ``float'' |
798
  | double | ``double'' |
799
  | long double | ``long double'' |
800
  | void | ``void'' |
801
  | auto | placeholder for a type to be deduced |
 
802
  | decltype(*expression*) | the type as defined below |
803
 
804
 
805
- When multiple *simple-type-specifiers* are allowed, they can be freely
806
- intermixed with other *decl-specifiers* in any order. It is
807
- implementation-defined whether objects of `char` type are represented as
808
- signed or unsigned quantities. The `signed` specifier forces `char`
809
- objects to be signed; it is redundant in other contexts.
 
 
810
 
811
  For an expression `e`, the type denoted by `decltype(e)` is defined as
812
  follows:
813
 
814
- - if `e` is an unparenthesized *id-expression* or an unparenthesized
815
- class member access ([[expr.ref]]), `decltype(e)` is the type of the
816
- entity named by `e`. If there is no such entity, or if `e` names a set
817
- of overloaded functions, the program is ill-formed;
 
 
 
818
  - otherwise, if `e` is an xvalue, `decltype(e)` is `T&&`, where `T` is
819
  the type of `e`;
820
  - otherwise, if `e` is an lvalue, `decltype(e)` is `T&`, where `T` is
821
  the type of `e`;
822
  - otherwise, `decltype(e)` is the type of `e`.
823
 
824
  The operand of the `decltype` specifier is an unevaluated operand
825
  (Clause  [[expr]]).
826
 
 
 
827
  ``` cpp
828
  const int&& foo();
829
  int i;
830
  struct A { double x; };
831
  const A* a = new A();
832
- decltype(foo()) x1 = 0; // type is const int&&
833
  decltype(i) x2; // type is int
834
  decltype(a->x) x3; // type is double
835
  decltype((a->x)) x4 = x3; // type is const double&
836
  ```
837
 
838
- The rules for determining types involving `decltype(auto)` are specified
839
- in  [[dcl.spec.auto]].
840
-
841
- in the case where the operand of a *decltype-specifier* is a function
842
- call and the return type of the function is a class type, a special
843
- rule ([[expr.call]]) ensures that the return type is not required to be
844
- complete (as it would be if the call appeared in a sub-expression or
845
- outside of a *decltype-specifier*). In this context, the common purpose
846
- of writing the expression is merely to refer to its type. In that sense,
847
- a *decltype-specifier* is analogous to a use of a *typedef-name*, so the
848
- usual reasons for requiring a complete type do not apply. In particular,
849
- it is not necessary to allocate storage for a temporary object or to
850
- enforce the semantic constraints associated with invoking the type’s
851
- destructor.
 
 
 
 
 
 
 
 
 
 
852
 
853
  ``` cpp
854
  template<class T> struct A { ~A() = delete; };
855
  template<class T> auto h()
856
  -> A<T>;
857
  template<class T> auto i(T) // identity
858
  -> T;
859
  template<class T> auto f(T) // #1
860
- -> decltype(i(h<T>())); // forces completion of A<T> and implicitly uses
861
- // A<T>::~A() for the temporary introduced by the
862
- // use of h(). (A temporary is not introduced
863
- // as a result of the use of i().)
864
  template<class T> auto f(T) // #2
865
  -> void;
866
  auto g() -> void {
867
- f(42); // OK: calls #2. (#1 is not a viable candidate: type
868
- // deduction fails~([temp.deduct]) because A<int>::~{A()}
869
- // is implicitly used in its decltype-specifier)
870
  }
871
  template<class T> auto q(T)
872
- -> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is
873
- // not implicitly used within the context of this decltype-specifier
874
  void r() {
875
- q(42); // Error: deduction against q succeeds, so overload resolution
876
- // selects the specialization ``q(T) -> decltype((h<T>())) [with T=int]''.
877
  // The return type is A<int>, so a temporary is introduced and its
878
  // destructor is used, so the program is ill-formed.
879
  }
880
  ```
881
 
 
 
882
  #### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
883
 
884
  ``` bnf
885
  elaborated-type-specifier:
886
  class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
@@ -914,20 +1058,26 @@ class whenever it is named.
914
  resolves to a *class-name* or *enum-name*, the
915
  *elaborated-type-specifier* introduces it into the declaration the same
916
  way a *simple-type-specifier* introduces its *type-name*. If the
917
  *identifier* resolves to a *typedef-name* or the *simple-template-id*
918
  resolves to an alias template specialization, the
919
- *elaborated-type-specifier* is ill-formed. This implies that, within a
920
- class template with a template *type-parameter* `T`, the declaration
 
 
 
 
921
 
922
  ``` cpp
923
  friend class T;
924
  ```
925
 
926
  is ill-formed. However, the similar declaration `friend T;` is allowed (
927
  [[class.friend]]).
928
 
 
 
929
  The *class-key* or `enum` keyword present in the
930
  *elaborated-type-specifier* shall agree in kind with the declaration to
931
  which the name in the *elaborated-type-specifier* refers. This rule also
932
  applies to the form of *elaborated-type-specifier* that declares a
933
  *class-name* or `friend` class since it can be construed as referring to
@@ -936,164 +1086,130 @@ the `enum` keyword shall be used to refer to an enumeration (
936
  [[dcl.enum]]), the `union` *class-key* shall be used to refer to a union
937
  (Clause  [[class]]), and either the `class` or `struct` *class-key*
938
  shall be used to refer to a class (Clause  [[class]]) declared using the
939
  `class` or `struct` *class-key*.
940
 
 
 
941
  ``` cpp
942
  enum class E { a, b };
943
  enum E x = E::a; // OK
944
  ```
945
 
946
- #### `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
947
 
948
- The `auto` and `decltype(auto)` *type-specifier*s designate a
949
- placeholder type that will be replaced later, either by deduction from
950
- an initializer or by explicit specification with a
951
- *trailing-return-type*. The `auto` *type-specifier* is also used to
952
- signify that a lambda is a generic lambda.
 
 
 
 
953
 
954
  The placeholder type can appear with a function declarator in the
955
  *decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
956
  *trailing-return-type*, in any context where such a declarator is valid.
957
  If the function declarator includes a *trailing-return-type* (
958
- [[dcl.fct]]), that specifies the declared return type of the function.
959
- If the declared return type of the function contains a placeholder type,
960
- the return type of the function is deduced from `return` statements in
961
- the body of the function, if any.
 
 
962
 
963
  If the `auto` *type-specifier* appears as one of the *decl-specifier*s
964
  in the *decl-specifier-seq* of a *parameter-declaration* of a
965
  *lambda-expression*, the lambda is a *generic lambda* (
966
- [[expr.prim.lambda]]).
 
 
967
 
968
  ``` cpp
969
  auto glambda = [](int i, auto a) { return i; }; // OK: a generic lambda
970
  ```
971
 
 
 
972
  The type of a variable declared using `auto` or `decltype(auto)` is
973
- deduced from its initializer. This use is allowed when declaring
974
- variables in a block ([[stmt.block]]), in namespace scope (
975
- [[basic.scope.namespace]]), and in a  ([[stmt.for]]). `auto` or
976
- `decltype(auto)` shall appear as one of the *decl-specifier*s in the
977
- *decl-specifier-seq* and the *decl-specifier-seq* shall be followed by
978
- one or more *init-declarator*s, each of which shall have a non-empty
979
  *initializer*. In an *initializer* of the form
980
 
981
  ``` cpp
982
  ( expression-list )
983
  ```
984
 
985
  the *expression-list* shall be a single *assignment-expression*.
986
 
 
 
987
  ``` cpp
988
  auto x = 5; // OK: x has type int
989
  const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
990
  static auto y = 0.0; // OK: y has type double
991
  auto int r; // error: auto is not a storage-class-specifier
992
  auto f() -> int; // OK: f returns int
993
  auto g() { return 0.0; } // OK: g returns double
994
  auto h(); // OK: h's return type will be deduced when it is defined
995
  ```
996
 
997
- A placeholder type can also be used in declaring a variable in the of a
998
- selection statement ([[stmt.select]]) or an iteration statement (
999
- [[stmt.iter]]), in the in the or of a  ([[expr.new]]), in a
1000
- *for-range-declaration*, and in declaring a static data member with a
1001
- *brace-or-equal-initializer* that appears within the of a class
1002
- definition ([[class.static.data]]).
1003
 
1004
  A program that uses `auto` or `decltype(auto)` in a context not
1005
  explicitly allowed in this section is ill-formed.
1006
 
1007
- When a variable declared using a placeholder type is initialized, or a
1008
- `return` statement occurs in a function declared with a return type that
1009
- contains a placeholder type, the deduced return type or variable type is
1010
- determined from the type of its initializer. In the case of a `return`
1011
- with no operand, the initializer is considered to be `void()`. Let `T`
1012
- be the declared type of the variable or return type of the function. If
1013
- the placeholder is the `auto` *type-specifier*, the deduced type is
1014
- determined using the rules for template argument deduction. If the
1015
- deduction is for a `return` statement and the initializer is a
1016
- *braced-init-list* ([[dcl.init.list]]), the program is ill-formed.
1017
- Otherwise, obtain `P` from `T` by replacing the occurrences of `auto`
1018
- with either a new invented type template parameter `U` or, if the
1019
- initializer is a *braced-init-list*, with `std::initializer_list<U>`.
1020
- Deduce a value for `U` using the rules of template argument deduction
1021
- from a function call ([[temp.deduct.call]]), where `P` is a function
1022
- template parameter type and the initializer is the corresponding
1023
- argument. If the deduction fails, the declaration is ill-formed.
1024
- Otherwise, the type deduced for the variable or return type is obtained
1025
- by substituting the deduced `U` into `P`.
1026
-
1027
- ``` cpp
1028
- auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
1029
- auto x2 = { 1, 2.0 }; // error: cannot deduce element type
1030
- ```
1031
-
1032
- ``` cpp
1033
- const auto &i = expr;
1034
- ```
1035
-
1036
- The type of `i` is the deduced type of the parameter `u` in the call
1037
- `f(expr)` of the following invented function template:
1038
-
1039
- ``` cpp
1040
- template <class U> void f(const U& u);
1041
- ```
1042
-
1043
- If the placeholder is the `decltype(auto)` *type-specifier*, the
1044
- declared type of the variable or return type of the function shall be
1045
- the placeholder alone. The type deduced for the variable or return type
1046
- is determined as described in  [[dcl.type.simple]], as though the
1047
- initializer had been the operand of the `decltype`.
1048
-
1049
- ``` cpp
1050
- int i;
1051
- int&& f();
1052
- auto x3a = i; // decltype(x3a) is int
1053
- decltype(auto) x3d = i; // decltype(x3d) is int
1054
- auto x4a = (i); // decltype(x4a) is int
1055
- decltype(auto) x4d = (i); // decltype(x4d) is int&
1056
- auto x5a = f(); // decltype(x5a) is int
1057
- decltype(auto) x5d = f(); // decltype(x5d) is int&&
1058
- auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
1059
- decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression
1060
- auto *x7a = &i; // decltype(x7a) is int*
1061
- decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)
1062
- ```
1063
-
1064
  If the *init-declarator-list* contains more than one *init-declarator*,
1065
  they shall all form declarations of variables. The type of each declared
1066
- variable is determined as described above, and if the type that replaces
1067
- the placeholder type is not the same in each deduction, the program is
1068
- ill-formed.
 
 
1069
 
1070
  ``` cpp
1071
  auto x = 5, *y = &x; // OK: auto is int
1072
  auto a = 5, b = { 1, 2 }; // error: different types for auto
1073
  ```
1074
 
 
 
1075
  If a function with a declared return type that contains a placeholder
1076
- type has multiple `return` statements, the return type is deduced for
1077
- each `return` statement. If the type deduced is not the same in each
1078
- deduction, the program is ill-formed.
1079
 
1080
  If a function with a declared return type that uses a placeholder type
1081
- has no `return` statements, the return type is deduced as though from a
1082
- `return` statement with no operand at the closing brace of the function
1083
- body.
 
 
1084
 
1085
  ``` cpp
1086
  auto f() { } // OK, return type is void
1087
  auto* g() { } // error, cannot deduce auto* from void()
1088
  ```
1089
 
 
 
1090
  If the type of an entity with an undeduced placeholder type is needed to
1091
  determine the type of an expression, the program is ill-formed. Once a
1092
- `return` statement has been seen in a function, however, the return type
1093
- deduced from that statement can be used in the rest of the function,
1094
- including in other `return` statements.
 
 
1095
 
1096
  ``` cpp
1097
  auto n = n; // error, n's type is unknown
1098
  auto f();
1099
  void g() { &f; } // error, f's return type is unknown
@@ -1103,30 +1219,41 @@ auto sum(int i) {
1103
  else
1104
  return sum(i-1)+i; // OK, sum's return type has been deduced
1105
  }
1106
  ```
1107
 
 
 
1108
  Return type deduction for a function template with a placeholder in its
1109
  declared type occurs when the definition is instantiated even if the
1110
  function body contains a `return` statement with a non-type-dependent
1111
- operand. Therefore, any use of a specialization of the function template
1112
- will cause an implicit instantiation. Any errors that arise from this
1113
- instantiation are not in the immediate context of the function type and
1114
- can result in the program being ill-formed.
 
 
 
 
 
1115
 
1116
  ``` cpp
1117
  template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
1118
  typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
1119
  template<class T> auto f(T* t) { return *t; }
1120
  void g() { int (*p)(int*) = &f; } // instantiates both fs to determine return types,
1121
  // chooses second
1122
  ```
1123
 
 
 
1124
  Redeclarations or specializations of a function or function template
1125
  with a declared return type that uses a placeholder type shall also use
1126
  that placeholder, not a deduced type.
1127
 
 
 
1128
  ``` cpp
1129
  auto f();
1130
  auto f() { return 42; } // return type is int
1131
  auto f(); // OK
1132
  int f(); // error, cannot be overloaded with auto f()
@@ -1147,20 +1274,155 @@ template <typename T> struct A {
1147
  friend T frf(T);
1148
  };
1149
  auto frf(int i) { return i; } // not a friend of A<int>
1150
  ```
1151
 
 
 
1152
  A function declared with a return type that uses a placeholder type
1153
  shall not be `virtual` ([[class.virtual]]).
1154
 
1155
  An explicit instantiation declaration ([[temp.explicit]]) does not
1156
  cause the instantiation of an entity declared using a placeholder type,
1157
  but it also does not prevent that entity from being instantiated as
1158
  needed to determine its type.
1159
 
 
 
1160
  ``` cpp
1161
  template <typename T> auto f(T t) { return t; }
1162
  extern template auto f(int); // does not instantiate f<int>
1163
  int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
1164
  // instantiation definition is still required somewhere in the program
1165
  ```
1166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  The specifiers that can be used in a declaration are
4
 
5
  ``` bnf
6
  decl-specifier:
7
  storage-class-specifier
8
+ defining-type-specifier
9
  function-specifier
10
  'friend'
11
  'typedef'
12
  'constexpr'
13
+ 'inline'
14
  ```
15
 
16
  ``` bnf
17
  decl-specifier-seq:
18
  decl-specifier attribute-specifier-seqₒₚₜ
 
23
  appertains to the type determined by the preceding *decl-specifier*s (
24
  [[dcl.meaning]]). The *attribute-specifier-seq* affects the type only
25
  for the declaration it appears in, not other declarations involving the
26
  same type.
27
 
28
+ Each *decl-specifier* shall appear at most once in a complete
29
+ *decl-specifier-seq*, except that `long` may appear twice.
30
+
31
  If a *type-name* is encountered while parsing a *decl-specifier-seq*, it
32
  is interpreted as part of the *decl-specifier-seq* if and only if there
33
+ is no previous *defining-type-specifier* other than a *cv-qualifier* in
34
+ the *decl-specifier-seq*. The sequence shall be self-consistent as
35
+ described below.
36
+
37
+ [*Example 1*:
38
 
39
  ``` cpp
40
  typedef char* Pc;
41
  static Pc; // error: name missing
42
  ```
 
51
  ``` cpp
52
  void f(const Pc); // void f(char* const) (not const char*)
53
  void g(const int Pc); // void g(const int)
54
  ```
55
 
56
+ — *end example*]
57
+
58
+ [*Note 1*:
59
+
60
  Since `signed`, `unsigned`, `long`, and `short` by default imply `int`,
61
  a *type-name* appearing after one of those specifiers is treated as the
62
  name being (re)declared.
63
 
64
+ [*Example 2*:
65
+
66
  ``` cpp
67
  void h(unsigned Pc); // void h(unsigned int)
68
  void k(unsigned int Pc); // void k(unsigned int)
69
  ```
70
 
71
+ — *end example*]
72
+
73
+ — *end note*]
74
+
75
  ### Storage class specifiers <a id="dcl.stc">[[dcl.stc]]</a>
76
 
77
  The storage class specifiers are
78
 
79
  ``` bnf
80
  storage-class-specifier:
 
81
  'static'
82
  'thread_local'
83
  'extern'
84
  'mutable'
85
  ```
 
88
  *decl-specifier-seq*, except that `thread_local` may appear with
89
  `static` or `extern`. If `thread_local` appears in any declaration of a
90
  variable it shall be present in all declarations of that entity. If a
91
  *storage-class-specifier* appears in a *decl-specifier-seq*, there can
92
  be no `typedef` specifier in the same *decl-specifier-seq* and the
93
+ *init-declarator-list* or *member-declarator-list* of the declaration
94
+ shall not be empty (except for an anonymous union declared in a named
95
+ namespace or in the global namespace, which shall be declared `static` (
96
+ [[class.union.anon]])). The *storage-class-specifier* applies to the
97
+ name declared by each *init-declarator* in the list and not to any names
98
+ declared by other specifiers. A *storage-class-specifier* other than
99
+ `thread_local` shall not be specified in an explicit specialization (
100
+ [[temp.expl.spec]]) or an explicit instantiation ([[temp.explicit]])
101
+ directive.
102
 
103
+ [*Note 1*: A variable declared without a *storage-class-specifier* at
104
+ block scope or declared as a function parameter has automatic storage
105
+ duration by default ([[basic.stc.auto]]). *end note*]
 
 
 
 
 
 
 
 
106
 
107
  The `thread_local` specifier indicates that the named entity has thread
108
  storage duration ([[basic.stc.thread]]). It shall be applied only to
109
  the names of variables of namespace or block scope and to the names of
110
  static data members. When `thread_local` is applied to a variable of
111
  block scope the *storage-class-specifier* `static` is implied if no
112
  other *storage-class-specifier* appears in the *decl-specifier-seq*.
113
 
114
  The `static` specifier can be applied only to names of variables and
115
+ functions and to anonymous unions ([[class.union.anon]]). There can be
116
+ no `static` function declarations within a block, nor any `static`
117
+ function parameters. A `static` specifier used in the declaration of a
118
+ variable declares the variable to have static storage duration (
119
  [[basic.stc.static]]), unless accompanied by the `thread_local`
120
  specifier, which declares the variable to have thread storage duration (
121
  [[basic.stc.thread]]). A `static` specifier can be used in declarations
122
  of class members;  [[class.static]] describes its effect. For the
123
  linkage of a name declared with a `static` specifier, see 
124
  [[basic.link]].
125
 
126
  The `extern` specifier can be applied only to the names of variables and
127
  functions. The `extern` specifier cannot be used in the declaration of
128
  class members or function parameters. For the linkage of a name declared
129
+ with an `extern` specifier, see  [[basic.link]].
130
+
131
+ [*Note 2*: The `extern` keyword can also be used in
132
+ *explicit-instantiation*s and *linkage-specification*s, but it is not a
133
+ *storage-class-specifier* in such contexts. — *end note*]
134
 
135
  The linkages implied by successive declarations for a given entity shall
136
  agree. That is, within a given scope, each declaration declaring the
137
  same variable name or the same overloading of a function name shall
138
  imply the same linkage. Each function in a given set of overloaded
139
  functions can have a different linkage, however.
140
 
141
+ [*Example 1*:
142
+
143
  ``` cpp
144
  static char* f(); // f() has internal linkage
145
  char* f() // f() still has internal linkage
146
+ { ... }
147
 
148
  char* g(); // g() has external linkage
149
  static char* g() // error: inconsistent linkage
150
+ { ... }
151
 
152
  void h();
153
  inline void h(); // external linkage
154
 
155
  inline void l();
 
172
 
173
  extern int d; // d has external linkage
174
  static int d; // error: inconsistent linkage
175
  ```
176
 
177
+ — *end example*]
178
+
179
  The name of a declared but undefined class can be used in an `extern`
180
  declaration. Such a declaration can only be used in ways that do not
181
  require a complete class type.
182
 
183
+ [*Example 2*:
184
+
185
  ``` cpp
186
  struct S;
187
  extern S a;
188
  extern S f();
189
  extern void g(S);
 
192
  g(a); // error: S is incomplete
193
  f(); // error: S is incomplete
194
  }
195
  ```
196
 
197
+ *end example*]
198
+
199
+ The `mutable` specifier shall appear only in the declaration of a
200
+ non-static data member ([[class.mem]]) whose type is neither
201
+ const-qualified nor a reference type.
202
+
203
+ [*Example 3*:
204
 
205
  ``` cpp
206
  class X {
207
  mutable const int* p; // OK
208
  mutable int* const q; // ill-formed
209
  };
210
  ```
211
 
212
+ — *end example*]
213
+
214
  The `mutable` specifier on a class data member nullifies a `const`
215
  specifier applied to the containing class object and permits
216
  modification of the mutable class member even though the rest of the
217
  object is `const` ([[dcl.type.cv]]).
218
 
 
220
 
221
  can be used only in function declarations.
222
 
223
  ``` bnf
224
  function-specifier:
 
225
  'virtual'
226
  'explicit'
227
  ```
228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  The `virtual` specifier shall be used only in the initial declaration of
230
  a non-static class member function; see  [[class.virtual]].
231
 
232
  The `explicit` specifier shall be used only in the declaration of a
233
  constructor or conversion function within its class definition; see 
 
237
 
238
  Declarations containing the *decl-specifier* `typedef` declare
239
  identifiers that can be used later for naming fundamental (
240
  [[basic.fundamental]]) or compound ([[basic.compound]]) types. The
241
  `typedef` specifier shall not be combined in a *decl-specifier-seq* with
242
+ any other kind of specifier except a *defining-type-specifier*, and it
243
+ shall not be used in the *decl-specifier-seq* of a
244
+ *parameter-declaration* ([[dcl.fct]]) nor in the *decl-specifier-seq*
245
+ of a *function-definition* ([[dcl.fct.def]]). If a `typedef` specifier
246
+ appears in a declaration without a *declarator*, the program is
247
+ ill-formed.
248
 
249
  ``` bnf
250
  typedef-name:
251
  identifier
252
  ```
 
255
  Within the scope of its declaration, a *typedef-name* is syntactically
256
  equivalent to a keyword and names the type associated with the
257
  identifier in the way described in Clause  [[dcl.decl]]. A
258
  *typedef-name* is thus a synonym for another type. A *typedef-name* does
259
  not introduce a new type the way a class declaration ([[class.name]])
260
+ or enum declaration does.
261
+
262
+ [*Example 1*:
263
+
264
+ After
265
 
266
  ``` cpp
267
  typedef int MILES, *KLICKSP;
268
  ```
269
 
 
273
  MILES distance;
274
  extern KLICKSP metricp;
275
  ```
276
 
277
  are all correct declarations; the type of `distance` is `int` and that
278
+ of `metricp` is “pointer to `int`”.
279
+
280
+ — *end example*]
281
 
282
  A *typedef-name* can also be introduced by an *alias-declaration*. The
283
  *identifier* following the `using` keyword becomes a *typedef-name* and
284
  the optional *attribute-specifier-seq* following the *identifier*
285
+ appertains to that *typedef-name*. Such a *typedef-name* has the same
286
+ semantics as if it were introduced by the `typedef` specifier. In
287
+ particular, it does not define a new type.
288
+
289
+ [*Example 2*:
290
 
291
  ``` cpp
292
  using handler_t = void (*)(int);
293
  extern handler_t ignore;
294
  extern void (*ignore)(int); // redeclare ignore
295
  using cell = pair<void*, cell*>; // ill-formed
296
  ```
297
 
298
+ — *end example*]
299
+
300
+ The *defining-type-specifier-seq* of the *defining-type-id* shall not
301
+ define a class or enumeration if the *alias-declaration* is the
302
+ *declaration* of a *template-declaration*.
303
+
304
  In a given non-class scope, a `typedef` specifier can be used to
305
  redefine the name of any type declared in that scope to refer to the
306
  type to which it already refers.
307
 
308
+ [*Example 3*:
309
+
310
  ``` cpp
311
+ typedef struct s { ... } s;
312
  typedef int I;
313
  typedef int I;
314
  typedef I I;
315
  ```
316
 
317
+ — *end example*]
318
+
319
  In a given class scope, a `typedef` specifier can be used to redefine
320
  any *class-name* declared in that scope that is not also a
321
  *typedef-name* to refer to the type to which it already refers.
322
 
323
+ [*Example 4*:
324
+
325
  ``` cpp
326
  struct S {
327
  typedef struct A { } A; // OK
328
  typedef struct B B; // OK
329
  typedef A A; // error
330
  };
331
  ```
332
 
333
+ — *end example*]
334
+
335
  If a `typedef` specifier is used to redefine in a given scope an entity
336
  that can be referenced using an *elaborated-type-specifier*, the entity
337
  can continue to be referenced by an *elaborated-type-specifier* or as an
338
  enumeration or class name in an enumeration or class definition
339
  respectively.
340
 
341
+ [*Example 5*:
342
+
343
  ``` cpp
344
  struct S;
345
  typedef struct S S;
346
  int main() {
347
  struct S* p; // OK
348
  }
349
  struct S { }; // OK
350
  ```
351
 
352
+ — *end example*]
353
+
354
  In a given scope, a `typedef` specifier shall not be used to redefine
355
  the name of any type declared in that scope to refer to a different
356
  type.
357
 
358
+ [*Example 6*:
359
+
360
  ``` cpp
361
+ class complex { ... };
362
  typedef int complex; // error: redefinition
363
  ```
364
 
365
+ — *end example*]
366
+
367
  Similarly, in a given scope, a class or enumeration shall not be
368
  declared with the same name as a *typedef-name* that is declared in that
369
  scope and refers to a type other than the class or enumeration itself.
370
 
371
+ [*Example 7*:
372
+
373
  ``` cpp
374
  typedef int complex;
375
+ class complex { ... }; // error: redefinition
376
  ```
377
 
378
+ *end example*]
379
+
380
+ [*Note 1*: A *typedef-name* that names a class type, or a cv-qualified
381
+ version thereof, is also a *class-name* ([[class.name]]). If a
382
+ *typedef-name* is used to identify the subject of an
383
+ *elaborated-type-specifier* ([[dcl.type.elab]]), a class definition
384
+ (Clause  [[class]]), a constructor declaration ([[class.ctor]]), or a
385
+ destructor declaration ([[class.dtor]]), the program is
386
+ ill-formed. — *end note*]
387
+
388
+ [*Example 8*:
389
 
390
  ``` cpp
391
  struct S {
392
  S();
393
  ~S();
 
397
 
398
  S a = T(); // OK
399
  struct T * p; // error
400
  ```
401
 
402
+ — *end example*]
403
+
404
  If the typedef declaration defines an unnamed class (or enum), the first
405
  *typedef-name* declared by the declaration to be that class type (or
406
  enum type) is used to denote the class type (or enum type) for linkage
407
  purposes only ([[basic.link]]).
408
 
409
+ [*Example 9*:
410
+
411
  ``` cpp
412
  typedef struct { } *ps, S; // S is the class name for linkage purposes
413
  ```
414
 
415
+ — *end example*]
416
+
417
  ### The `friend` specifier <a id="dcl.friend">[[dcl.friend]]</a>
418
 
419
  The `friend` specifier is used to specify access to class members; see 
420
  [[class.friend]].
421
 
422
  ### The `constexpr` specifier <a id="dcl.constexpr">[[dcl.constexpr]]</a>
423
 
424
  The `constexpr` specifier shall be applied only to the definition of a
425
+ variable or variable template or the declaration of a function or
426
+ function template. A function or static data member declared with the
427
+ `constexpr` specifier is implicitly an inline function or variable (
428
+ [[dcl.inline]]). If any declaration of a function or function template
429
+ has a `constexpr` specifier, then all its declarations shall contain the
430
+ `constexpr` specifier.
431
+
432
+ [*Note 1*: An explicit specialization can differ from the template
433
+ declaration with respect to the `constexpr` specifier. — *end note*]
434
+
435
+ [*Note 2*: Function parameters cannot be declared
436
+ `constexpr`. — *end note*]
437
+
438
+ [*Example 1*:
439
 
440
  ``` cpp
441
  constexpr void square(int &x); // OK: declaration
442
  constexpr int bufsz = 1024; // OK: definition
443
  constexpr struct pixel { // error: pixel is a type
 
459
  return x + 1;
460
  }
461
  extern constexpr int memsz; // error: not a definition
462
  ```
463
 
464
+ — *end example*]
465
+
466
  A `constexpr` specifier used in the declaration of a function that is
467
  not a constructor declares that function to be a *constexpr function*.
468
  Similarly, a `constexpr` specifier used in a constructor declaration
469
+ declares that constructor to be a *constexpr constructor*.
 
 
470
 
471
+ The definition of a constexpr function shall satisfy the following
472
+ requirements:
473
 
474
  - it shall not be virtual ([[class.virtual]]);
475
  - its return type shall be a literal type;
476
  - each of its parameter types shall be a literal type;
477
  - its *function-body* shall be `= delete`, `= default`, or a
478
  *compound-statement* that does not contain
479
  - an *asm-definition*,
480
  - a `goto` statement,
481
+ - an identifier label ([[stmt.label]]),
482
  - a *try-block*, or
483
  - a definition of a variable of non-literal type or of static or
484
  thread storage duration or for which no initialization is performed.
485
 
486
+ [*Example 2*:
487
+
488
  ``` cpp
489
  constexpr int square(int x)
490
  { return x * x; } // OK
491
  constexpr long long_max()
492
  { return 2147483647; } // OK
 
510
  while (--n > 0) r *= x;
511
  return r;
512
  }
513
  ```
514
 
515
+ *end example*]
516
+
517
+ The definition of a constexpr constructor shall satisfy the following
518
+ requirements:
519
 
520
  - the class shall not have any virtual base classes;
521
  - each of the parameter types shall be a literal type;
522
+ - its *function-body* shall not be a *function-try-block*.
523
 
524
  In addition, either its *function-body* shall be `= delete`, or it shall
525
+ satisfy the following requirements:
526
 
527
  - either its *function-body* shall be `= default`, or the
528
  *compound-statement* of its *function-body* shall satisfy the
529
+ requirements for a *function-body* of a constexpr function;
530
+ - every non-variant non-static data member and base class subobject
531
  shall be initialized ([[class.base.init]]);
532
  - if the class is a union having variant members ([[class.union]]),
533
  exactly one of them shall be initialized;
534
  - if the class is a union-like class, but is not a union, for each of
535
  its anonymous union members having variant members, exactly one of
536
  them shall be initialized;
537
  - for a non-delegating constructor, every constructor selected to
538
+ initialize non-static data members and base class subobjects shall be
539
+ a constexpr constructor;
540
  - for a delegating constructor, the target constructor shall be a
541
+ constexpr constructor.
542
+
543
+ [*Example 3*:
544
 
545
  ``` cpp
546
  struct Length {
547
  constexpr explicit Length(int i = 0) : val(i) { }
548
  private:
549
  int val;
550
  };
551
  ```
552
 
553
+ *end example*]
554
+
555
+ For a constexpr function or constexpr constructor that is neither
556
+ defaulted nor a template, if no argument values exist such that an
557
+ invocation of the function or constructor could be an evaluated
558
+ subexpression of a core constant expression ([[expr.const]]), or, for a
559
+ constructor, a constant initializer for some object (
560
+ [[basic.start.static]]), the program is ill-formed, no diagnostic
561
  required.
562
 
563
+ [*Example 4*:
564
+
565
  ``` cpp
566
  constexpr int f(bool b)
567
  { return b ? throw 0 : 0; } // OK
568
  constexpr int f() { return f(true); } // ill-formed, no diagnostic required
569
 
 
578
  constexpr D() : B(global) { } // ill-formed, no diagnostic required
579
  // lvalue-to-rvalue conversion on non-constant global
580
  };
581
  ```
582
 
583
+ *end example*]
584
+
585
+ If the instantiated template specialization of a constexpr function
586
  template or member function of a class template would fail to satisfy
587
+ the requirements for a constexpr function or constexpr constructor, that
588
+ specialization is still a constexpr function or constexpr constructor,
589
+ even though a call to such a function cannot appear in a constant
590
+ expression. If no specialization of the template would satisfy the
591
+ requirements for a constexpr function or constexpr constructor when
592
+ considered as a non-template function or constructor, the template is
593
+ ill-formed, no diagnostic required.
594
+
595
+ A call to a constexpr function produces the same result as a call to an
596
+ equivalent non-constexpr function in all respects except that
597
+
598
+ - a call to a constexpr function can appear in a constant expression (
599
+ [[expr.const]]) and
600
+ - copy elision is mandatory in a constant expression ([[class.copy]]).
601
+
602
+ The `constexpr` specifier has no effect on the type of a constexpr
603
+ function or a constexpr constructor.
604
+
605
+ [*Example 5*:
606
 
607
  ``` cpp
608
  constexpr int bar(int x, int y) // OK
609
  { return x + y + x*y; }
610
  // ...
611
  int bar(int x, int y) // error: redefinition of bar
612
  { return x * 2 + 3 * y; }
613
  ```
614
 
615
+ — *end example*]
616
+
617
  A `constexpr` specifier used in an object declaration declares the
618
  object as `const`. Such an object shall have literal type and shall be
619
+ initialized. In any `constexpr` variable declaration, the
620
+ full-expression of the initialization shall be a constant expression (
621
+ [[expr.const]]).
622
+
623
+ [*Example 6*:
 
 
624
 
625
  ``` cpp
626
  struct pixel {
627
  int x, y;
628
  };
629
  constexpr pixel ur = { 1294, 1024 }; // OK
630
  constexpr pixel origin; // error: initializer missing
631
  ```
632
 
633
+ — *end example*]
634
+
635
+ ### The `inline` specifier <a id="dcl.inline">[[dcl.inline]]</a>
636
+
637
+ The `inline` specifier can be applied only to the declaration or
638
+ definition of a variable or function.
639
+
640
+ A function declaration ([[dcl.fct]],  [[class.mfct]], [[class.friend]])
641
+ with an `inline` specifier declares an *inline function*. The inline
642
+ specifier indicates to the implementation that inline substitution of
643
+ the function body at the point of call is to be preferred to the usual
644
+ function call mechanism. An implementation is not required to perform
645
+ this inline substitution at the point of call; however, even if this
646
+ inline substitution is omitted, the other rules for inline functions
647
+ specified in this section shall still be respected.
648
+
649
+ A variable declaration with an `inline` specifier declares an *inline
650
+ variable*.
651
+
652
+ A function defined within a class definition is an inline function.
653
+
654
+ The `inline` specifier shall not appear on a block scope
655
+ declaration.[^2] If the `inline` specifier is used in a friend function
656
+ declaration, that declaration shall be a definition or the function
657
+ shall have previously been declared inline.
658
+
659
+ An inline function or variable shall be defined in every translation
660
+ unit in which it is odr-used and shall have exactly the same definition
661
+ in every case ([[basic.def.odr]]).
662
+
663
+ [*Note 1*: A call to the inline function or a use of the inline
664
+ variable may be encountered before its definition appears in the
665
+ translation unit. — *end note*]
666
+
667
+ If the definition of a function or variable appears in a translation
668
+ unit before its first declaration as inline, the program is ill-formed.
669
+ If a function or variable with external linkage is declared inline in
670
+ one translation unit, it shall be declared inline in all translation
671
+ units in which it appears; no diagnostic is required. An inline function
672
+ or variable with external linkage shall have the same address in all
673
+ translation units.
674
+
675
+ [*Note 2*: A `static` local variable in an inline function with
676
+ external linkage always refers to the same object. A type defined within
677
+ the body of an inline function with external linkage is the same type in
678
+ every translation unit. — *end note*]
679
+
680
  ### Type specifiers <a id="dcl.type">[[dcl.type]]</a>
681
 
682
  The type-specifiers are
683
 
684
  ``` bnf
685
  type-specifier:
 
 
 
 
 
 
 
686
  simple-type-specifier
687
  elaborated-type-specifier
688
  typename-specifier
689
  cv-qualifier
690
  ```
 
694
  type-specifier attribute-specifier-seqₒₚₜ
695
  type-specifier type-specifier-seq
696
  ```
697
 
698
  ``` bnf
699
+ defining-type-specifier:
700
+ type-specifier
701
+ class-specifier
702
+ enum-specifier
703
+ ```
704
+
705
+ ``` bnf
706
+ defining-type-specifier-seq:
707
+ defining-type-specifier attribute-specifier-seqₒₚₜ
708
+ defining-type-specifier defining-type-specifier-seq
709
  ```
710
 
711
  The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
712
+ *defining-type-specifier-seq* appertains to the type denoted by the
713
+ preceding *type-specifier*s or *defining-type-specifier*s (
714
+ [[dcl.meaning]]). The *attribute-specifier-seq* affects the type only
715
+ for the declaration it appears in, not other declarations involving the
716
+ same type.
717
 
718
+ As a general rule, at most one *defining-type-specifier* is allowed in
719
+ the complete *decl-specifier-seq* of a *declaration* or in a
720
+ *defining-type-specifier-seq*, and at most one *type-specifier* is
721
+ allowed in a *type-specifier-seq*. The only exceptions to this rule are
722
+ the following:
723
 
724
  - `const` can be combined with any type specifier except itself.
725
  - `volatile` can be combined with any type specifier except itself.
726
  - `signed` or `unsigned` can be combined with `char`, `long`, `short`,
727
  or `int`.
728
  - `short` or `long` can be combined with `int`.
729
  - `long` can be combined with `double`.
730
  - `long` can be combined with `long`.
731
 
732
  Except in a declaration of a constructor, destructor, or conversion
733
+ function, at least one *defining-type-specifier* that is not a
734
+ *cv-qualifier* shall appear in a complete *type-specifier-seq* or a
735
+ complete *decl-specifier-seq*.[^3]
 
 
 
736
 
737
+ [*Note 1*: *enum-specifier*s, *class-specifier*s, and
738
+ *typename-specifier*s are discussed in [[dcl.enum]], Clause  [[class]],
739
+ and [[temp.res]], respectively. The remaining *type-specifier*s are
740
+ discussed in the rest of this section. — *end note*]
741
 
742
+ #### The *cv-qualifier*s <a id="dcl.type.cv">[[dcl.type.cv]]</a>
743
 
744
+ There are two *cv-qualifier*s, `const` and `volatile`. Each
745
  *cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
746
  *cv-qualifier* appears in a *decl-specifier-seq*, the
747
+ *init-declarator-list* or *member-declarator-list* of the declaration
748
+ shall not be empty.
749
+
750
+ [*Note 1*: [[basic.type.qualifier]] and [[dcl.fct]] describe how
751
+ cv-qualifiers affect object and function types. — *end note*]
752
+
753
+ Redundant cv-qualifications are ignored.
754
+
755
+ [*Note 2*: For example, these could be introduced by
756
+ typedefs. — *end note*]
757
+
758
+ [*Note 3*: Declaring a variable `const` can affect its linkage (
759
+ [[dcl.stc]]) and its usability in constant expressions (
760
+ [[expr.const]]). As described in  [[dcl.init]], the definition of an
761
+ object or subobject of const-qualified type must specify an initializer
762
+ or be subject to default-initialization. — *end note*]
763
 
764
  A pointer or reference to a cv-qualified type need not actually point or
765
  refer to a cv-qualified object, but it is treated as if it does; a
766
  const-qualified access path cannot be used to modify an object even if
767
  the object referenced is a non-const object and can be modified through
768
+ some other access path.
769
+
770
+ [*Note 4*: Cv-qualifiers are supported by the type system so that they
771
+ cannot be subverted without casting (
772
+ [[expr.const.cast]]). — *end note*]
773
 
774
  Except that any class member declared `mutable` ([[dcl.stc]]) can be
775
  modified, any attempt to modify a `const` object during its lifetime (
776
  [[basic.life]]) results in undefined behavior.
777
 
778
+ [*Example 1*:
779
+
780
  ``` cpp
781
  const int ci = 3; // cv-qualified (initialized as required)
782
  ci = 4; // ill-formed: attempt to modify const
783
 
784
  int i = 2; // not cv-qualified
 
793
  const int* ciq = new const int (3); // initialized as required
794
  int* iq = const_cast<int*>(ciq); // cast required
795
  *iq = 4; // undefined: modifies a const object
796
  ```
797
 
798
+ For another example,
799
 
800
  ``` cpp
801
  struct X {
802
  mutable int i;
803
  int j;
 
813
  Y* p = const_cast<Y*>(&y); // cast away const-ness of y
814
  p->x.i = 99; // well-formed: mutable member can be modified
815
  p->x.j = 99; // undefined: modifies a const member
816
  ```
817
 
818
+ *end example*]
 
 
 
819
 
820
+ The semantics of an access through a volatile glvalue are
821
+ *implementation-defined*. If an attempt is made to access an object
822
+ defined with a volatile-qualified type through the use of a non-volatile
823
+ glvalue, the behavior is undefined.
824
+
825
+ [*Note 5*: `volatile` is a hint to the implementation to avoid
826
+ aggressive optimization involving the object because the value of the
827
+ object might be changed by means undetectable by an implementation.
828
+ Furthermore, for some implementations, `volatile` might indicate that
829
+ special hardware instructions are required to access the object. See 
830
+ [[intro.execution]] for detailed semantics. In general, the semantics of
831
+ `volatile` are intended to be the same in C++as they are in
832
+ C. — *end note*]
833
 
834
  #### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
835
 
836
  The simple type specifiers are
837
 
838
  ``` bnf
839
  simple-type-specifier:
840
  nested-name-specifierₒₚₜ type-name
841
  nested-name-specifier 'template' simple-template-id
842
+ nested-name-specifierₒₚₜ template-name
843
  'char'
844
  'char16_t'
845
  'char32_t'
846
  'wchar_t'
847
  'bool'
 
869
  decltype-specifier:
870
  'decltype' '(' expression ')'
871
  'decltype' '(' 'auto' ')'
872
  ```
873
 
874
+ The *simple-type-specifier* `auto` is a placeholder for a type to be
875
+ deduced ([[dcl.spec.auto]]). A *type-specifier* of the form
876
+ `typename`ₒₚₜ *nested-name-specifier*ₒₚₜ *template-name* is a
877
+ placeholder for a deduced class type ([[dcl.type.class.deduct]]). The
878
+ *template-name* shall name a class template that is not an
879
+ injected-class-name. The other *simple-type-specifier*s specify either a
880
  previously-declared type, a type determined from an expression, or one
881
  of the fundamental types ([[basic.fundamental]]). Table 
882
  [[tab:simple.type.specifiers]] summarizes the valid combinations of
883
  *simple-type-specifier*s and the types they specify.
884
 
885
  **Table: *simple-type-specifier*{s} and the types they specify** <a id="tab:simple.type.specifiers">[tab:simple.type.specifiers]</a>
886
 
887
+ | Specifier(s) | Type |
888
  | ---------------------- | -------------------------------------- |
889
  | *type-name* | the type named |
890
  | *simple-template-id* | the type as defined in~ [[temp.names]] |
891
+ | *template-name* | placeholder for a type to be deduced |
892
  | char | ``char'' |
893
  | unsigned char | ``unsigned char'' |
894
  | signed char | ``signed char'' |
895
  | char16_t | ``char16_t'' |
896
  | char32_t | ``char32_t'' |
 
922
  | float | ``float'' |
923
  | double | ``double'' |
924
  | long double | ``long double'' |
925
  | void | ``void'' |
926
  | auto | placeholder for a type to be deduced |
927
+ | decltype(auto) | placeholder for a type to be deduced |
928
  | decltype(*expression*) | the type as defined below |
929
 
930
 
931
+ When multiple *simple-type-specifier*s are allowed, they can be freely
932
+ intermixed with other *decl-specifier*s in any order.
933
+
934
+ [*Note 1*: It is *implementation-defined* whether objects of `char`
935
+ type are represented as signed or unsigned quantities. The `signed`
936
+ specifier forces `char` objects to be signed; it is redundant in other
937
+ contexts. — *end note*]
938
 
939
  For an expression `e`, the type denoted by `decltype(e)` is defined as
940
  follows:
941
 
942
+ - if `e` is an unparenthesized *id-expression* naming a structured
943
+ binding ([[dcl.struct.bind]]), `decltype(e)` is the referenced type
944
+ as given in the specification of the structured binding declaration;
945
+ - otherwise, if `e` is an unparenthesized *id-expression* or an
946
+ unparenthesized class member access ([[expr.ref]]), `decltype(e)` is
947
+ the type of the entity named by `e`. If there is no such entity, or if
948
+ `e` names a set of overloaded functions, the program is ill-formed;
949
  - otherwise, if `e` is an xvalue, `decltype(e)` is `T&&`, where `T` is
950
  the type of `e`;
951
  - otherwise, if `e` is an lvalue, `decltype(e)` is `T&`, where `T` is
952
  the type of `e`;
953
  - otherwise, `decltype(e)` is the type of `e`.
954
 
955
  The operand of the `decltype` specifier is an unevaluated operand
956
  (Clause  [[expr]]).
957
 
958
+ [*Example 1*:
959
+
960
  ``` cpp
961
  const int&& foo();
962
  int i;
963
  struct A { double x; };
964
  const A* a = new A();
965
+ decltype(foo()) x1 = 17; // type is const int&&
966
  decltype(i) x2; // type is int
967
  decltype(a->x) x3; // type is double
968
  decltype((a->x)) x4 = x3; // type is const double&
969
  ```
970
 
971
+ *end example*]
972
+
973
+ [*Note 2*: The rules for determining types involving `decltype(auto)`
974
+ are specified in  [[dcl.spec.auto]]. *end note*]
975
+
976
+ If the operand of a *decltype-specifier* is a prvalue, the temporary
977
+ materialization conversion is not applied ([[conv.rval]]) and no result
978
+ object is provided for the prvalue. The type of the prvalue may be
979
+ incomplete.
980
+
981
+ [*Note 3*: As a result, storage is not allocated for the prvalue and it
982
+ is not destroyed. Thus, a class type is not instantiated as a result of
983
+ being the type of a function call in this context. In this context, the
984
+ common purpose of writing the expression is merely to refer to its type.
985
+ In that sense, a *decltype-specifier* is analogous to a use of a
986
+ *typedef-name*, so the usual reasons for requiring a complete type do
987
+ not apply. In particular, it is not necessary to allocate storage for a
988
+ temporary object or to enforce the semantic constraints associated with
989
+ invoking the type’s destructor. — *end note*]
990
+
991
+ [*Note 4*: Unlike the preceding rule, parentheses have no special
992
+ meaning in this context. — *end note*]
993
+
994
+ [*Example 2*:
995
 
996
  ``` cpp
997
  template<class T> struct A { ~A() = delete; };
998
  template<class T> auto h()
999
  -> A<T>;
1000
  template<class T> auto i(T) // identity
1001
  -> T;
1002
  template<class T> auto f(T) // #1
1003
+ -> decltype(i(h<T>())); // forces completion of A<T> and implicitly uses A<T>::~A()
1004
+ // for the temporary introduced by the use of h().
1005
+ // (A temporary is not introduced as a result of the use of i().)
 
1006
  template<class T> auto f(T) // #2
1007
  -> void;
1008
  auto g() -> void {
1009
+ f(42); // OK: calls #2. (#1 is not a viable candidate: type deduction
1010
+ // fails~([temp.deduct]) because A<int>::~{A()} is implicitly used in its
1011
+ // decltype-specifier)
1012
  }
1013
  template<class T> auto q(T)
1014
+ -> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is not implicitly
1015
+ // used within the context of this decltype-specifier
1016
  void r() {
1017
+ q(42); // Error: deduction against q succeeds, so overload resolution selects
1018
+ // the specialization ``q(T) -> decltype((h<T>())) [with T=int]''.
1019
  // The return type is A<int>, so a temporary is introduced and its
1020
  // destructor is used, so the program is ill-formed.
1021
  }
1022
  ```
1023
 
1024
+ — *end example*]
1025
+
1026
  #### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
1027
 
1028
  ``` bnf
1029
  elaborated-type-specifier:
1030
  class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
 
1058
  resolves to a *class-name* or *enum-name*, the
1059
  *elaborated-type-specifier* introduces it into the declaration the same
1060
  way a *simple-type-specifier* introduces its *type-name*. If the
1061
  *identifier* resolves to a *typedef-name* or the *simple-template-id*
1062
  resolves to an alias template specialization, the
1063
+ *elaborated-type-specifier* is ill-formed.
1064
+
1065
+ [*Note 1*:
1066
+
1067
+ This implies that, within a class template with a template
1068
+ *type-parameter* `T`, the declaration
1069
 
1070
  ``` cpp
1071
  friend class T;
1072
  ```
1073
 
1074
  is ill-formed. However, the similar declaration `friend T;` is allowed (
1075
  [[class.friend]]).
1076
 
1077
+ — *end note*]
1078
+
1079
  The *class-key* or `enum` keyword present in the
1080
  *elaborated-type-specifier* shall agree in kind with the declaration to
1081
  which the name in the *elaborated-type-specifier* refers. This rule also
1082
  applies to the form of *elaborated-type-specifier* that declares a
1083
  *class-name* or `friend` class since it can be construed as referring to
 
1086
  [[dcl.enum]]), the `union` *class-key* shall be used to refer to a union
1087
  (Clause  [[class]]), and either the `class` or `struct` *class-key*
1088
  shall be used to refer to a class (Clause  [[class]]) declared using the
1089
  `class` or `struct` *class-key*.
1090
 
1091
+ [*Example 1*:
1092
+
1093
  ``` cpp
1094
  enum class E { a, b };
1095
  enum E x = E::a; // OK
1096
  ```
1097
 
1098
+ *end example*]
1099
 
1100
+ #### The `auto` specifier <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
1101
+
1102
+ The `auto` and `decltype(auto)` *type-specifier*s are used to designate
1103
+ a placeholder type that will be replaced later by deduction from an
1104
+ initializer. The `auto` *type-specifier* is also used to introduce a
1105
+ function type having a *trailing-return-type* or to signify that a
1106
+ lambda is a generic lambda ([[expr.prim.lambda.closure]]). The `auto`
1107
+ *type-specifier* is also used to introduce a structured binding
1108
+ declaration ([[dcl.struct.bind]]).
1109
 
1110
  The placeholder type can appear with a function declarator in the
1111
  *decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
1112
  *trailing-return-type*, in any context where such a declarator is valid.
1113
  If the function declarator includes a *trailing-return-type* (
1114
+ [[dcl.fct]]), that *trailing-return-type* specifies the declared return
1115
+ type of the function. Otherwise, the function declarator shall declare a
1116
+ function. If the declared return type of the function contains a
1117
+ placeholder type, the return type of the function is deduced from
1118
+ non-discarded `return` statements, if any, in the body of the function (
1119
+ [[stmt.if]]).
1120
 
1121
  If the `auto` *type-specifier* appears as one of the *decl-specifier*s
1122
  in the *decl-specifier-seq* of a *parameter-declaration* of a
1123
  *lambda-expression*, the lambda is a *generic lambda* (
1124
+ [[expr.prim.lambda.closure]]).
1125
+
1126
+ [*Example 1*:
1127
 
1128
  ``` cpp
1129
  auto glambda = [](int i, auto a) { return i; }; // OK: a generic lambda
1130
  ```
1131
 
1132
+ — *end example*]
1133
+
1134
  The type of a variable declared using `auto` or `decltype(auto)` is
1135
+ deduced from its initializer. This use is allowed in an initializing
1136
+ declaration ([[dcl.init]]) of a variable. `auto` or `decltype(auto)`
1137
+ shall appear as one of the *decl-specifier*s in the *decl-specifier-seq*
1138
+ and the *decl-specifier-seq* shall be followed by one or more
1139
+ *declarator*s, each of which shall be followed by a non-empty
 
1140
  *initializer*. In an *initializer* of the form
1141
 
1142
  ``` cpp
1143
  ( expression-list )
1144
  ```
1145
 
1146
  the *expression-list* shall be a single *assignment-expression*.
1147
 
1148
+ [*Example 2*:
1149
+
1150
  ``` cpp
1151
  auto x = 5; // OK: x has type int
1152
  const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int
1153
  static auto y = 0.0; // OK: y has type double
1154
  auto int r; // error: auto is not a storage-class-specifier
1155
  auto f() -> int; // OK: f returns int
1156
  auto g() { return 0.0; } // OK: g returns double
1157
  auto h(); // OK: h's return type will be deduced when it is defined
1158
  ```
1159
 
1160
+ *end example*]
1161
+
1162
+ A placeholder type can also be used in the *type-specifier-seq* in the
1163
+ *new-type-id* or *type-id* of a *new-expression* ([[expr.new]]) and as
1164
+ a *decl-specifier* of the *parameter-declaration*'s *decl-specifier-seq*
1165
+ in a *template-parameter* ([[temp.param]]).
1166
 
1167
  A program that uses `auto` or `decltype(auto)` in a context not
1168
  explicitly allowed in this section is ill-formed.
1169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1170
  If the *init-declarator-list* contains more than one *init-declarator*,
1171
  they shall all form declarations of variables. The type of each declared
1172
+ variable is determined by placeholder type deduction (
1173
+ [[dcl.type.auto.deduct]]), and if the type that replaces the placeholder
1174
+ type is not the same in each deduction, the program is ill-formed.
1175
+
1176
+ [*Example 3*:
1177
 
1178
  ``` cpp
1179
  auto x = 5, *y = &x; // OK: auto is int
1180
  auto a = 5, b = { 1, 2 }; // error: different types for auto
1181
  ```
1182
 
1183
+ — *end example*]
1184
+
1185
  If a function with a declared return type that contains a placeholder
1186
+ type has multiple non-discarded `return` statements, the return type is
1187
+ deduced for each such `return` statement. If the type deduced is not the
1188
+ same in each deduction, the program is ill-formed.
1189
 
1190
  If a function with a declared return type that uses a placeholder type
1191
+ has no non-discarded `return` statements, the return type is deduced as
1192
+ though from a `return` statement with no operand at the closing brace of
1193
+ the function body.
1194
+
1195
+ [*Example 4*:
1196
 
1197
  ``` cpp
1198
  auto f() { } // OK, return type is void
1199
  auto* g() { } // error, cannot deduce auto* from void()
1200
  ```
1201
 
1202
+ — *end example*]
1203
+
1204
  If the type of an entity with an undeduced placeholder type is needed to
1205
  determine the type of an expression, the program is ill-formed. Once a
1206
+ non-discarded `return` statement has been seen in a function, however,
1207
+ the return type deduced from that statement can be used in the rest of
1208
+ the function, including in other `return` statements.
1209
+
1210
+ [*Example 5*:
1211
 
1212
  ``` cpp
1213
  auto n = n; // error, n's type is unknown
1214
  auto f();
1215
  void g() { &f; } // error, f's return type is unknown
 
1219
  else
1220
  return sum(i-1)+i; // OK, sum's return type has been deduced
1221
  }
1222
  ```
1223
 
1224
+ — *end example*]
1225
+
1226
  Return type deduction for a function template with a placeholder in its
1227
  declared type occurs when the definition is instantiated even if the
1228
  function body contains a `return` statement with a non-type-dependent
1229
+ operand.
1230
+
1231
+ [*Note 1*: Therefore, any use of a specialization of the function
1232
+ template will cause an implicit instantiation. Any errors that arise
1233
+ from this instantiation are not in the immediate context of the function
1234
+ type and can result in the program being ill-formed (
1235
+ [[temp.deduct]]). — *end note*]
1236
+
1237
+ [*Example 6*:
1238
 
1239
  ``` cpp
1240
  template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
1241
  typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
1242
  template<class T> auto f(T* t) { return *t; }
1243
  void g() { int (*p)(int*) = &f; } // instantiates both fs to determine return types,
1244
  // chooses second
1245
  ```
1246
 
1247
+ — *end example*]
1248
+
1249
  Redeclarations or specializations of a function or function template
1250
  with a declared return type that uses a placeholder type shall also use
1251
  that placeholder, not a deduced type.
1252
 
1253
+ [*Example 7*:
1254
+
1255
  ``` cpp
1256
  auto f();
1257
  auto f() { return 42; } // return type is int
1258
  auto f(); // OK
1259
  int f(); // error, cannot be overloaded with auto f()
 
1274
  friend T frf(T);
1275
  };
1276
  auto frf(int i) { return i; } // not a friend of A<int>
1277
  ```
1278
 
1279
+ — *end example*]
1280
+
1281
  A function declared with a return type that uses a placeholder type
1282
  shall not be `virtual` ([[class.virtual]]).
1283
 
1284
  An explicit instantiation declaration ([[temp.explicit]]) does not
1285
  cause the instantiation of an entity declared using a placeholder type,
1286
  but it also does not prevent that entity from being instantiated as
1287
  needed to determine its type.
1288
 
1289
+ [*Example 8*:
1290
+
1291
  ``` cpp
1292
  template <typename T> auto f(T t) { return t; }
1293
  extern template auto f(int); // does not instantiate f<int>
1294
  int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
1295
  // instantiation definition is still required somewhere in the program
1296
  ```
1297
 
1298
+ — *end example*]
1299
+
1300
+ ##### Placeholder type deduction <a id="dcl.type.auto.deduct">[[dcl.type.auto.deduct]]</a>
1301
+
1302
+ *Placeholder type deduction* is the process by which a type containing a
1303
+ placeholder type is replaced by a deduced type.
1304
+
1305
+ A type `T` containing a placeholder type, and a corresponding
1306
+ initializer `e`, are determined as follows:
1307
+
1308
+ - for a non-discarded `return` statement that occurs in a function
1309
+ declared with a return type that contains a placeholder type, `T` is
1310
+ the declared return type and `e` is the operand of the `return`
1311
+ statement. If the `return` statement has no operand, then `e` is
1312
+ `void()`;
1313
+ - for a variable declared with a type that contains a placeholder type,
1314
+ `T` is the declared type of the variable and `e` is the initializer.
1315
+ If the initialization is direct-list-initialization, the initializer
1316
+ shall be a *braced-init-list* containing only a single
1317
+ *assignment-expression* and `e` is the *assignment-expression*;
1318
+ - for a non-type template parameter declared with a type that contains a
1319
+ placeholder type, `T` is the declared type of the non-type template
1320
+ parameter and `e` is the corresponding template argument.
1321
+
1322
+ In the case of a `return` statement with no operand or with an operand
1323
+ of type `void`, `T` shall be either `decltype(auto)` or cv `auto`.
1324
+
1325
+ If the deduction is for a `return` statement and `e` is a
1326
+ *braced-init-list* ([[dcl.init.list]]), the program is ill-formed.
1327
+
1328
+ If the placeholder is the `auto` *type-specifier*, the deduced type T'
1329
+ replacing `T` is determined using the rules for template argument
1330
+ deduction. Obtain `P` from `T` by replacing the occurrences of `auto`
1331
+ with either a new invented type template parameter `U` or, if the
1332
+ initialization is copy-list-initialization, with
1333
+ `std::initializer_list<U>`. Deduce a value for `U` using the rules of
1334
+ template argument deduction from a function call (
1335
+ [[temp.deduct.call]]), where `P` is a function template parameter type
1336
+ and the corresponding argument is `e`. If the deduction fails, the
1337
+ declaration is ill-formed. Otherwise, T' is obtained by substituting the
1338
+ deduced `U` into `P`.
1339
+
1340
+ [*Example 9*:
1341
+
1342
+ ``` cpp
1343
+ auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
1344
+ auto x2 = { 1, 2.0 }; // error: cannot deduce element type
1345
+ auto x3{ 1, 2 }; // error: not a single element
1346
+ auto x4 = { 3 }; // decltype(x4) is std::initializer_list<int>
1347
+ auto x5{ 3 }; // decltype(x5) is int
1348
+ ```
1349
+
1350
+ — *end example*]
1351
+
1352
+ [*Example 10*:
1353
+
1354
+ ``` cpp
1355
+ const auto &i = expr;
1356
+ ```
1357
+
1358
+ The type of `i` is the deduced type of the parameter `u` in the call
1359
+ `f(expr)` of the following invented function template:
1360
+
1361
+ ``` cpp
1362
+ template <class U> void f(const U& u);
1363
+ ```
1364
+
1365
+ — *end example*]
1366
+
1367
+ If the placeholder is the `decltype(auto)` *type-specifier*, `T` shall
1368
+ be the placeholder alone. The type deduced for `T` is determined as
1369
+ described in  [[dcl.type.simple]], as though `e` had been the operand of
1370
+ the `decltype`.
1371
+
1372
+ [*Example 11*:
1373
+
1374
+ ``` cpp
1375
+ int i;
1376
+ int&& f();
1377
+ auto x2a(i); // decltype(x2a) is int
1378
+ decltype(auto) x2d(i); // decltype(x2d) is int
1379
+ auto x3a = i; // decltype(x3a) is int
1380
+ decltype(auto) x3d = i; // decltype(x3d) is int
1381
+ auto x4a = (i); // decltype(x4a) is int
1382
+ decltype(auto) x4d = (i); // decltype(x4d) is int&
1383
+ auto x5a = f(); // decltype(x5a) is int
1384
+ decltype(auto) x5d = f(); // decltype(x5d) is int&&
1385
+ auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
1386
+ decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression
1387
+ auto *x7a = &i; // decltype(x7a) is int*
1388
+ decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)
1389
+ ```
1390
+
1391
+ — *end example*]
1392
+
1393
+ #### Deduced class template specialization types <a id="dcl.type.class.deduct">[[dcl.type.class.deduct]]</a>
1394
+
1395
+ If a placeholder for a deduced class type appears as a *decl-specifier*
1396
+ in the *decl-specifier-seq* of an initializing declaration (
1397
+ [[dcl.init]]) of a variable, the placeholder is replaced by the return
1398
+ type of the function selected by overload resolution for class template
1399
+ deduction ([[over.match.class.deduct]]). If the *decl-specifier-seq* is
1400
+ followed by an *init-declarator-list* or *member-declarator-list*
1401
+ containing more than one *declarator*, the type that replaces the
1402
+ placeholder shall be the same in each deduction.
1403
+
1404
+ A placeholder for a deduced class type can also be used in the
1405
+ *type-specifier-seq* in the *new-type-id* or *type-id* of a
1406
+ *new-expression* ([[expr.new]]), or as the *simple-type-specifier* in
1407
+ an explicit type conversion (functional notation) ([[expr.type.conv]]).
1408
+ A placeholder for a deduced class type shall not appear in any other
1409
+ context.
1410
+
1411
+ [*Example 1*:
1412
+
1413
+ ``` cpp
1414
+ template<class T> struct container {
1415
+ container(T t) {}
1416
+ template<class Iter> container(Iter beg, Iter end);
1417
+ };
1418
+ template<class Iter>
1419
+ container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>;
1420
+ std::vector<double> v = { ... };
1421
+
1422
+ container c(7); // OK, deduces int for T
1423
+ auto d = container(v.begin(), v.end()); // OK, deduces double for T
1424
+ container e{5, 6}; // error, int is not an iterator
1425
+ ```
1426
+
1427
+ — *end example*]
1428
+