From Jason Turner

[dcl.dcl]

Large diff (277.9 KB) - rendering may be slow on some devices

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpe1ud_pib/{from.md → to.md} +0 -7880
tmp/tmpe1ud_pib/{from.md → to.md} RENAMED
@@ -1,7880 +0,0 @@
1
- # Declarations <a id="dcl.dcl">[[dcl.dcl]]</a>
2
-
3
- ## Preamble <a id="dcl.pre">[[dcl.pre]]</a>
4
-
5
- Declarations generally specify how names are to be interpreted.
6
- Declarations have the form
7
-
8
- ``` bnf
9
- declaration-seq:
10
- declaration
11
- declaration-seq declaration
12
- ```
13
-
14
- ``` bnf
15
- declaration:
16
- name-declaration
17
- special-declaration
18
- ```
19
-
20
- ``` bnf
21
- name-declaration:
22
- block-declaration
23
- nodeclspec-function-declaration
24
- function-definition
25
- template-declaration
26
- deduction-guide
27
- linkage-specification
28
- namespace-definition
29
- empty-declaration
30
- attribute-declaration
31
- module-import-declaration
32
- ```
33
-
34
- ``` bnf
35
- special-declaration:
36
- explicit-instantiation
37
- explicit-specialization
38
- export-declaration
39
- ```
40
-
41
- ``` bnf
42
- block-declaration:
43
- simple-declaration
44
- asm-declaration
45
- namespace-alias-definition
46
- using-declaration
47
- using-enum-declaration
48
- using-directive
49
- static_assert-declaration
50
- alias-declaration
51
- opaque-enum-declaration
52
- ```
53
-
54
- ``` bnf
55
- nodeclspec-function-declaration:
56
- attribute-specifier-seqₒₚₜ declarator ';'
57
- ```
58
-
59
- ``` bnf
60
- alias-declaration:
61
- using identifier attribute-specifier-seqₒₚₜ '=' defining-type-id ';'
62
- ```
63
-
64
- ``` bnf
65
- simple-declaration:
66
- decl-specifier-seq init-declarator-listₒₚₜ ';'
67
- attribute-specifier-seq decl-specifier-seq init-declarator-list ';'
68
- attribute-specifier-seqₒₚₜ decl-specifier-seq ref-qualifierₒₚₜ '[' identifier-list ']' initializer ';'
69
- ```
70
-
71
- ``` bnf
72
- static_assert-declaration:
73
- static_assert '(' constant-expression ')' ';'
74
- static_assert '(' constant-expression ',' string-literal ')' ';'
75
- ```
76
-
77
- ``` bnf
78
- empty-declaration:
79
- ';'
80
- ```
81
-
82
- ``` bnf
83
- attribute-declaration:
84
- attribute-specifier-seq ';'
85
- ```
86
-
87
- [*Note 1*: *asm-declaration*s are described in  [[dcl.asm]], and
88
- *linkage-specification*s are described in  [[dcl.link]];
89
- *function-definition*s are described in  [[dcl.fct.def]] and
90
- *template-declaration*s and *deduction-guide*s are described in
91
- [[temp.deduct.guide]]; *namespace-definition*s are described in 
92
- [[namespace.def]], *using-declaration*s are described in 
93
- [[namespace.udecl]] and *using-directive*s are described in 
94
- [[namespace.udir]]. — *end note*]
95
-
96
- Certain declarations contain one or more scopes [[basic.scope.scope]].
97
- Unless otherwise stated, utterances in [[dcl.dcl]] about components in,
98
- of, or contained by a declaration or subcomponent thereof refer only to
99
- those components of the declaration that are *not* nested within scopes
100
- nested within the declaration.
101
-
102
- A *simple-declaration* or *nodeclspec-function-declaration* of the form
103
-
104
- ``` bnf
105
- attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ init-declarator-listₒₚₜ ';'
106
- ```
107
-
108
- is divided into three parts. Attributes are described in  [[dcl.attr]].
109
- *decl-specifier*s, the principal components of a *decl-specifier-seq*,
110
- are described in  [[dcl.spec]]. *declarator*s, the components of an
111
- *init-declarator-list*, are described in [[dcl.decl]]. The
112
- *attribute-specifier-seq* appertains to each of the entities declared by
113
- the *declarator*s of the *init-declarator-list*.
114
-
115
- [*Note 2*: In the declaration for an entity, attributes appertaining to
116
- that entity can appear at the start of the declaration and after the
117
- *declarator-id* for that declaration. — *end note*]
118
-
119
- [*Example 1*:
120
-
121
- ``` cpp
122
- [[noreturn]] void f [[noreturn]] (); // OK
123
- ```
124
-
125
- — *end example*]
126
-
127
- If a *declarator-id* is a name, the *init-declarator* and (hence) the
128
- declaration introduce that name.
129
-
130
- [*Note 3*: Otherwise, the *declarator-id* is a *qualified-id* or names
131
- a destructor or its *unqualified-id* is a *template-id* and no name is
132
- introduced. — *end note*]
133
-
134
- The *defining-type-specifier*s [[dcl.type]] in the *decl-specifier-seq*
135
- and the recursive *declarator* structure describe a type
136
- [[dcl.meaning]], which is then associated with the *declarator-id*.
137
-
138
- In a *simple-declaration*, the optional *init-declarator-list* can be
139
- omitted only when declaring a class [[class.pre]] or enumeration
140
- [[dcl.enum]], that is, when the *decl-specifier-seq* contains either a
141
- *class-specifier*, an *elaborated-type-specifier* with a *class-key*
142
- [[class.name]], or an *enum-specifier*. In these cases and whenever a
143
- *class-specifier* or *enum-specifier* is present in the
144
- *decl-specifier-seq*, the identifiers in these specifiers are also
145
- declared (as *class-name*s, *enum-name*s, or *enumerator*s, depending on
146
- the syntax). In such cases, the *decl-specifier-seq* shall (re)introduce
147
- one or more names into the program.
148
-
149
- [*Example 2*:
150
-
151
- ``` cpp
152
- enum { }; // error
153
- typedef class { }; // error
154
- ```
155
-
156
- — *end example*]
157
-
158
- A *simple-declaration* with an *identifier-list* is called a *structured
159
- binding declaration* [[dcl.struct.bind]]. Each *decl-specifier* in the
160
- *decl-specifier-seq* shall be `static`, `thread_local`, `auto`
161
- [[dcl.spec.auto]], or a *cv-qualifier*.
162
-
163
- [*Example 3*:
164
-
165
- ``` cpp
166
- template<class T> concept C = true;
167
- C auto [x, y] = std::pair{1, 2}; // error: constrained placeholder-type-specifier
168
- // not permitted for structured bindings
169
- ```
170
-
171
- — *end example*]
172
-
173
- The *initializer* shall be of the form “`=` *assignment-expression*”, of
174
- the form “`{` *assignment-expression* `}`”, or of the form “`(`
175
- *assignment-expression* `)`”, where the *assignment-expression* is of
176
- array or non-union class type.
177
-
178
- If the *decl-specifier-seq* contains the `typedef` specifier, the
179
- declaration is a *typedef declaration* and each *declarator-id* is
180
- declared to be a *typedef-name*, synonymous with its associated type
181
- [[dcl.typedef]].
182
-
183
- [*Note 4*: Such a *declarator-id* is an *identifier*
184
- [[class.conv.fct]]. — *end note*]
185
-
186
- Otherwise, if the type associated with a *declarator-id* is a function
187
- type [[dcl.fct]], the declaration is a *function declaration*.
188
- Otherwise, if the type associated with a *declarator-id* is an object or
189
- reference type, the declaration is an *object declaration*. Otherwise,
190
- the program is ill-formed.
191
-
192
- [*Example 4*:
193
-
194
- ``` cpp
195
- int f(), x; // OK, function declaration for f and object declaration for x
196
- extern void g(), // OK, function declaration for g
197
- y; // error: void is not an object type
198
- ```
199
-
200
- — *end example*]
201
-
202
- Syntactic components beyond those found in the general form of
203
- *simple-declaration* are added to a function declaration to make a
204
- *function-definition*. An object declaration, however, is also a
205
- definition unless it contains the `extern` specifier and has no
206
- initializer [[basic.def]]. An object definition causes storage of
207
- appropriate size and alignment to be reserved and any appropriate
208
- initialization [[dcl.init]] to be done.
209
-
210
- A *nodeclspec-function-declaration* shall declare a constructor,
211
- destructor, or conversion function.
212
-
213
- [*Note 5*: Because a member function cannot be subject to a
214
- non-defining declaration outside of a class definition [[class.mfct]], a
215
- *nodeclspec-function-declaration* can only be used in a
216
- *template-declaration* [[temp.pre]], *explicit-instantiation*
217
- [[temp.explicit]], or *explicit-specialization*
218
- [[temp.expl.spec]]. — *end note*]
219
-
220
- In a *static_assert-declaration*, the *constant-expression* is
221
- contextually converted to `bool` and the converted expression shall be a
222
- constant expression [[expr.const]]. If the value of the expression when
223
- so converted is `true` or the expression is evaluated in the context of
224
- a template definition, the declaration has no effect. Otherwise, the
225
- *static_assert-declaration* *fails*, the program is ill-formed, and the
226
- resulting diagnostic message [[intro.compliance]] should include the
227
- text of the *string-literal*, if one is supplied.
228
-
229
- [*Example 5*:
230
-
231
- ``` cpp
232
- static_assert(sizeof(int) == sizeof(void*), "wrong pointer size");
233
- static_assert(sizeof(int[2])); // OK, narrowing allowed
234
-
235
- template <class T>
236
- void f(T t) {
237
- if constexpr (sizeof(T) == sizeof(int)) {
238
- use(t);
239
- } else {
240
- static_assert(false, "must be int-sized");
241
- }
242
- }
243
-
244
- void g(char c) {
245
- f(0); // OK
246
- f(c); // error on implementations where sizeof(int) > 1: must be int-sized
247
- }
248
- ```
249
-
250
- — *end example*]
251
-
252
- An *empty-declaration* has no effect.
253
-
254
- Except where otherwise specified, the meaning of an
255
- *attribute-declaration* is *implementation-defined*.
256
-
257
- ## Specifiers <a id="dcl.spec">[[dcl.spec]]</a>
258
-
259
- ### General <a id="dcl.spec.general">[[dcl.spec.general]]</a>
260
-
261
- The specifiers that can be used in a declaration are
262
-
263
- ``` bnf
264
- decl-specifier:
265
- storage-class-specifier
266
- defining-type-specifier
267
- function-specifier
268
- friend
269
- typedef
270
- constexpr
271
- consteval
272
- constinit
273
- inline
274
- ```
275
-
276
- ``` bnf
277
- decl-specifier-seq:
278
- decl-specifier attribute-specifier-seqₒₚₜ
279
- decl-specifier decl-specifier-seq
280
- ```
281
-
282
- The optional *attribute-specifier-seq* in a *decl-specifier-seq*
283
- appertains to the type determined by the preceding *decl-specifier*s
284
- [[dcl.meaning]]. The *attribute-specifier-seq* affects the type only for
285
- the declaration it appears in, not other declarations involving the same
286
- type.
287
-
288
- Each *decl-specifier* shall appear at most once in a complete
289
- *decl-specifier-seq*, except that `long` may appear twice. At most one
290
- of the `constexpr`, `consteval`, and `constinit` keywords shall appear
291
- in a *decl-specifier-seq*.
292
-
293
- If a *type-name* is encountered while parsing a *decl-specifier-seq*, it
294
- is interpreted as part of the *decl-specifier-seq* if and only if there
295
- is no previous *defining-type-specifier* other than a *cv-qualifier* in
296
- the *decl-specifier-seq*. The sequence shall be self-consistent as
297
- described below.
298
-
299
- [*Example 1*:
300
-
301
- ``` cpp
302
- typedef char* Pc;
303
- static Pc; // error: name missing
304
- ```
305
-
306
- Here, the declaration `static` `Pc` is ill-formed because no name was
307
- specified for the static variable of type `Pc`. To get a variable called
308
- `Pc`, a *type-specifier* (other than `const` or `volatile`) has to be
309
- present to indicate that the *typedef-name* `Pc` is the name being
310
- (re)declared, rather than being part of the *decl-specifier* sequence.
311
- For another example,
312
-
313
- ``` cpp
314
- void f(const Pc); // void f(char* const) (not const char*)
315
- void g(const int Pc); // void g(const int)
316
- ```
317
-
318
- — *end example*]
319
-
320
- [*Note 1*:
321
-
322
- Since `signed`, `unsigned`, `long`, and `short` by default imply `int`,
323
- a *type-name* appearing after one of those specifiers is treated as the
324
- name being (re)declared.
325
-
326
- [*Example 2*:
327
-
328
- ``` cpp
329
- void h(unsigned Pc); // void h(unsigned int)
330
- void k(unsigned int Pc); // void k(unsigned int)
331
- ```
332
-
333
- — *end example*]
334
-
335
- — *end note*]
336
-
337
- ### Storage class specifiers <a id="dcl.stc">[[dcl.stc]]</a>
338
-
339
- The storage class specifiers are
340
-
341
- ``` bnf
342
- storage-class-specifier:
343
- static
344
- thread_local
345
- extern
346
- mutable
347
- ```
348
-
349
- At most one *storage-class-specifier* shall appear in a given
350
- *decl-specifier-seq*, except that `thread_local` may appear with
351
- `static` or `extern`. If `thread_local` appears in any declaration of a
352
- variable it shall be present in all declarations of that entity. If a
353
- *storage-class-specifier* appears in a *decl-specifier-seq*, there can
354
- be no `typedef` specifier in the same *decl-specifier-seq* and the
355
- *init-declarator-list* or *member-declarator-list* of the declaration
356
- shall not be empty (except for an anonymous union declared in a
357
- namespace scope [[class.union.anon]]). The *storage-class-specifier*
358
- applies to the name declared by each *init-declarator* in the list and
359
- not to any names declared by other specifiers.
360
-
361
- [*Note 1*: See [[temp.expl.spec]] and [[temp.explicit]] for
362
- restrictions in explicit specializations and explicit instantiations,
363
- respectively. — *end note*]
364
-
365
- [*Note 2*: A variable declared without a *storage-class-specifier* at
366
- block scope or declared as a function parameter has automatic storage
367
- duration by default [[basic.stc.auto]]. — *end note*]
368
-
369
- The `thread_local` specifier indicates that the named entity has thread
370
- storage duration [[basic.stc.thread]]. It shall be applied only to the
371
- declaration of a variable of namespace or block scope, to a structured
372
- binding declaration [[dcl.struct.bind]], or to the declaration of a
373
- static data member. When `thread_local` is applied to a variable of
374
- block scope the *storage-class-specifier* `static` is implied if no
375
- other *storage-class-specifier* appears in the *decl-specifier-seq*.
376
-
377
- The `static` specifier shall be applied only to the declaration of a
378
- variable or function, to a structured binding declaration
379
- [[dcl.struct.bind]], or to the declaration of an anonymous union
380
- [[class.union.anon]]. There can be no `static` function declarations
381
- within a block, nor any `static` function parameters. A `static`
382
- specifier used in the declaration of a variable declares the variable to
383
- have static storage duration [[basic.stc.static]], unless accompanied by
384
- the `thread_local` specifier, which declares the variable to have thread
385
- storage duration [[basic.stc.thread]]. A `static` specifier can be used
386
- in declarations of class members;  [[class.static]] describes its
387
- effect. For the linkage of a name declared with a `static` specifier,
388
- see  [[basic.link]].
389
-
390
- The `extern` specifier shall be applied only to the declaration of a
391
- variable or function. The `extern` specifier shall not be used in the
392
- declaration of a class member or function parameter. For the linkage of
393
- a name declared with an `extern` specifier, see  [[basic.link]].
394
-
395
- [*Note 3*: The `extern` keyword can also be used in
396
- *explicit-instantiation*s and *linkage-specification*s, but it is not a
397
- *storage-class-specifier* in such contexts. — *end note*]
398
-
399
- All declarations for a given entity shall give its name the same
400
- linkage.
401
-
402
- [*Note 4*: The linkage given by some declarations is affected by
403
- previous declarations. Overloads are distinct entities. — *end note*]
404
-
405
- [*Example 1*:
406
-
407
- ``` cpp
408
- static char* f(); // f() has internal linkage
409
- char* f() // f() still has internal linkage
410
- { ... }
411
-
412
- char* g(); // g() has external linkage
413
- static char* g() // error: inconsistent linkage
414
- { ... }
415
-
416
- void h();
417
- inline void h(); // external linkage
418
-
419
- inline void l();
420
- void l(); // external linkage
421
-
422
- inline void m();
423
- extern void m(); // external linkage
424
-
425
- static void n();
426
- inline void n(); // internal linkage
427
-
428
- static int a; // a has internal linkage
429
- int a; // error: two definitions
430
-
431
- static int b; // b has internal linkage
432
- extern int b; // b still has internal linkage
433
-
434
- int c; // c has external linkage
435
- static int c; // error: inconsistent linkage
436
-
437
- extern int d; // d has external linkage
438
- static int d; // error: inconsistent linkage
439
- ```
440
-
441
- — *end example*]
442
-
443
- The name of a declared but undefined class can be used in an `extern`
444
- declaration. Such a declaration can only be used in ways that do not
445
- require a complete class type.
446
-
447
- [*Example 2*:
448
-
449
- ``` cpp
450
- struct S;
451
- extern S a;
452
- extern S f();
453
- extern void g(S);
454
-
455
- void h() {
456
- g(a); // error: S is incomplete
457
- f(); // error: S is incomplete
458
- }
459
- ```
460
-
461
- — *end example*]
462
-
463
- The `mutable` specifier shall appear only in the declaration of a
464
- non-static data member [[class.mem]] whose type is neither
465
- const-qualified nor a reference type.
466
-
467
- [*Example 3*:
468
-
469
- ``` cpp
470
- class X {
471
- mutable const int* p; // OK
472
- mutable int* const q; // error
473
- };
474
- ```
475
-
476
- — *end example*]
477
-
478
- [*Note 5*: The `mutable` specifier on a class data member nullifies a
479
- `const` specifier applied to the containing class object and permits
480
- modification of the mutable class member even though the rest of the
481
- object is const
482
- [[basic.type.qualifier]], [[dcl.type.cv]]. — *end note*]
483
-
484
- ### Function specifiers <a id="dcl.fct.spec">[[dcl.fct.spec]]</a>
485
-
486
- A *function-specifier* can be used only in a function declaration.
487
-
488
- ``` bnf
489
- function-specifier:
490
- virtual
491
- explicit-specifier
492
- ```
493
-
494
- ``` bnf
495
- explicit-specifier:
496
- explicit '(' constant-expression ')'
497
- explicit
498
- ```
499
-
500
- The `virtual` specifier shall be used only in the initial declaration of
501
- a non-static member function; see  [[class.virtual]].
502
-
503
- An *explicit-specifier* shall be used only in the declaration of a
504
- constructor or conversion function within its class definition; see 
505
- [[class.conv.ctor]] and  [[class.conv.fct]].
506
-
507
- In an *explicit-specifier*, the *constant-expression*, if supplied,
508
- shall be a contextually converted constant expression of type `bool`
509
- [[expr.const]]. The *explicit-specifier* `explicit` without a
510
- *constant-expression* is equivalent to the *explicit-specifier*
511
- `explicit(true)`. If the constant expression evaluates to `true`, the
512
- function is explicit. Otherwise, the function is not explicit. A `(`
513
- token that follows `explicit` is parsed as part of the
514
- *explicit-specifier*.
515
-
516
- [*Example 1*:
517
-
518
- ``` cpp
519
- struct S {
520
- explicit(sizeof(char[2])) S(char); // error: narrowing conversion of value 2 to type bool
521
- explicit(sizeof(char)) S(bool); // OK, conversion of value 1 to type bool is non-narrowing
522
- };
523
- ```
524
-
525
- — *end example*]
526
-
527
- ### The `typedef` specifier <a id="dcl.typedef">[[dcl.typedef]]</a>
528
-
529
- Declarations containing the *decl-specifier* `typedef` declare
530
- identifiers that can be used later for naming fundamental
531
- [[basic.fundamental]] or compound [[basic.compound]] types. The
532
- `typedef` specifier shall not be combined in a *decl-specifier-seq* with
533
- any other kind of specifier except a *defining-type-specifier*, and it
534
- shall not be used in the *decl-specifier-seq* of a
535
- *parameter-declaration* [[dcl.fct]] nor in the *decl-specifier-seq* of a
536
- *function-definition* [[dcl.fct.def]]. If a `typedef` specifier appears
537
- in a declaration without a *declarator*, the program is ill-formed.
538
-
539
- ``` bnf
540
- typedef-name:
541
- identifier
542
- simple-template-id
543
- ```
544
-
545
- A name declared with the `typedef` specifier becomes a *typedef-name*. A
546
- *typedef-name* names the type associated with the *identifier*
547
- [[dcl.decl]] or *simple-template-id* [[temp.pre]]; a *typedef-name* is
548
- thus a synonym for another type. A *typedef-name* does not introduce a
549
- new type the way a class declaration [[class.name]] or enum declaration
550
- [[dcl.enum]] does.
551
-
552
- [*Example 1*:
553
-
554
- After
555
-
556
- ``` cpp
557
- typedef int MILES, *KLICKSP;
558
- ```
559
-
560
- the constructions
561
-
562
- ``` cpp
563
- MILES distance;
564
- extern KLICKSP metricp;
565
- ```
566
-
567
- are all correct declarations; the type of `distance` is `int` and that
568
- of `metricp` is “pointer to `int`”.
569
-
570
- — *end example*]
571
-
572
- A *typedef-name* can also be introduced by an *alias-declaration*. The
573
- *identifier* following the `using` keyword is not looked up; it becomes
574
- a *typedef-name* and the optional *attribute-specifier-seq* following
575
- the *identifier* appertains to that *typedef-name*. Such a
576
- *typedef-name* has the same semantics as if it were introduced by the
577
- `typedef` specifier. In particular, it does not define a new type.
578
-
579
- [*Example 2*:
580
-
581
- ``` cpp
582
- using handler_t = void (*)(int);
583
- extern handler_t ignore;
584
- extern void (*ignore)(int); // redeclare ignore
585
- template<class T> struct P { };
586
- using cell = P<cell*>; // error: cell not found[basic.scope.pdecl]
587
- ```
588
-
589
- — *end example*]
590
-
591
- The *defining-type-specifier-seq* of the *defining-type-id* shall not
592
- define a class or enumeration if the *alias-declaration* is the
593
- *declaration* of a *template-declaration*.
594
-
595
- A *simple-template-id* is only a *typedef-name* if its *template-name*
596
- names an alias template or a template *template-parameter*.
597
-
598
- [*Note 1*: A *simple-template-id* that names a class template
599
- specialization is a *class-name* [[class.name]]. If a *typedef-name* is
600
- used to identify the subject of an *elaborated-type-specifier*
601
- [[dcl.type.elab]], a class definition [[class]], a constructor
602
- declaration [[class.ctor]], or a destructor declaration [[class.dtor]],
603
- the program is ill-formed. — *end note*]
604
-
605
- [*Example 3*:
606
-
607
- ``` cpp
608
- struct S {
609
- S();
610
- ~S();
611
- };
612
-
613
- typedef struct S T;
614
-
615
- S a = T(); // OK
616
- struct T * p; // error
617
- ```
618
-
619
- — *end example*]
620
-
621
- An unnamed class or enumeration C defined in a typedef declaration has
622
- the first *typedef-name* declared by the declaration to be of type C as
623
- its *typedef name for linkage purposes* [[basic.link]].
624
-
625
- [*Note 2*: A typedef declaration involving a *lambda-expression* does
626
- not itself define the associated closure type, and so the closure type
627
- is not given a typedef name for linkage purposes. — *end note*]
628
-
629
- [*Example 4*:
630
-
631
- ``` cpp
632
- typedef struct { } *ps, S; // S is the typedef name for linkage purposes
633
- typedef decltype([]{}) C; // the closure type has no typedef name for linkage purposes
634
- ```
635
-
636
- — *end example*]
637
-
638
- An unnamed class with a typedef name for linkage purposes shall not
639
-
640
- - declare any members other than non-static data members, member
641
- enumerations, or member classes,
642
- - have any base classes or default member initializers, or
643
- - contain a *lambda-expression*,
644
-
645
- and all member classes shall also satisfy these requirements
646
- (recursively).
647
-
648
- [*Example 5*:
649
-
650
- ``` cpp
651
- typedef struct {
652
- int f() {}
653
- } X; // error: struct with typedef name for linkage has member functions
654
- ```
655
-
656
- — *end example*]
657
-
658
- ### The `friend` specifier <a id="dcl.friend">[[dcl.friend]]</a>
659
-
660
- The `friend` specifier is used to specify access to class members; see 
661
- [[class.friend]].
662
-
663
- ### The `constexpr` and `consteval` specifiers <a id="dcl.constexpr">[[dcl.constexpr]]</a>
664
-
665
- The `constexpr` specifier shall be applied only to the definition of a
666
- variable or variable template or the declaration of a function or
667
- function template. The `consteval` specifier shall be applied only to
668
- the declaration of a function or function template. A function or static
669
- data member declared with the `constexpr` or `consteval` specifier is
670
- implicitly an inline function or variable [[dcl.inline]]. If any
671
- declaration of a function or function template has a `constexpr` or
672
- `consteval` specifier, then all its declarations shall contain the same
673
- specifier.
674
-
675
- [*Note 1*: An explicit specialization can differ from the template
676
- declaration with respect to the `constexpr` or `consteval`
677
- specifier. — *end note*]
678
-
679
- [*Note 2*: Function parameters cannot be declared
680
- `constexpr`. — *end note*]
681
-
682
- [*Example 1*:
683
-
684
- ``` cpp
685
- constexpr void square(int &x); // OK, declaration
686
- constexpr int bufsz = 1024; // OK, definition
687
- constexpr struct pixel { // error: pixel is a type
688
- int x;
689
- int y;
690
- constexpr pixel(int); // OK, declaration
691
- };
692
- constexpr pixel::pixel(int a)
693
- : x(a), y(x) // OK, definition
694
- { square(x); }
695
- constexpr pixel small(2); // error: square not defined, so small(2)
696
- // not constant[expr.const] so constexpr not satisfied
697
-
698
- constexpr void square(int &x) { // OK, definition
699
- x *= x;
700
- }
701
- constexpr pixel large(4); // OK, square defined
702
- int next(constexpr int x) { // error: not for parameters
703
- return x + 1;
704
- }
705
- extern constexpr int memsz; // error: not a definition
706
- ```
707
-
708
- — *end example*]
709
-
710
- A `constexpr` or `consteval` specifier used in the declaration of a
711
- function declares that function to be a *constexpr function*.
712
-
713
- [*Note 3*: A function or constructor declared with the `consteval`
714
- specifier is an immediate function [[expr.const]]. — *end note*]
715
-
716
- A destructor, an allocation function, or a deallocation function shall
717
- not be declared with the `consteval` specifier.
718
-
719
- A function is *constexpr-suitable* if:
720
-
721
- - it is not a coroutine [[dcl.fct.def.coroutine]], and
722
- - if the function is a constructor or destructor, its class does not
723
- have any virtual base classes.
724
-
725
- Except for instantiated constexpr functions, non-templated constexpr
726
- functions shall be constexpr-suitable.
727
-
728
- [*Example 2*:
729
-
730
- ``` cpp
731
- constexpr int square(int x)
732
- { return x * x; } // OK
733
- constexpr long long_max()
734
- { return 2147483647; } // OK
735
- constexpr int abs(int x) {
736
- if (x < 0)
737
- x = -x;
738
- return x; // OK
739
- }
740
- constexpr int constant_non_42(int n) { // OK
741
- if (n == 42) {
742
- static int value = n;
743
- return value;
744
- }
745
- return n;
746
- }
747
- constexpr int uninit() {
748
- struct { int a; } s;
749
- return s.a; // error: uninitialized read of s.a
750
- }
751
- constexpr int prev(int x)
752
- { return --x; } // OK
753
- constexpr int g(int x, int n) { // OK
754
- int r = 1;
755
- while (--n > 0) r *= x;
756
- return r;
757
- }
758
- ```
759
-
760
- — *end example*]
761
-
762
- An invocation of a constexpr function in a given context produces the
763
- same result as an invocation of an equivalent non-constexpr function in
764
- the same context in all respects except that
765
-
766
- - an invocation of a constexpr function can appear in a constant
767
- expression [[expr.const]] and
768
- - copy elision is not performed in a constant expression
769
- [[class.copy.elision]].
770
-
771
- [*Note 4*: Declaring a function constexpr can change whether an
772
- expression is a constant expression. This can indirectly cause calls to
773
- `std::is_constant_evaluated` within an invocation of the function to
774
- produce a different value. — *end note*]
775
-
776
- [*Note 5*: It is possible to write a constexpr function for which no
777
- invocation satisfies the requirements of a core constant
778
- expression. — *end note*]
779
-
780
- The `constexpr` and `consteval` specifiers have no effect on the type of
781
- a constexpr function.
782
-
783
- [*Example 3*:
784
-
785
- ``` cpp
786
- constexpr int bar(int x, int y) // OK
787
- { return x + y + x*y; }
788
- // ...
789
- int bar(int x, int y) // error: redefinition of bar
790
- { return x * 2 + 3 * y; }
791
- ```
792
-
793
- — *end example*]
794
-
795
- A `constexpr` specifier used in an object declaration declares the
796
- object as const. Such an object shall have literal type and shall be
797
- initialized. In any `constexpr` variable declaration, the
798
- full-expression of the initialization shall be a constant expression
799
- [[expr.const]]. A `constexpr` variable that is an object, as well as any
800
- temporary to which a `constexpr` reference is bound, shall have constant
801
- destruction.
802
-
803
- [*Example 4*:
804
-
805
- ``` cpp
806
- struct pixel {
807
- int x, y;
808
- };
809
- constexpr pixel ur = { 1294, 1024 }; // OK
810
- constexpr pixel origin; // error: initializer missing
811
- ```
812
-
813
- — *end example*]
814
-
815
- ### The `constinit` specifier <a id="dcl.constinit">[[dcl.constinit]]</a>
816
-
817
- The `constinit` specifier shall be applied only to a declaration of a
818
- variable with static or thread storage duration. If the specifier is
819
- applied to any declaration of a variable, it shall be applied to the
820
- initializing declaration. No diagnostic is required if no `constinit`
821
- declaration is reachable at the point of the initializing declaration.
822
-
823
- If a variable declared with the `constinit` specifier has dynamic
824
- initialization [[basic.start.dynamic]], the program is ill-formed, even
825
- if the implementation would perform that initialization as a static
826
- initialization [[basic.start.static]].
827
-
828
- [*Note 1*: The `constinit` specifier ensures that the variable is
829
- initialized during static initialization. — *end note*]
830
-
831
- [*Example 1*:
832
-
833
- ``` cpp
834
- const char * g() { return "dynamic initialization"; }
835
- constexpr const char * f(bool p) { return p ? "constant initializer" : g(); }
836
- constinit const char * c = f(true); // OK
837
- constinit const char * d = f(false); // error
838
- ```
839
-
840
- — *end example*]
841
-
842
- ### The `inline` specifier <a id="dcl.inline">[[dcl.inline]]</a>
843
-
844
- The `inline` specifier shall be applied only to the declaration of a
845
- variable or function.
846
-
847
- A function declaration [[dcl.fct]], [[class.mfct]], [[class.friend]]
848
- with an `inline` specifier declares an *inline function*. The inline
849
- specifier indicates to the implementation that inline substitution of
850
- the function body at the point of call is to be preferred to the usual
851
- function call mechanism. An implementation is not required to perform
852
- this inline substitution at the point of call; however, even if this
853
- inline substitution is omitted, the other rules for inline functions
854
- specified in this subclause shall still be respected.
855
-
856
- [*Note 1*: The `inline` keyword has no effect on the linkage of a
857
- function. In certain cases, an inline function cannot use names with
858
- internal linkage; see  [[basic.link]]. — *end note*]
859
-
860
- A variable declaration with an `inline` specifier declares an
861
- *inline variable*.
862
-
863
- The `inline` specifier shall not appear on a block scope declaration or
864
- on the declaration of a function parameter. If the `inline` specifier is
865
- used in a friend function declaration, that declaration shall be a
866
- definition or the function shall have previously been declared inline.
867
-
868
- If a definition of a function or variable is reachable at the point of
869
- its first declaration as inline, the program is ill-formed. If a
870
- function or variable with external or module linkage is declared inline
871
- in one definition domain, an inline declaration of it shall be reachable
872
- from the end of every definition domain in which it is declared; no
873
- diagnostic is required.
874
-
875
- [*Note 2*: A call to an inline function or a use of an inline variable
876
- can be encountered before its definition becomes reachable in a
877
- translation unit. — *end note*]
878
-
879
- [*Note 3*: An inline function or variable with external or module
880
- linkage can be defined in multiple translation units [[basic.def.odr]],
881
- but is one entity with one address. A type or `static` variable defined
882
- in the body of such a function is therefore a single
883
- entity. — *end note*]
884
-
885
- If an inline function or variable that is attached to a named module is
886
- declared in a definition domain, it shall be defined in that domain.
887
-
888
- [*Note 4*: A constexpr function [[dcl.constexpr]] is implicitly inline.
889
- In the global module, a function defined within a class definition is
890
- implicitly inline [[class.mfct]], [[class.friend]]. — *end note*]
891
-
892
- ### Type specifiers <a id="dcl.type">[[dcl.type]]</a>
893
-
894
- #### General <a id="dcl.type.general">[[dcl.type.general]]</a>
895
-
896
- The type-specifiers are
897
-
898
- ``` bnf
899
- type-specifier:
900
- simple-type-specifier
901
- elaborated-type-specifier
902
- typename-specifier
903
- cv-qualifier
904
- ```
905
-
906
- ``` bnf
907
- type-specifier-seq:
908
- type-specifier attribute-specifier-seqₒₚₜ
909
- type-specifier type-specifier-seq
910
- ```
911
-
912
- ``` bnf
913
- defining-type-specifier:
914
- type-specifier
915
- class-specifier
916
- enum-specifier
917
- ```
918
-
919
- ``` bnf
920
- defining-type-specifier-seq:
921
- defining-type-specifier attribute-specifier-seqₒₚₜ
922
- defining-type-specifier defining-type-specifier-seq
923
- ```
924
-
925
- The optional *attribute-specifier-seq* in a *type-specifier-seq* or a
926
- *defining-type-specifier-seq* appertains to the type denoted by the
927
- preceding *type-specifier*s or *defining-type-specifier*s
928
- [[dcl.meaning]]. The *attribute-specifier-seq* affects the type only for
929
- the declaration it appears in, not other declarations involving the same
930
- type.
931
-
932
- As a general rule, at most one *defining-type-specifier* is allowed in
933
- the complete *decl-specifier-seq* of a *declaration* or in a
934
- *defining-type-specifier-seq*, and at most one *type-specifier* is
935
- allowed in a *type-specifier-seq*. The only exceptions to this rule are
936
- the following:
937
-
938
- - `const` can be combined with any type specifier except itself.
939
- - `volatile` can be combined with any type specifier except itself.
940
- - `signed` or `unsigned` can be combined with `char`, `long`, `short`,
941
- or `int`.
942
- - `short` or `long` can be combined with `int`.
943
- - `long` can be combined with `double`.
944
- - `long` can be combined with `long`.
945
-
946
- Except in a declaration of a constructor, destructor, or conversion
947
- function, at least one *defining-type-specifier* that is not a
948
- *cv-qualifier* shall appear in a complete *type-specifier-seq* or a
949
- complete *decl-specifier-seq*.[^1]
950
-
951
- [*Note 1*: *enum-specifier*s, *class-specifier*s, and
952
- *typename-specifier*s are discussed in [[dcl.enum]], [[class]], and
953
- [[temp.res]], respectively. The remaining *type-specifier*s are
954
- discussed in the rest of [[dcl.type]]. — *end note*]
955
-
956
- #### The *cv-qualifier*s <a id="dcl.type.cv">[[dcl.type.cv]]</a>
957
-
958
- There are two *cv-qualifier*s, `const` and `volatile`. Each
959
- *cv-qualifier* shall appear at most once in a *cv-qualifier-seq*. If a
960
- *cv-qualifier* appears in a *decl-specifier-seq*, the
961
- *init-declarator-list* or *member-declarator-list* of the declaration
962
- shall not be empty.
963
-
964
- [*Note 1*: [[basic.type.qualifier]] and [[dcl.fct]] describe how
965
- cv-qualifiers affect object and function types. — *end note*]
966
-
967
- Redundant cv-qualifications are ignored.
968
-
969
- [*Note 2*: For example, these could be introduced by
970
- typedefs. — *end note*]
971
-
972
- [*Note 3*: Declaring a variable `const` can affect its linkage
973
- [[dcl.stc]] and its usability in constant expressions [[expr.const]]. As
974
- described in  [[dcl.init]], the definition of an object or subobject of
975
- const-qualified type must specify an initializer or be subject to
976
- default-initialization. — *end note*]
977
-
978
- A pointer or reference to a cv-qualified type need not actually point or
979
- refer to a cv-qualified object, but it is treated as if it does; a
980
- const-qualified access path cannot be used to modify an object even if
981
- the object referenced is a non-const object and can be modified through
982
- some other access path.
983
-
984
- [*Note 4*: Cv-qualifiers are supported by the type system so that they
985
- cannot be subverted without casting [[expr.const.cast]]. — *end note*]
986
-
987
- Any attempt to modify
988
- [[expr.ass]], [[expr.post.incr]], [[expr.pre.incr]] a const object
989
- [[basic.type.qualifier]] during its lifetime [[basic.life]] results in
990
- undefined behavior.
991
-
992
- [*Example 1*:
993
-
994
- ``` cpp
995
- const int ci = 3; // cv-qualified (initialized as required)
996
- ci = 4; // error: attempt to modify const
997
-
998
- int i = 2; // not cv-qualified
999
- const int* cip; // pointer to const int
1000
- cip = &i; // OK, cv-qualified access path to unqualified
1001
- *cip = 4; // error: attempt to modify through ptr to const
1002
-
1003
- int* ip;
1004
- ip = const_cast<int*>(cip); // cast needed to convert const int* to int*
1005
- *ip = 4; // defined: *ip points to i, a non-const object
1006
-
1007
- const int* ciq = new const int (3); // initialized as required
1008
- int* iq = const_cast<int*>(ciq); // cast required
1009
- *iq = 4; // undefined behavior: modifies a const object
1010
- ```
1011
-
1012
- For another example,
1013
-
1014
- ``` cpp
1015
- struct X {
1016
- mutable int i;
1017
- int j;
1018
- };
1019
- struct Y {
1020
- X x;
1021
- Y();
1022
- };
1023
-
1024
- const Y y;
1025
- y.x.i++; // well-formed: mutable member can be modified
1026
- y.x.j++; // error: const-qualified member modified
1027
- Y* p = const_cast<Y*>(&y); // cast away const-ness of y
1028
- p->x.i = 99; // well-formed: mutable member can be modified
1029
- p->x.j = 99; // undefined behavior: modifies a const subobject
1030
- ```
1031
-
1032
- — *end example*]
1033
-
1034
- The semantics of an access through a volatile glvalue are
1035
- *implementation-defined*. If an attempt is made to access an object
1036
- defined with a volatile-qualified type through the use of a non-volatile
1037
- glvalue, the behavior is undefined.
1038
-
1039
- [*Note 5*: `volatile` is a hint to the implementation to avoid
1040
- aggressive optimization involving the object because the value of the
1041
- object might be changed by means undetectable by an implementation.
1042
- Furthermore, for some implementations, `volatile` might indicate that
1043
- special hardware instructions are required to access the object. See 
1044
- [[intro.execution]] for detailed semantics. In general, the semantics of
1045
- `volatile` are intended to be the same in C++ as they are in
1046
- C. — *end note*]
1047
-
1048
- #### Simple type specifiers <a id="dcl.type.simple">[[dcl.type.simple]]</a>
1049
-
1050
- The simple type specifiers are
1051
-
1052
- ``` bnf
1053
- simple-type-specifier:
1054
- nested-name-specifierₒₚₜ type-name
1055
- nested-name-specifier template simple-template-id
1056
- decltype-specifier
1057
- placeholder-type-specifier
1058
- nested-name-specifierₒₚₜ template-name
1059
- char
1060
- char8_t
1061
- char16_t
1062
- char32_t
1063
- wchar_t
1064
- bool
1065
- short
1066
- int
1067
- long
1068
- signed
1069
- unsigned
1070
- float
1071
- double
1072
- void
1073
- ```
1074
-
1075
- ``` bnf
1076
- type-name:
1077
- class-name
1078
- enum-name
1079
- typedef-name
1080
- ```
1081
-
1082
- The component names of a *simple-type-specifier* are those of its
1083
- *nested-name-specifier*, *type-name*, *simple-template-id*,
1084
- *template-name*, and/or *type-constraint* (if it is a
1085
- *placeholder-type-specifier*). The component name of a *type-name* is
1086
- the first name in it.
1087
-
1088
- A *placeholder-type-specifier* is a placeholder for a type to be deduced
1089
- [[dcl.spec.auto]]. A *type-specifier* of the form `typename`ₒₚₜ
1090
- *nested-name-specifier*ₒₚₜ *template-name* is a placeholder for a
1091
- deduced class type [[dcl.type.class.deduct]]. The
1092
- *nested-name-specifier*, if any, shall be non-dependent and the
1093
- *template-name* shall name a deducible template. A *deducible template*
1094
- is either a class template or is an alias template whose
1095
- *defining-type-id* is of the form
1096
-
1097
- ``` bnf
1098
- typenameₒₚₜ nested-name-specifierₒₚₜ templateₒₚₜ simple-template-id
1099
- ```
1100
-
1101
- where the *nested-name-specifier* (if any) is non-dependent and the
1102
- *template-name* of the *simple-template-id* names a deducible template.
1103
-
1104
- [*Note 1*: An injected-class-name is never interpreted as a
1105
- *template-name* in contexts where class template argument deduction
1106
- would be performed [[temp.local]]. — *end note*]
1107
-
1108
- The other *simple-type-specifier*s specify either a previously-declared
1109
- type, a type determined from an expression, or one of the fundamental
1110
- types [[basic.fundamental]]. [[dcl.type.simple]] summarizes the valid
1111
- combinations of *simple-type-specifier*s and the types they specify.
1112
-
1113
- **Table: *simple-type-specifier*{s} and the types they specify** <a id="dcl.type.simple">[dcl.type.simple]</a>
1114
-
1115
- | Specifier(s) | Type |
1116
- | ---------------------------- | ------------------------------------------------- |
1117
- | *type-name* | the type named |
1118
- | *simple-template-id* | the type as defined in~ [[temp.names]] |
1119
- | *decltype-specifier* | the type as defined in~ [[dcl.type.decltype]] |
1120
- | *placeholder-type-specifier* | the type as defined in~ [[dcl.spec.auto]] |
1121
- | *template-name* | the type as defined in~ [[dcl.type.class.deduct]] |
1122
- | `char` | ```char`'' |
1123
- | `unsigned char` | ```unsigned char`'' |
1124
- | `signed char` | ```signed char`'' |
1125
- | `char8_t` | ```char8_t`'' |
1126
- | `char16_t` | ```char16_t`'' |
1127
- | `char32_t` | ```char32_t`'' |
1128
- | `bool` | ```bool`'' |
1129
- | `unsigned` | ```unsigned int`'' |
1130
- | `unsigned int` | ```unsigned int`'' |
1131
- | `signed` | ```int`'' |
1132
- | `signed int` | ```int`'' |
1133
- | `int` | ```int`'' |
1134
- | `unsigned short int` | ```unsigned short int`'' |
1135
- | `unsigned short` | ```unsigned short int`'' |
1136
- | `unsigned long int` | ```unsigned long int`'' |
1137
- | `unsigned long` | ```unsigned long int`'' |
1138
- | `unsigned long long int` | ```unsigned long long int`'' |
1139
- | `unsigned long long` | ```unsigned long long int`'' |
1140
- | `signed long int` | ```long int`'' |
1141
- | `signed long` | ```long int`'' |
1142
- | `signed long long int` | ```long long int`'' |
1143
- | `signed long long` | ```long long int`'' |
1144
- | `long long int` | ```long long int`'' |
1145
- | `long long` | ```long long int`'' |
1146
- | `long int` | ```long int`'' |
1147
- | `long` | ```long int`'' |
1148
- | `signed short int` | ```short int`'' |
1149
- | `signed short` | ```short int`'' |
1150
- | `short int` | ```short int`'' |
1151
- | `short` | ```short int`'' |
1152
- | `wchar_t` | ```wchar_t`'' |
1153
- | `float` | ```float`'' |
1154
- | `double` | ```double`'' |
1155
- | `long double` | ```long double`'' |
1156
- | `void` | ```void`'' |
1157
-
1158
-
1159
- When multiple *simple-type-specifier*s are allowed, they can be freely
1160
- intermixed with other *decl-specifier*s in any order.
1161
-
1162
- [*Note 2*: It is *implementation-defined* whether objects of `char`
1163
- type are represented as signed or unsigned quantities. The `signed`
1164
- specifier forces `char` objects to be signed; it is redundant in other
1165
- contexts. — *end note*]
1166
-
1167
- #### Elaborated type specifiers <a id="dcl.type.elab">[[dcl.type.elab]]</a>
1168
-
1169
- ``` bnf
1170
- elaborated-type-specifier:
1171
- class-key attribute-specifier-seqₒₚₜ nested-name-specifierₒₚₜ identifier
1172
- class-key simple-template-id
1173
- class-key nested-name-specifier templateₒₚₜ simple-template-id
1174
- enum nested-name-specifierₒₚₜ identifier
1175
- ```
1176
-
1177
- The component names of an *elaborated-type-specifier* are its
1178
- *identifier* (if any) and those of its *nested-name-specifier* and
1179
- *simple-template-id* (if any).
1180
-
1181
- If an *elaborated-type-specifier* is the sole constituent of a
1182
- declaration, the declaration is ill-formed unless it is an explicit
1183
- specialization [[temp.expl.spec]], an explicit instantiation
1184
- [[temp.explicit]] or it has one of the following forms:
1185
-
1186
- ``` bnf
1187
- class-key attribute-specifier-seqₒₚₜ identifier ';'
1188
- class-key attribute-specifier-seqₒₚₜ simple-template-id ';'
1189
- ```
1190
-
1191
- In the first case, the *elaborated-type-specifier* declares the
1192
- *identifier* as a *class-name*. The second case shall appear only in an
1193
- *explicit-specialization* [[temp.expl.spec]] or in a
1194
- *template-declaration* (where it declares a partial specialization
1195
- [[temp.decls]]). The *attribute-specifier-seq*, if any, appertains to
1196
- the class or template being declared.
1197
-
1198
- Otherwise, an *elaborated-type-specifier* E shall not have an
1199
- *attribute-specifier-seq*. If E contains an *identifier* but no
1200
- *nested-name-specifier* and (unqualified) lookup for the *identifier*
1201
- finds nothing, E shall not be introduced by the `enum` keyword and
1202
- declares the *identifier* as a *class-name*. The target scope of E is
1203
- the nearest enclosing namespace or block scope.
1204
-
1205
- If an *elaborated-type-specifier* appears with the `friend` specifier as
1206
- an entire *member-declaration*, the *member-declaration* shall have one
1207
- of the following forms:
1208
-
1209
- ``` bnf
1210
- friend class-key nested-name-specifierₒₚₜ identifier ';'
1211
- friend class-key simple-template-id ';'
1212
- friend class-key nested-name-specifier templateₒₚₜ simple-template-id ';'
1213
- ```
1214
-
1215
- Any unqualified lookup for the *identifier* (in the first case) does not
1216
- consider scopes that contain the target scope; no name is bound.
1217
-
1218
- [*Note 1*: A *using-directive* in the target scope is ignored if it
1219
- refers to a namespace not contained by that scope. [[basic.lookup.elab]]
1220
- describes how name lookup proceeds in an
1221
- *elaborated-type-specifier*. — *end note*]
1222
-
1223
- [*Note 2*: An *elaborated-type-specifier* can be used to refer to a
1224
- previously declared *class-name* or *enum-name* even if the name has
1225
- been hidden by a non-type declaration. — *end note*]
1226
-
1227
- If the *identifier* or *simple-template-id* resolves to a *class-name*
1228
- or *enum-name*, the *elaborated-type-specifier* introduces it into the
1229
- declaration the same way a *simple-type-specifier* introduces its
1230
- *type-name* [[dcl.type.simple]]. If the *identifier* or
1231
- *simple-template-id* resolves to a *typedef-name*
1232
- [[dcl.typedef]], [[temp.names]], the *elaborated-type-specifier* is
1233
- ill-formed.
1234
-
1235
- [*Note 3*:
1236
-
1237
- This implies that, within a class template with a template
1238
- *type-parameter* `T`, the declaration
1239
-
1240
- ``` cpp
1241
- friend class T;
1242
- ```
1243
-
1244
- is ill-formed. However, the similar declaration `friend T;` is allowed
1245
- [[class.friend]].
1246
-
1247
- — *end note*]
1248
-
1249
- The *class-key* or `enum` keyword present in the
1250
- *elaborated-type-specifier* shall agree in kind with the declaration to
1251
- which the name in the *elaborated-type-specifier* refers. This rule also
1252
- applies to the form of *elaborated-type-specifier* that declares a
1253
- *class-name* or friend class since it can be construed as referring to
1254
- the definition of the class. Thus, in any *elaborated-type-specifier*,
1255
- the `enum` keyword shall be used to refer to an enumeration
1256
- [[dcl.enum]], the `union` *class-key* shall be used to refer to a union
1257
- [[class.union]], and either the `class` or `struct` *class-key* shall be
1258
- used to refer to a non-union class [[class.pre]].
1259
-
1260
- [*Example 1*:
1261
-
1262
- ``` cpp
1263
- enum class E { a, b };
1264
- enum E x = E::a; // OK
1265
- struct S { } s;
1266
- class S* p = &s; // OK
1267
- ```
1268
-
1269
- — *end example*]
1270
-
1271
- #### Decltype specifiers <a id="dcl.type.decltype">[[dcl.type.decltype]]</a>
1272
-
1273
- ``` bnf
1274
- decltype-specifier:
1275
- decltype '(' expression ')'
1276
- ```
1277
-
1278
- For an expression E, the type denoted by `decltype(E)` is defined as
1279
- follows:
1280
-
1281
- - if E is an unparenthesized *id-expression* naming a structured binding
1282
- [[dcl.struct.bind]], `decltype(E)` is the referenced type as given in
1283
- the specification of the structured binding declaration;
1284
- - otherwise, if E is an unparenthesized *id-expression* naming a
1285
- non-type *template-parameter* [[temp.param]], `decltype(E)` is the
1286
- type of the *template-parameter* after performing any necessary type
1287
- deduction [[dcl.spec.auto]], [[dcl.type.class.deduct]];
1288
- - otherwise, if E is an unparenthesized *id-expression* or an
1289
- unparenthesized class member access [[expr.ref]], `decltype(E)` is the
1290
- type of the entity named by E. If there is no such entity, the program
1291
- is ill-formed;
1292
- - otherwise, if E is an xvalue, `decltype(E)` is `T&&`, where `T` is the
1293
- type of E;
1294
- - otherwise, if E is an lvalue, `decltype(E)` is `T&`, where `T` is the
1295
- type of E;
1296
- - otherwise, `decltype(E)` is the type of E.
1297
-
1298
- The operand of the `decltype` specifier is an unevaluated operand
1299
- [[term.unevaluated.operand]].
1300
-
1301
- [*Example 1*:
1302
-
1303
- ``` cpp
1304
- const int&& foo();
1305
- int i;
1306
- struct A { double x; };
1307
- const A* a = new A();
1308
- decltype(foo()) x1 = 17; // type is const int&&
1309
- decltype(i) x2; // type is int
1310
- decltype(a->x) x3; // type is double
1311
- decltype((a->x)) x4 = x3; // type is const double&
1312
- ```
1313
-
1314
- — *end example*]
1315
-
1316
- [*Note 1*: The rules for determining types involving `decltype(auto)`
1317
- are specified in  [[dcl.spec.auto]]. — *end note*]
1318
-
1319
- If the operand of a *decltype-specifier* is a prvalue and is not a
1320
- (possibly parenthesized) immediate invocation [[expr.const]], the
1321
- temporary materialization conversion is not applied [[conv.rval]] and no
1322
- result object is provided for the prvalue. The type of the prvalue may
1323
- be incomplete or an abstract class type.
1324
-
1325
- [*Note 2*: As a result, storage is not allocated for the prvalue and it
1326
- is not destroyed. Thus, a class type is not instantiated as a result of
1327
- being the type of a function call in this context. In this context, the
1328
- common purpose of writing the expression is merely to refer to its type.
1329
- In that sense, a *decltype-specifier* is analogous to a use of a
1330
- *typedef-name*, so the usual reasons for requiring a complete type do
1331
- not apply. In particular, it is not necessary to allocate storage for a
1332
- temporary object or to enforce the semantic constraints associated with
1333
- invoking the type’s destructor. — *end note*]
1334
-
1335
- [*Note 3*: Unlike the preceding rule, parentheses have no special
1336
- meaning in this context. — *end note*]
1337
-
1338
- [*Example 2*:
1339
-
1340
- ``` cpp
1341
- template<class T> struct A { ~A() = delete; };
1342
- template<class T> auto h()
1343
- -> A<T>;
1344
- template<class T> auto i(T) // identity
1345
- -> T;
1346
- template<class T> auto f(T) // #1
1347
- -> decltype(i(h<T>())); // forces completion of A<T> and implicitly uses A<T>::~A()
1348
- // for the temporary introduced by the use of h().
1349
- // (A temporary is not introduced as a result of the use of i().)
1350
- template<class T> auto f(T) // #2
1351
- -> void;
1352
- auto g() -> void {
1353
- f(42); // OK, calls #2. (#1 is not a viable candidate: type deduction
1354
- // fails[temp.deduct] because A<int>::~A() is implicitly used in its
1355
- // decltype-specifier)
1356
- }
1357
- template<class T> auto q(T)
1358
- -> decltype((h<T>())); // does not force completion of A<T>; A<T>::~A() is not implicitly
1359
- // used within the context of this decltype-specifier
1360
- void r() {
1361
- q(42); // error: deduction against q succeeds, so overload resolution selects
1362
- // the specialization ``q(T) -> decltype((h<T>()))'' with T=int;
1363
- // the return type is A<int>, so a temporary is introduced and its
1364
- // destructor is used, so the program is ill-formed
1365
- }
1366
- ```
1367
-
1368
- — *end example*]
1369
-
1370
- #### Placeholder type specifiers <a id="dcl.spec.auto">[[dcl.spec.auto]]</a>
1371
-
1372
- ##### General <a id="dcl.spec.auto.general">[[dcl.spec.auto.general]]</a>
1373
-
1374
- ``` bnf
1375
- placeholder-type-specifier:
1376
- type-constraintₒₚₜ auto
1377
- type-constraintₒₚₜ decltype '(' auto ')'
1378
- ```
1379
-
1380
- A *placeholder-type-specifier* designates a placeholder type that will
1381
- be replaced later by deduction from an initializer.
1382
-
1383
- A *placeholder-type-specifier* of the form *type-constraint*ₒₚₜ `auto`
1384
- can be used as a *decl-specifier* of the *decl-specifier-seq* of a
1385
- *parameter-declaration* of a function declaration or *lambda-expression*
1386
- and, if it is not the `auto` *type-specifier* introducing a
1387
- *trailing-return-type* (see below), is a *generic parameter type
1388
- placeholder* of the function declaration or *lambda-expression*.
1389
-
1390
- [*Note 1*: Having a generic parameter type placeholder signifies that
1391
- the function is an abbreviated function template [[dcl.fct]] or the
1392
- lambda is a generic lambda [[expr.prim.lambda]]. — *end note*]
1393
-
1394
- A placeholder type can appear with a function declarator in the
1395
- *decl-specifier-seq*, *type-specifier-seq*, *conversion-function-id*, or
1396
- *trailing-return-type*, in any context where such a declarator is valid.
1397
- If the function declarator includes a *trailing-return-type*
1398
- [[dcl.fct]], that *trailing-return-type* specifies the declared return
1399
- type of the function. Otherwise, the function declarator shall declare a
1400
- function. If the declared return type of the function contains a
1401
- placeholder type, the return type of the function is deduced from
1402
- non-discarded `return` statements, if any, in the body of the function
1403
- [[stmt.if]].
1404
-
1405
- The type of a variable declared using a placeholder type is deduced from
1406
- its initializer. This use is allowed in an initializing declaration
1407
- [[dcl.init]] of a variable. The placeholder type shall appear as one of
1408
- the *decl-specifier*s in the *decl-specifier-seq* and the
1409
- *decl-specifier-seq* shall be followed by one or more *declarator*s,
1410
- each of which shall be followed by a non-empty *initializer*.
1411
-
1412
- [*Example 1*:
1413
-
1414
- ``` cpp
1415
- auto x = 5; // OK, x has type int
1416
- const auto *v = &x, u = 6; // OK, v has type const int*, u has type const int
1417
- static auto y = 0.0; // OK, y has type double
1418
- auto int r; // error: auto is not a storage-class-specifier
1419
- auto f() -> int; // OK, f returns int
1420
- auto g() { return 0.0; } // OK, g returns double
1421
- auto h(); // OK, h's return type will be deduced when it is defined
1422
- ```
1423
-
1424
- — *end example*]
1425
-
1426
- The `auto` *type-specifier* can also be used to introduce a structured
1427
- binding declaration [[dcl.struct.bind]].
1428
-
1429
- A placeholder type can also be used in the *type-specifier-seq* in the
1430
- *new-type-id* or *type-id* of a *new-expression* [[expr.new]] and as a
1431
- *decl-specifier* of the *parameter-declaration*'s *decl-specifier-seq*
1432
- in a *template-parameter* [[temp.param]]. The `auto` *type-specifier*
1433
- can also be used as the *simple-type-specifier* in an explicit type
1434
- conversion (functional notation) [[expr.type.conv]].
1435
-
1436
- A program that uses a placeholder type in a context not explicitly
1437
- allowed in [[dcl.spec.auto]] is ill-formed.
1438
-
1439
- If the *init-declarator-list* contains more than one *init-declarator*,
1440
- they shall all form declarations of variables. The type of each declared
1441
- variable is determined by placeholder type deduction
1442
- [[dcl.type.auto.deduct]], and if the type that replaces the placeholder
1443
- type is not the same in each deduction, the program is ill-formed.
1444
-
1445
- [*Example 2*:
1446
-
1447
- ``` cpp
1448
- auto x = 5, *y = &x; // OK, auto is int
1449
- auto a = 5, b = { 1, 2 }; // error: different types for auto
1450
- ```
1451
-
1452
- — *end example*]
1453
-
1454
- If a function with a declared return type that contains a placeholder
1455
- type has multiple non-discarded `return` statements, the return type is
1456
- deduced for each such `return` statement. If the type deduced is not the
1457
- same in each deduction, the program is ill-formed.
1458
-
1459
- If a function with a declared return type that uses a placeholder type
1460
- has no non-discarded `return` statements, the return type is deduced as
1461
- though from a `return` statement with no operand at the closing brace of
1462
- the function body.
1463
-
1464
- [*Example 3*:
1465
-
1466
- ``` cpp
1467
- auto f() { } // OK, return type is void
1468
- auto* g() { } // error: cannot deduce auto* from void()
1469
- ```
1470
-
1471
- — *end example*]
1472
-
1473
- An exported function with a declared return type that uses a placeholder
1474
- type shall be defined in the translation unit containing its exported
1475
- declaration, outside the *private-module-fragment* (if any).
1476
-
1477
- [*Note 2*: The deduced return type cannot have a name with internal
1478
- linkage [[basic.link]]. — *end note*]
1479
-
1480
- If a variable or function with an undeduced placeholder type is named by
1481
- an expression [[basic.def.odr]], the program is ill-formed. Once a
1482
- non-discarded `return` statement has been seen in a function, however,
1483
- the return type deduced from that statement can be used in the rest of
1484
- the function, including in other `return` statements.
1485
-
1486
- [*Example 4*:
1487
-
1488
- ``` cpp
1489
- auto n = n; // error: n's initializer refers to n
1490
- auto f();
1491
- void g() { &f; } // error: f's return type is unknown
1492
- auto sum(int i) {
1493
- if (i == 1)
1494
- return i; // sum's return type is int
1495
- else
1496
- return sum(i-1)+i; // OK, sum's return type has been deduced
1497
- }
1498
- ```
1499
-
1500
- — *end example*]
1501
-
1502
- Return type deduction for a templated function with a placeholder in its
1503
- declared type occurs when the definition is instantiated even if the
1504
- function body contains a `return` statement with a non-type-dependent
1505
- operand.
1506
-
1507
- [*Note 3*: Therefore, any use of a specialization of the function
1508
- template will cause an implicit instantiation. Any errors that arise
1509
- from this instantiation are not in the immediate context of the function
1510
- type and can result in the program being ill-formed
1511
- [[temp.deduct]]. — *end note*]
1512
-
1513
- [*Example 5*:
1514
-
1515
- ``` cpp
1516
- template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
1517
- typedef decltype(f(1)) fint_t; // instantiates f<int> to deduce return type
1518
- template<class T> auto f(T* t) { return *t; }
1519
- void g() { int (*p)(int*) = &f; } // instantiates both fs to determine return types,
1520
- // chooses second
1521
- ```
1522
-
1523
- — *end example*]
1524
-
1525
- If a function or function template F has a declared return type that
1526
- uses a placeholder type, redeclarations or specializations of F shall
1527
- use that placeholder type, not a deduced type; otherwise, they shall not
1528
- use a placeholder type.
1529
-
1530
- [*Example 6*:
1531
-
1532
- ``` cpp
1533
- auto f();
1534
- auto f() { return 42; } // return type is int
1535
- auto f(); // OK
1536
- int f(); // error: auto and int don't match
1537
- decltype(auto) f(); // error: auto and decltype(auto) don't match
1538
-
1539
- template <typename T> auto g(T t) { return t; } // #1
1540
- template auto g(int); // OK, return type is int
1541
- template char g(char); // error: no matching template
1542
- template<> auto g(double); // OK, forward declaration with unknown return type
1543
-
1544
- template <class T> T g(T t) { return t; } // OK, not functionally equivalent to #1
1545
- template char g(char); // OK, now there is a matching template
1546
- template auto g(float); // still matches #1
1547
-
1548
- void h() { return g(42); } // error: ambiguous
1549
-
1550
- template <typename T> struct A {
1551
- friend T frf(T);
1552
- };
1553
- auto frf(int i) { return i; } // not a friend of A<int>
1554
- extern int v;
1555
- auto v = 17; // OK, redeclares v
1556
- struct S {
1557
- static int i;
1558
- };
1559
- auto S::i = 23; // OK
1560
- ```
1561
-
1562
- — *end example*]
1563
-
1564
- A function declared with a return type that uses a placeholder type
1565
- shall not be `virtual` [[class.virtual]].
1566
-
1567
- A function declared with a return type that uses a placeholder type
1568
- shall not be a coroutine [[dcl.fct.def.coroutine]].
1569
-
1570
- An explicit instantiation declaration [[temp.explicit]] does not cause
1571
- the instantiation of an entity declared using a placeholder type, but it
1572
- also does not prevent that entity from being instantiated as needed to
1573
- determine its type.
1574
-
1575
- [*Example 7*:
1576
-
1577
- ``` cpp
1578
- template <typename T> auto f(T t) { return t; }
1579
- extern template auto f(int); // does not instantiate f<int>
1580
- int (*p)(int) = f; // instantiates f<int> to determine its return type, but an explicit
1581
- // instantiation definition is still required somewhere in the program
1582
- ```
1583
-
1584
- — *end example*]
1585
-
1586
- ##### Placeholder type deduction <a id="dcl.type.auto.deduct">[[dcl.type.auto.deduct]]</a>
1587
-
1588
- *Placeholder type deduction* is the process by which a type containing a
1589
- placeholder type is replaced by a deduced type.
1590
-
1591
- A type `T` containing a placeholder type, and a corresponding
1592
- *initializer-clause* E, are determined as follows:
1593
-
1594
- - For a non-discarded `return` statement that occurs in a function
1595
- declared with a return type that contains a placeholder type, `T` is
1596
- the declared return type.
1597
- - If the `return` statement has no operand, then E is `void()`.
1598
- - If the operand is a *braced-init-list* [[dcl.init.list]], the
1599
- program is ill-formed.
1600
- - If the operand is an *expression* X that is not an
1601
- *assignment-expression*, E is `(X)`. \[*Note 4*: A comma expression
1602
- [[expr.comma]] is not an *assignment-expression*. — *end note*]
1603
- - Otherwise, E is the operand of the `return` statement.
1604
-
1605
- If E has type `void`, `T` shall be either *type-constraint*ₒₚₜ
1606
- `decltype(auto)` or cv *type-constraint*ₒₚₜ `auto`.
1607
- - For a variable declared with a type that contains a placeholder type,
1608
- `T` is the declared type of the variable.
1609
- - If the initializer of the variable is a *brace-or-equal-initializer*
1610
- of the form `= initializer-clause`, E is the *initializer-clause*.
1611
- - If the initializer is a *braced-init-list*, it shall consist of a
1612
- single brace-enclosed *assignment-expression* and E is the
1613
- *assignment-expression*.
1614
- - If the initializer is a parenthesized *expression-list*, the
1615
- *expression-list* shall be a single *assignment-expression* and E is
1616
- the *assignment-expression*.
1617
- - For an explicit type conversion [[expr.type.conv]], `T` is the
1618
- specified type, which shall be `auto`.
1619
- - If the initializer is a *braced-init-list*, it shall consist of a
1620
- single brace-enclosed *assignment-expression* and E is the
1621
- *assignment-expression*.
1622
- - If the initializer is a parenthesized *expression-list*, the
1623
- *expression-list* shall be a single *assignment-expression* and E is
1624
- the *assignment-expression*.
1625
- - For a non-type template parameter declared with a type that contains a
1626
- placeholder type, `T` is the declared type of the non-type template
1627
- parameter and E is the corresponding template argument.
1628
-
1629
- `T` shall not be an array type.
1630
-
1631
- If the *placeholder-type-specifier* is of the form *type-constraint*ₒₚₜ
1632
- `auto`, the deduced type T' replacing `T` is determined using the rules
1633
- for template argument deduction. If the initialization is
1634
- copy-list-initialization, a declaration of `std::initializer_list` shall
1635
- precede [[basic.lookup.general]] the *placeholder-type-specifier*.
1636
- Obtain `P` from `T` by replacing the occurrences of
1637
- *type-constraint*ₒₚₜ `auto` either with a new invented type template
1638
- parameter `U` or, if the initialization is copy-list-initialization,
1639
- with `std::initializer_list<U>`. Deduce a value for `U` using the rules
1640
- of template argument deduction from a function call
1641
- [[temp.deduct.call]], where `P` is a function template parameter type
1642
- and the corresponding argument is E. If the deduction fails, the
1643
- declaration is ill-formed. Otherwise, T' is obtained by substituting the
1644
- deduced `U` into `P`.
1645
-
1646
- [*Example 8*:
1647
-
1648
- ``` cpp
1649
- auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int>
1650
- auto x2 = { 1, 2.0 }; // error: cannot deduce element type
1651
- auto x3{ 1, 2 }; // error: not a single element
1652
- auto x4 = { 3 }; // decltype(x4) is std::initializer_list<int>
1653
- auto x5{ 3 }; // decltype(x5) is int
1654
- ```
1655
-
1656
- — *end example*]
1657
-
1658
- [*Example 9*:
1659
-
1660
- ``` cpp
1661
- const auto &i = expr;
1662
- ```
1663
-
1664
- The type of `i` is the deduced type of the parameter `u` in the call
1665
- `f(expr)` of the following invented function template:
1666
-
1667
- ``` cpp
1668
- template <class U> void f(const U& u);
1669
- ```
1670
-
1671
- — *end example*]
1672
-
1673
- If the *placeholder-type-specifier* is of the form *type-constraint*ₒₚₜ
1674
- `decltype(auto)`, `T` shall be the placeholder alone. The type deduced
1675
- for `T` is determined as described in  [[dcl.type.decltype]], as though
1676
- E had been the operand of the `decltype`.
1677
-
1678
- [*Example 10*:
1679
-
1680
- ``` cpp
1681
- int i;
1682
- int&& f();
1683
- auto x2a(i); // decltype(x2a) is int
1684
- decltype(auto) x2d(i); // decltype(x2d) is int
1685
- auto x3a = i; // decltype(x3a) is int
1686
- decltype(auto) x3d = i; // decltype(x3d) is int
1687
- auto x4a = (i); // decltype(x4a) is int
1688
- decltype(auto) x4d = (i); // decltype(x4d) is int&
1689
- auto x5a = f(); // decltype(x5a) is int
1690
- decltype(auto) x5d = f(); // decltype(x5d) is int&&
1691
- auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
1692
- decltype(auto) x6d = { 1, 2 }; // error: { 1, 2 } is not an expression
1693
- auto *x7a = &i; // decltype(x7a) is int*
1694
- decltype(auto)*x7d = &i; // error: declared type is not plain decltype(auto)
1695
- auto f1(int x) -> decltype((x)) { return (x); } // return type is int&
1696
- auto f2(int x) -> decltype(auto) { return (x); } // return type is int&&
1697
- ```
1698
-
1699
- — *end example*]
1700
-
1701
- For a *placeholder-type-specifier* with a *type-constraint*, the
1702
- immediately-declared constraint [[temp.param]] of the *type-constraint*
1703
- for the type deduced for the placeholder shall be satisfied.
1704
-
1705
- #### Deduced class template specialization types <a id="dcl.type.class.deduct">[[dcl.type.class.deduct]]</a>
1706
-
1707
- If a placeholder for a deduced class type appears as a *decl-specifier*
1708
- in the *decl-specifier-seq* of an initializing declaration [[dcl.init]]
1709
- of a variable, the declared type of the variable shall be cv `T`, where
1710
- `T` is the placeholder.
1711
-
1712
- [*Example 1*:
1713
-
1714
- ``` cpp
1715
- template <class ...T> struct A {
1716
- A(T...) {}
1717
- };
1718
- A x[29]{}; // error: no declarator operators allowed
1719
- const A& y{}; // error: no declarator operators allowed
1720
- ```
1721
-
1722
- — *end example*]
1723
-
1724
- The placeholder is replaced by the return type of the function selected
1725
- by overload resolution for class template deduction
1726
- [[over.match.class.deduct]]. If the *decl-specifier-seq* is followed by
1727
- an *init-declarator-list* or *member-declarator-list* containing more
1728
- than one *declarator*, the type that replaces the placeholder shall be
1729
- the same in each deduction.
1730
-
1731
- A placeholder for a deduced class type can also be used in the
1732
- *type-specifier-seq* in the *new-type-id* or *type-id* of a
1733
- *new-expression* [[expr.new]], as the *simple-type-specifier* in an
1734
- explicit type conversion (functional notation) [[expr.type.conv]], or as
1735
- the *type-specifier* in the *parameter-declaration* of a
1736
- *template-parameter* [[temp.param]]. A placeholder for a deduced class
1737
- type shall not appear in any other context.
1738
-
1739
- [*Example 2*:
1740
-
1741
- ``` cpp
1742
- template<class T> struct container {
1743
- container(T t) {}
1744
- template<class Iter> container(Iter beg, Iter end);
1745
- };
1746
- template<class Iter>
1747
- container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>;
1748
- std::vector<double> v = { ... };
1749
-
1750
- container c(7); // OK, deduces int for T
1751
- auto d = container(v.begin(), v.end()); // OK, deduces double for T
1752
- container e{5, 6}; // error: int is not an iterator
1753
- ```
1754
-
1755
- — *end example*]
1756
-
1757
- ## Declarators <a id="dcl.decl">[[dcl.decl]]</a>
1758
-
1759
- ### General <a id="dcl.decl.general">[[dcl.decl.general]]</a>
1760
-
1761
- A declarator declares a single variable, function, or type, within a
1762
- declaration. The *init-declarator-list* appearing in a
1763
- *simple-declaration* is a comma-separated sequence of declarators, each
1764
- of which can have an initializer.
1765
-
1766
- ``` bnf
1767
- init-declarator-list:
1768
- init-declarator
1769
- init-declarator-list ',' init-declarator
1770
- ```
1771
-
1772
- ``` bnf
1773
- init-declarator:
1774
- declarator initializerₒₚₜ
1775
- declarator requires-clause
1776
- ```
1777
-
1778
- In all contexts, a *declarator* is interpreted as given below. Where an
1779
- *abstract-declarator* can be used (or omitted) in place of a
1780
- *declarator* [[dcl.fct]], [[except.pre]], it is as if a unique
1781
- identifier were included in the appropriate place [[dcl.name]]. The
1782
- preceding specifiers indicate the type, storage class or other
1783
- properties of the entity or entities being declared. Each declarator
1784
- specifies one entity and (optionally) names it and/or modifies the type
1785
- of the specifiers with operators such as `*` (pointer to) and `()`
1786
- (function returning).
1787
-
1788
- [*Note 1*: An *init-declarator* can also specify an initializer
1789
- [[dcl.init]]. — *end note*]
1790
-
1791
- Each *init-declarator* or *member-declarator* in a declaration is
1792
- analyzed separately as if it were in a declaration by itself.
1793
-
1794
- [*Note 2*:
1795
-
1796
- A declaration with several declarators is usually equivalent to the
1797
- corresponding sequence of declarations each with a single declarator.
1798
- That is,
1799
-
1800
- ``` cpp
1801
- T D1, D2, ... Dn;
1802
- ```
1803
-
1804
- is usually equivalent to
1805
-
1806
- ``` cpp
1807
- T D1; T D2; ... T Dn;
1808
- ```
1809
-
1810
- where `T` is a *decl-specifier-seq* and each `Di` is an
1811
- *init-declarator* or *member-declarator*. One exception is when a name
1812
- introduced by one of the *declarator*s hides a type name used by the
1813
- *decl-specifier*s, so that when the same *decl-specifier*s are used in a
1814
- subsequent declaration, they do not have the same meaning, as in
1815
-
1816
- ``` cpp
1817
- struct S { ... };
1818
- S S, T; // declare two instances of struct S
1819
- ```
1820
-
1821
- which is not equivalent to
1822
-
1823
- ``` cpp
1824
- struct S { ... };
1825
- S S;
1826
- S T; // error
1827
- ```
1828
-
1829
- Another exception is when `T` is `auto` [[dcl.spec.auto]], for example:
1830
-
1831
- ``` cpp
1832
- auto i = 1, j = 2.0; // error: deduced types for i and j do not match
1833
- ```
1834
-
1835
- as opposed to
1836
-
1837
- ``` cpp
1838
- auto i = 1; // OK, i deduced to have type int
1839
- auto j = 2.0; // OK, j deduced to have type double
1840
- ```
1841
-
1842
- — *end note*]
1843
-
1844
- The optional *requires-clause* in an *init-declarator* or
1845
- *member-declarator* shall be present only if the declarator declares a
1846
- templated function [[temp.pre]]. When present after a declarator, the
1847
- *requires-clause* is called the *trailing *requires-clause**. The
1848
- trailing *requires-clause* introduces the *constraint-expression* that
1849
- results from interpreting its *constraint-logical-or-expression* as a
1850
- *constraint-expression*.
1851
-
1852
- [*Example 1*:
1853
-
1854
- ``` cpp
1855
- void f1(int a) requires true; // error: non-templated function
1856
- template<typename T>
1857
- auto f2(T a) -> bool requires true; // OK
1858
- template<typename T>
1859
- auto f3(T a) requires true -> bool; // error: requires-clause precedes trailing-return-type
1860
- void (*pf)() requires true; // error: constraint on a variable
1861
- void g(int (*)() requires true); // error: constraint on a parameter-declaration
1862
-
1863
- auto* p = new void(*)(char) requires true; // error: not a function declaration
1864
- ```
1865
-
1866
- — *end example*]
1867
-
1868
- Declarators have the syntax
1869
-
1870
- ``` bnf
1871
- declarator:
1872
- ptr-declarator
1873
- noptr-declarator parameters-and-qualifiers trailing-return-type
1874
- ```
1875
-
1876
- ``` bnf
1877
- ptr-declarator:
1878
- noptr-declarator
1879
- ptr-operator ptr-declarator
1880
- ```
1881
-
1882
- ``` bnf
1883
- noptr-declarator:
1884
- declarator-id attribute-specifier-seqₒₚₜ
1885
- noptr-declarator parameters-and-qualifiers
1886
- noptr-declarator '[' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
1887
- '(' ptr-declarator ')'
1888
- ```
1889
-
1890
- ``` bnf
1891
- parameters-and-qualifiers:
1892
- '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
1893
- ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
1894
- ```
1895
-
1896
- ``` bnf
1897
- trailing-return-type:
1898
- '->' type-id
1899
- ```
1900
-
1901
- ``` bnf
1902
- ptr-operator:
1903
- '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ
1904
- '&' attribute-specifier-seqₒₚₜ
1905
- '&&' attribute-specifier-seqₒₚₜ
1906
- nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ
1907
- ```
1908
-
1909
- ``` bnf
1910
- cv-qualifier-seq:
1911
- cv-qualifier cv-qualifier-seqₒₚₜ
1912
- ```
1913
-
1914
- ``` bnf
1915
- cv-qualifier:
1916
- const
1917
- volatile
1918
- ```
1919
-
1920
- ``` bnf
1921
- ref-qualifier:
1922
- '&'
1923
- '&&'
1924
- ```
1925
-
1926
- ``` bnf
1927
- declarator-id:
1928
- '...'ₒₚₜ id-expression
1929
- ```
1930
-
1931
- ### Type names <a id="dcl.name">[[dcl.name]]</a>
1932
-
1933
- To specify type conversions explicitly, and as an argument of `sizeof`,
1934
- `alignof`, `new`, or `typeid`, the name of a type shall be specified.
1935
- This can be done with a *type-id*, which is syntactically a declaration
1936
- for a variable or function of that type that omits the name of the
1937
- entity.
1938
-
1939
- ``` bnf
1940
- type-id:
1941
- type-specifier-seq abstract-declaratorₒₚₜ
1942
- ```
1943
-
1944
- ``` bnf
1945
- defining-type-id:
1946
- defining-type-specifier-seq abstract-declaratorₒₚₜ
1947
- ```
1948
-
1949
- ``` bnf
1950
- abstract-declarator:
1951
- ptr-abstract-declarator
1952
- noptr-abstract-declaratorₒₚₜ parameters-and-qualifiers trailing-return-type
1953
- abstract-pack-declarator
1954
- ```
1955
-
1956
- ``` bnf
1957
- ptr-abstract-declarator:
1958
- noptr-abstract-declarator
1959
- ptr-operator ptr-abstract-declaratorₒₚₜ
1960
- ```
1961
-
1962
- ``` bnf
1963
- noptr-abstract-declarator:
1964
- noptr-abstract-declaratorₒₚₜ parameters-and-qualifiers
1965
- noptr-abstract-declaratorₒₚₜ '[' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
1966
- '(' ptr-abstract-declarator ')'
1967
- ```
1968
-
1969
- ``` bnf
1970
- abstract-pack-declarator:
1971
- noptr-abstract-pack-declarator
1972
- ptr-operator abstract-pack-declarator
1973
- ```
1974
-
1975
- ``` bnf
1976
- noptr-abstract-pack-declarator:
1977
- noptr-abstract-pack-declarator parameters-and-qualifiers
1978
- noptr-abstract-pack-declarator '[' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
1979
- '...'
1980
- ```
1981
-
1982
- It is possible to identify uniquely the location in the
1983
- *abstract-declarator* where the identifier would appear if the
1984
- construction were a declarator in a declaration. The named type is then
1985
- the same as the type of the hypothetical identifier.
1986
-
1987
- [*Example 1*:
1988
-
1989
- ``` cpp
1990
- int // int i
1991
- int * // int *pi
1992
- int *[3] // int *p[3]
1993
- int (*)[3] // int (*p3i)[3]
1994
- int *() // int *f()
1995
- int (*)(double) // int (*pf)(double)
1996
- ```
1997
-
1998
- name respectively the types “`int`”, “pointer to `int`”, “array of 3
1999
- pointers to `int`”, “pointer to array of 3 `int`”, “function of (no
2000
- parameters) returning pointer to `int`”, and “pointer to a function of
2001
- (`double`) returning `int`”.
2002
-
2003
- — *end example*]
2004
-
2005
- A type can also be named (often more easily) by using a `typedef`
2006
- [[dcl.typedef]].
2007
-
2008
- ### Ambiguity resolution <a id="dcl.ambig.res">[[dcl.ambig.res]]</a>
2009
-
2010
- The ambiguity arising from the similarity between a function-style cast
2011
- and a declaration mentioned in  [[stmt.ambig]] can also occur in the
2012
- context of a declaration. In that context, the choice is between an
2013
- object declaration with a function-style cast as the initializer and a
2014
- declaration involving a function declarator with a redundant set of
2015
- parentheses around a parameter name. Just as for the ambiguities
2016
- mentioned in  [[stmt.ambig]], the resolution is to consider any
2017
- construct, such as the potential parameter declaration, that could
2018
- possibly be a declaration to be a declaration.
2019
-
2020
- [*Note 1*: A declaration can be explicitly disambiguated by adding
2021
- parentheses around the argument. The ambiguity can be avoided by use of
2022
- copy-initialization or list-initialization syntax, or by use of a
2023
- non-function-style cast. — *end note*]
2024
-
2025
- [*Example 1*:
2026
-
2027
- ``` cpp
2028
- struct S {
2029
- S(int);
2030
- };
2031
-
2032
- void foo(double a) {
2033
- S w(int(a)); // function declaration
2034
- S x(int()); // function declaration
2035
- S y((int(a))); // object declaration
2036
- S y((int)a); // object declaration
2037
- S z = int(a); // object declaration
2038
- }
2039
- ```
2040
-
2041
- — *end example*]
2042
-
2043
- An ambiguity can arise from the similarity between a function-style cast
2044
- and a *type-id*. The resolution is that any construct that could
2045
- possibly be a *type-id* in its syntactic context shall be considered a
2046
- *type-id*.
2047
-
2048
- [*Example 2*:
2049
-
2050
- ``` cpp
2051
- template <class T> struct X {};
2052
- template <int N> struct Y {};
2053
- X<int()> a; // type-id
2054
- X<int(1)> b; // expression (ill-formed)
2055
- Y<int()> c; // type-id (ill-formed)
2056
- Y<int(1)> d; // expression
2057
-
2058
- void foo(signed char a) {
2059
- sizeof(int()); // type-id (ill-formed)
2060
- sizeof(int(a)); // expression
2061
- sizeof(int(unsigned(a))); // type-id (ill-formed)
2062
-
2063
- (int())+1; // type-id (ill-formed)
2064
- (int(a))+1; // expression
2065
- (int(unsigned(a)))+1; // type-id (ill-formed)
2066
- }
2067
- ```
2068
-
2069
- — *end example*]
2070
-
2071
- Another ambiguity arises in a *parameter-declaration-clause* when a
2072
- *type-name* is nested in parentheses. In this case, the choice is
2073
- between the declaration of a parameter of type pointer to function and
2074
- the declaration of a parameter with redundant parentheses around the
2075
- *declarator-id*. The resolution is to consider the *type-name* as a
2076
- *simple-type-specifier* rather than a *declarator-id*.
2077
-
2078
- [*Example 3*:
2079
-
2080
- ``` cpp
2081
- class C { };
2082
- void f(int(C)) { } // void f(int(*fp)(C c)) { }
2083
- // not: void f(int C) { }
2084
-
2085
- int g(C);
2086
-
2087
- void foo() {
2088
- f(1); // error: cannot convert 1 to function pointer
2089
- f(g); // OK
2090
- }
2091
- ```
2092
-
2093
- For another example,
2094
-
2095
- ``` cpp
2096
- class C { };
2097
- void h(int *(C[10])); // void h(int *(*_fp)(C _parm[10]));
2098
- // not: void h(int *C[10]);
2099
- ```
2100
-
2101
- — *end example*]
2102
-
2103
- ### Meaning of declarators <a id="dcl.meaning">[[dcl.meaning]]</a>
2104
-
2105
- #### General <a id="dcl.meaning.general">[[dcl.meaning.general]]</a>
2106
-
2107
- A declarator contains exactly one *declarator-id*; it names the entity
2108
- that is declared. If the *unqualified-id* occurring in a *declarator-id*
2109
- is a *template-id*, the declarator shall appear in the *declaration* of
2110
- a *template-declaration* [[temp.decls]], *explicit-specialization*
2111
- [[temp.expl.spec]], or *explicit-instantiation* [[temp.explicit]].
2112
-
2113
- [*Note 1*: An *unqualified-id* that is not an *identifier* is used to
2114
- declare certain functions
2115
- [[class.conv.fct]], [[class.dtor]], [[over.oper]], [[over.literal]]. — *end note*]
2116
-
2117
- The optional *attribute-specifier-seq* following a *declarator-id*
2118
- appertains to the entity that is declared.
2119
-
2120
- If the declaration is a friend declaration:
2121
-
2122
- - The *declarator* does not bind a name.
2123
- - If the *id-expression* E in the *declarator-id* of the *declarator* is
2124
- a *qualified-id* or a *template-id*:
2125
- - If the friend declaration is not a template declaration, then in the
2126
- lookup for the terminal name of E:
2127
- - if the *unqualified-id* in E is a *template-id*, all function
2128
- declarations are discarded;
2129
- - otherwise, if the *declarator* corresponds [[basic.scope.scope]]
2130
- to any declaration found of a non-template function, all function
2131
- template declarations are discarded;
2132
- - each remaining function template is replaced with the
2133
- specialization chosen by deduction from the friend declaration
2134
- [[temp.deduct.decl]] or discarded if deduction fails.
2135
- - The *declarator* shall correspond to one or more declarations found
2136
- by the lookup; they shall all have the same target scope, and the
2137
- target scope of the *declarator* is that scope.
2138
- - Otherwise, the terminal name of E is not looked up. The declaration’s
2139
- target scope is the innermost enclosing namespace scope; if the
2140
- declaration is contained by a block scope, the declaration shall
2141
- correspond to a reachable [[module.reach]] declaration that inhabits
2142
- the innermost block scope.
2143
-
2144
- Otherwise:
2145
-
2146
- - If the *id-expression* in the *declarator-id* of the *declarator* is a
2147
- *qualified-id* Q, let S be its lookup context [[basic.lookup.qual]];
2148
- the declaration shall inhabit a namespace scope.
2149
- - Otherwise, let S be the entity associated with the scope inhabited by
2150
- the *declarator*.
2151
- - If the *declarator* declares an explicit instantiation or a partial or
2152
- explicit specialization, the *declarator* does not bind a name. If it
2153
- declares a class member, the terminal name of the *declarator-id* is
2154
- not looked up; otherwise, only those lookup results that are nominable
2155
- in S are considered when identifying any function template
2156
- specialization being declared [[temp.deduct.decl]].
2157
- \[*Example 1*:
2158
- ``` cpp
2159
- namespace N {
2160
- inline namespace O {
2161
- template<class T> void f(T); // #1
2162
- template<class T> void g(T) {}
2163
- }
2164
- namespace P {
2165
- template<class T> void f(T*); // #2, more specialized than #1
2166
- template<class> int g;
2167
- }
2168
- using P::f,P::g;
2169
- }
2170
- template<> void N::f(int*) {} // OK, #2 is not nominable
2171
- template void N::g(int); // error: lookup is ambiguous
2172
- ```
2173
-
2174
- — *end example*]
2175
- - Otherwise, the terminal name of the *declarator-id* is not looked up.
2176
- If it is a qualified name, the *declarator* shall correspond to one or
2177
- more declarations nominable in S; all the declarations shall have the
2178
- same target scope and the target scope of the *declarator* is that
2179
- scope.
2180
- \[*Example 2*:
2181
- ``` cpp
2182
- namespace Q {
2183
- namespace V {
2184
- void f();
2185
- }
2186
- void V::f() { ... } // OK
2187
- void V::g() { ... } // error: g() is not yet a member of V
2188
- namespace V {
2189
- void g();
2190
- }
2191
- }
2192
-
2193
- namespace R {
2194
- void Q::V::g() { ... } // error: R doesn't enclose Q
2195
- }
2196
- ```
2197
-
2198
- — *end example*]
2199
- - If the declaration inhabits a block scope S and declares a function
2200
- [[dcl.fct]] or uses the `extern` specifier, the declaration shall not
2201
- be attached to a named module [[module.unit]]; its target scope is the
2202
- innermost enclosing namespace scope, but the name is bound in S.
2203
- \[*Example 3*:
2204
- ``` cpp
2205
- namespace X {
2206
- void p() {
2207
- q(); // error: q not yet declared
2208
- extern void q(); // q is a member of namespace X
2209
- extern void r(); // r is a member of namespace X
2210
- }
2211
-
2212
- void middle() {
2213
- q(); // error: q not found
2214
- }
2215
-
2216
- void q() { ... } // definition of X::q
2217
- }
2218
-
2219
- void q() { ... } // some other, unrelated q
2220
- void X::r() { ... } // error: r cannot be declared by qualified-id
2221
- ```
2222
-
2223
- — *end example*]
2224
-
2225
- A `static`, `thread_local`, `extern`, `mutable`, `friend`, `inline`,
2226
- `virtual`, `constexpr`, `consteval`, `constinit`, or `typedef` specifier
2227
- or an *explicit-specifier* applies directly to each *declarator-id* in a
2228
- declaration; the type specified for each *declarator-id* depends on both
2229
- the *decl-specifier-seq* and its *declarator*.
2230
-
2231
- Thus, (for each *declarator*) a declaration has the form
2232
-
2233
- ``` cpp
2234
- T D
2235
- ```
2236
-
2237
- where `T` is of the form *attribute-specifier-seq*ₒₚₜ
2238
- *decl-specifier-seq* and `D` is a declarator. Following is a recursive
2239
- procedure for determining the type specified for the contained
2240
- *declarator-id* by such a declaration.
2241
-
2242
- First, the *decl-specifier-seq* determines a type. In a declaration
2243
-
2244
- ``` cpp
2245
- T D
2246
- ```
2247
-
2248
- the *decl-specifier-seq* `T` determines the type `T`.
2249
-
2250
- [*Example 4*:
2251
-
2252
- In the declaration
2253
-
2254
- ``` cpp
2255
- int unsigned i;
2256
- ```
2257
-
2258
- the type specifiers `int` `unsigned` determine the type “`unsigned int`”
2259
- [[dcl.type.simple]].
2260
-
2261
- — *end example*]
2262
-
2263
- In a declaration *attribute-specifier-seq*ₒₚₜ `T` `D` where `D` is an
2264
- unadorned name, the type of the declared entity is “`T`”.
2265
-
2266
- In a declaration `T` `D` where `D` has the form
2267
-
2268
- ``` bnf
2269
- '(' 'D1' ')'
2270
- ```
2271
-
2272
- the type of the contained *declarator-id* is the same as that of the
2273
- contained *declarator-id* in the declaration
2274
-
2275
- ``` cpp
2276
- T D1
2277
- ```
2278
-
2279
- Parentheses do not alter the type of the embedded *declarator-id*, but
2280
- they can alter the binding of complex declarators.
2281
-
2282
- #### Pointers <a id="dcl.ptr">[[dcl.ptr]]</a>
2283
-
2284
- In a declaration `T` `D` where `D` has the form
2285
-
2286
- ``` bnf
2287
- '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
2288
- ```
2289
-
2290
- and the type of the contained *declarator-id* in the declaration `T`
2291
- `D1` is “*derived-declarator-type-list* `T`”, the type of the
2292
- *declarator-id* in `D` is “*derived-declarator-type-list*
2293
- *cv-qualifier-seq* pointer to `T`”. The *cv-qualifier*s apply to the
2294
- pointer and not to the object pointed to. Similarly, the optional
2295
- *attribute-specifier-seq* [[dcl.attr.grammar]] appertains to the pointer
2296
- and not to the object pointed to.
2297
-
2298
- [*Example 1*:
2299
-
2300
- The declarations
2301
-
2302
- ``` cpp
2303
- const int ci = 10, *pc = &ci, *const cpc = pc, **ppc;
2304
- int i, *p, *const cp = &i;
2305
- ```
2306
-
2307
- declare `ci`, a constant integer; `pc`, a pointer to a constant integer;
2308
- `cpc`, a constant pointer to a constant integer; `ppc`, a pointer to a
2309
- pointer to a constant integer; `i`, an integer; `p`, a pointer to
2310
- integer; and `cp`, a constant pointer to integer. The value of `ci`,
2311
- `cpc`, and `cp` cannot be changed after initialization. The value of
2312
- `pc` can be changed, and so can the object pointed to by `cp`. Examples
2313
- of some correct operations are
2314
-
2315
- ``` cpp
2316
- i = ci;
2317
- *cp = ci;
2318
- pc++;
2319
- pc = cpc;
2320
- pc = p;
2321
- ppc = &pc;
2322
- ```
2323
-
2324
- Examples of ill-formed operations are
2325
-
2326
- ``` cpp
2327
- ci = 1; // error
2328
- ci++; // error
2329
- *pc = 2; // error
2330
- cp = &ci; // error
2331
- cpc++; // error
2332
- p = pc; // error
2333
- ppc = &p; // error
2334
- ```
2335
-
2336
- Each is unacceptable because it would either change the value of an
2337
- object declared `const` or allow it to be changed through a
2338
- cv-unqualified pointer later, for example:
2339
-
2340
- ``` cpp
2341
- *ppc = &ci; // OK, but would make p point to ci because of previous error
2342
- *p = 5; // clobber ci
2343
- ```
2344
-
2345
- — *end example*]
2346
-
2347
- See also  [[expr.ass]] and  [[dcl.init]].
2348
-
2349
- [*Note 1*: Forming a pointer to reference type is ill-formed; see 
2350
- [[dcl.ref]]. Forming a function pointer type is ill-formed if the
2351
- function type has *cv-qualifier*s or a *ref-qualifier*; see 
2352
- [[dcl.fct]]. Since the address of a bit-field [[class.bit]] cannot be
2353
- taken, a pointer can never point to a bit-field. — *end note*]
2354
-
2355
- #### References <a id="dcl.ref">[[dcl.ref]]</a>
2356
-
2357
- In a declaration `T` `D` where `D` has either of the forms
2358
-
2359
- ``` bnf
2360
- '&' attribute-specifier-seqₒₚₜ 'D1'
2361
- '&&' attribute-specifier-seqₒₚₜ 'D1'
2362
- ```
2363
-
2364
- and the type of the contained *declarator-id* in the declaration `T`
2365
- `D1` is “*derived-declarator-type-list* `T`”, the type of the
2366
- *declarator-id* in `D` is “*derived-declarator-type-list* reference to
2367
- `T`”. The optional *attribute-specifier-seq* appertains to the reference
2368
- type. Cv-qualified references are ill-formed except when the
2369
- cv-qualifiers are introduced through the use of a *typedef-name*
2370
- [[dcl.typedef]], [[temp.param]] or *decltype-specifier*
2371
- [[dcl.type.decltype]], in which case the cv-qualifiers are ignored.
2372
-
2373
- [*Example 1*:
2374
-
2375
- ``` cpp
2376
- typedef int& A;
2377
- const A aref = 3; // error: lvalue reference to non-const initialized with rvalue
2378
- ```
2379
-
2380
- The type of `aref` is “lvalue reference to `int`”, not “lvalue reference
2381
- to `const int`”.
2382
-
2383
- — *end example*]
2384
-
2385
- [*Note 1*: A reference can be thought of as a name of an
2386
- object. — *end note*]
2387
-
2388
- A declarator that specifies the type “reference to cv `void`” is
2389
- ill-formed.
2390
-
2391
- A reference type that is declared using `&` is called an *lvalue
2392
- reference*, and a reference type that is declared using `&&` is called
2393
- an *rvalue reference*. Lvalue references and rvalue references are
2394
- distinct types. Except where explicitly noted, they are semantically
2395
- equivalent and commonly referred to as references.
2396
-
2397
- [*Example 2*:
2398
-
2399
- ``` cpp
2400
- void f(double& a) { a += 3.14; }
2401
- // ...
2402
- double d = 0;
2403
- f(d);
2404
- ```
2405
-
2406
- declares `a` to be a reference parameter of `f` so the call `f(d)` will
2407
- add `3.14` to `d`.
2408
-
2409
- ``` cpp
2410
- int v[20];
2411
- // ...
2412
- int& g(int i) { return v[i]; }
2413
- // ...
2414
- g(3) = 7;
2415
- ```
2416
-
2417
- declares the function `g()` to return a reference to an integer so
2418
- `g(3)=7` will assign `7` to the fourth element of the array `v`. For
2419
- another example,
2420
-
2421
- ``` cpp
2422
- struct link {
2423
- link* next;
2424
- };
2425
-
2426
- link* first;
2427
-
2428
- void h(link*& p) { // p is a reference to pointer
2429
- p->next = first;
2430
- first = p;
2431
- p = 0;
2432
- }
2433
-
2434
- void k() {
2435
- link* q = new link;
2436
- h(q);
2437
- }
2438
- ```
2439
-
2440
- declares `p` to be a reference to a pointer to `link` so `h(q)` will
2441
- leave `q` with the value zero. See also  [[dcl.init.ref]].
2442
-
2443
- — *end example*]
2444
-
2445
- It is unspecified whether or not a reference requires storage
2446
- [[basic.stc]].
2447
-
2448
- There shall be no references to references, no arrays of references, and
2449
- no pointers to references. The declaration of a reference shall contain
2450
- an *initializer* [[dcl.init.ref]] except when the declaration contains
2451
- an explicit `extern` specifier [[dcl.stc]], is a class member
2452
- [[class.mem]] declaration within a class definition, or is the
2453
- declaration of a parameter or a return type [[dcl.fct]]; see 
2454
- [[basic.def]]. A reference shall be initialized to refer to a valid
2455
- object or function.
2456
-
2457
- [*Note 2*: In particular, a null reference cannot exist in a
2458
- well-defined program, because the only way to create such a reference
2459
- would be to bind it to the “object” obtained by indirection through a
2460
- null pointer, which causes undefined behavior. As described in 
2461
- [[class.bit]], a reference cannot be bound directly to a
2462
- bit-field. — *end note*]
2463
-
2464
- If a *typedef-name* [[dcl.typedef]], [[temp.param]] or a
2465
- *decltype-specifier* [[dcl.type.decltype]] denotes a type `TR` that is a
2466
- reference to a type `T`, an attempt to create the type “lvalue reference
2467
- to cv `TR`” creates the type “lvalue reference to `T`”, while an attempt
2468
- to create the type “rvalue reference to cv `TR`” creates the type `TR`.
2469
-
2470
- [*Note 3*: This rule is known as reference collapsing. — *end note*]
2471
-
2472
- [*Example 3*:
2473
-
2474
- ``` cpp
2475
- int i;
2476
- typedef int& LRI;
2477
- typedef int&& RRI;
2478
-
2479
- LRI& r1 = i; // r1 has the type int&
2480
- const LRI& r2 = i; // r2 has the type int&
2481
- const LRI&& r3 = i; // r3 has the type int&
2482
-
2483
- RRI& r4 = i; // r4 has the type int&
2484
- RRI&& r5 = 5; // r5 has the type int&&
2485
-
2486
- decltype(r2)& r6 = i; // r6 has the type int&
2487
- decltype(r2)&& r7 = i; // r7 has the type int&
2488
- ```
2489
-
2490
- — *end example*]
2491
-
2492
- [*Note 4*: Forming a reference to function type is ill-formed if the
2493
- function type has *cv-qualifier*s or a *ref-qualifier*; see 
2494
- [[dcl.fct]]. — *end note*]
2495
-
2496
- #### Pointers to members <a id="dcl.mptr">[[dcl.mptr]]</a>
2497
-
2498
- The component names of a *ptr-operator* are those of its
2499
- *nested-name-specifier*, if any.
2500
-
2501
- In a declaration `T` `D` where `D` has the form
2502
-
2503
- ``` bnf
2504
- nested-name-specifier '*' attribute-specifier-seqₒₚₜ cv-qualifier-seqₒₚₜ 'D1'
2505
- ```
2506
-
2507
- and the *nested-name-specifier* denotes a class, and the type of the
2508
- contained *declarator-id* in the declaration `T` `D1` is
2509
- “*derived-declarator-type-list* `T`”, the type of the *declarator-id* in
2510
- `D` is “*derived-declarator-type-list* *cv-qualifier-seq* pointer to
2511
- member of class *nested-name-specifier* of type `T`”. The optional
2512
- *attribute-specifier-seq* [[dcl.attr.grammar]] appertains to the
2513
- pointer-to-member.
2514
-
2515
- [*Example 1*:
2516
-
2517
- ``` cpp
2518
- struct X {
2519
- void f(int);
2520
- int a;
2521
- };
2522
- struct Y;
2523
-
2524
- int X::* pmi = &X::a;
2525
- void (X::* pmf)(int) = &X::f;
2526
- double X::* pmd;
2527
- char Y::* pmc;
2528
- ```
2529
-
2530
- declares `pmi`, `pmf`, `pmd` and `pmc` to be a pointer to a member of
2531
- `X` of type `int`, a pointer to a member of `X` of type `void(int)`, a
2532
- pointer to a member of `X` of type `double` and a pointer to a member of
2533
- `Y` of type `char` respectively. The declaration of `pmd` is well-formed
2534
- even though `X` has no members of type `double`. Similarly, the
2535
- declaration of `pmc` is well-formed even though `Y` is an incomplete
2536
- type. `pmi` and `pmf` can be used like this:
2537
-
2538
- ``` cpp
2539
- X obj;
2540
- // ...
2541
- obj.*pmi = 7; // assign 7 to an integer member of obj
2542
- (obj.*pmf)(7); // call a function member of obj with the argument 7
2543
- ```
2544
-
2545
- — *end example*]
2546
-
2547
- A pointer to member shall not point to a static member of a class
2548
- [[class.static]], a member with reference type, or “cv `void`”.
2549
-
2550
- [*Note 1*: See also  [[expr.unary]] and  [[expr.mptr.oper]]. The type
2551
- “pointer to member” is distinct from the type “pointer”, that is, a
2552
- pointer to member is declared only by the pointer-to-member declarator
2553
- syntax, and never by the pointer declarator syntax. There is no
2554
- “reference-to-member” type in C++. — *end note*]
2555
-
2556
- #### Arrays <a id="dcl.array">[[dcl.array]]</a>
2557
-
2558
- In a declaration `T` `D` where `D` has the form
2559
-
2560
- ``` bnf
2561
- 'D1' '[' constant-expressionₒₚₜ ']' attribute-specifier-seqₒₚₜ
2562
- ```
2563
-
2564
- and the type of the contained *declarator-id* in the declaration `T`
2565
- `D1` is “*derived-declarator-type-list* `T`”, the type of the
2566
- *declarator-id* in `D` is “*derived-declarator-type-list* array of `N`
2567
- `T`”. The *constant-expression* shall be a converted constant expression
2568
- of type `std::size_t` [[expr.const]]. Its value `N` specifies the *array
2569
- bound*, i.e., the number of elements in the array; `N` shall be greater
2570
- than zero.
2571
-
2572
- In a declaration `T` `D` where `D` has the form
2573
-
2574
- ``` bnf
2575
- 'D1 [ ]' attribute-specifier-seqₒₚₜ
2576
- ```
2577
-
2578
- and the type of the contained *declarator-id* in the declaration `T`
2579
- `D1` is “*derived-declarator-type-list* `T`”, the type of the
2580
- *declarator-id* in `D` is “*derived-declarator-type-list* array of
2581
- unknown bound of `T`”, except as specified below.
2582
-
2583
- A type of the form “array of `N` `U`” or “array of unknown bound of `U`”
2584
- is an *array type*. The optional *attribute-specifier-seq* appertains to
2585
- the array type.
2586
-
2587
- `U` is called the array *element type*; this type shall not be a
2588
- reference type, a function type, an array of unknown bound, or
2589
- cv `void`.
2590
-
2591
- [*Note 1*: An array can be constructed from one of the fundamental
2592
- types (except `void`), from a pointer, from a pointer to member, from a
2593
- class, from an enumeration type, or from an array of known
2594
- bound. — *end note*]
2595
-
2596
- [*Example 1*:
2597
-
2598
- ``` cpp
2599
- float fa[17], *afp[17];
2600
- ```
2601
-
2602
- declares an array of `float` numbers and an array of pointers to `float`
2603
- numbers.
2604
-
2605
- — *end example*]
2606
-
2607
- Any type of the form “*cv-qualifier-seq* array of `N` `U`” is adjusted
2608
- to “array of `N` *cv-qualifier-seq* `U`”, and similarly for “array of
2609
- unknown bound of `U`”.
2610
-
2611
- [*Example 2*:
2612
-
2613
- ``` cpp
2614
- typedef int A[5], AA[2][3];
2615
- typedef const A CA; // type is ``array of 5 const int''
2616
- typedef const AA CAA; // type is ``array of 2 array of 3 const int''
2617
- ```
2618
-
2619
- — *end example*]
2620
-
2621
- [*Note 2*: An “array of `N` *cv-qualifier-seq* `U`” has cv-qualified
2622
- type; see  [[basic.type.qualifier]]. — *end note*]
2623
-
2624
- An object of type “array of `N` `U`” consists of a contiguously
2625
- allocated non-empty set of `N` subobjects of type `U`, known as the
2626
- *elements* of the array, and numbered `0` to `N-1`.
2627
-
2628
- In addition to declarations in which an incomplete object type is
2629
- allowed, an array bound may be omitted in some cases in the declaration
2630
- of a function parameter [[dcl.fct]]. An array bound may also be omitted
2631
- when an object (but not a non-static data member) of array type is
2632
- initialized and the declarator is followed by an initializer
2633
- [[dcl.init]], [[class.mem]], [[expr.type.conv]], [[expr.new]]. In these
2634
- cases, the array bound is calculated from the number of initial elements
2635
- (say, `N`) supplied [[dcl.init.aggr]], and the type of the array is
2636
- “array of `N` `U`”.
2637
-
2638
- Furthermore, if there is a reachable declaration of the entity that
2639
- inhabits the same scope in which the bound was specified, an omitted
2640
- array bound is taken to be the same as in that earlier declaration, and
2641
- similarly for the definition of a static data member of a class.
2642
-
2643
- [*Example 3*:
2644
-
2645
- ``` cpp
2646
- extern int x[10];
2647
- struct S {
2648
- static int y[10];
2649
- };
2650
-
2651
- int x[]; // OK, bound is 10
2652
- int S::y[]; // OK, bound is 10
2653
-
2654
- void f() {
2655
- extern int x[];
2656
- int i = sizeof(x); // error: incomplete object type
2657
- }
2658
- ```
2659
-
2660
- — *end example*]
2661
-
2662
- [*Note 3*:
2663
-
2664
- When several “array of” specifications are adjacent, a multidimensional
2665
- array type is created; only the first of the constant expressions that
2666
- specify the bounds of the arrays can be omitted.
2667
-
2668
- [*Example 4*:
2669
-
2670
- ``` cpp
2671
- int x3d[3][5][7];
2672
- ```
2673
-
2674
- declares an array of three elements, each of which is an array of five
2675
- elements, each of which is an array of seven integers. The overall array
2676
- can be viewed as a three-dimensional array of integers, with rank
2677
- 3 × 5 × 7. Any of the expressions `x3d`, `x3d[i]`, `x3d[i][j]`,
2678
- `x3d[i][j][k]` can reasonably appear in an expression. The expression
2679
- `x3d[i]` is equivalent to `*(x3d + i)`; in that expression, `x3d` is
2680
- subject to the array-to-pointer conversion [[conv.array]] and is first
2681
- converted to a pointer to a 2-dimensional array with rank 5 × 7 that
2682
- points to the first element of `x3d`. Then `i` is added, which on
2683
- typical implementations involves multiplying `i` by the length of the
2684
- object to which the pointer points, which is `sizeof(int)`× 5 × 7. The
2685
- result of the addition and indirection is an lvalue denoting the `i`ᵗʰ
2686
- array element of `x3d` (an array of five arrays of seven integers). If
2687
- there is another subscript, the same argument applies again, so
2688
- `x3d[i][j]` is an lvalue denoting the `j`ᵗʰ array element of the `i`ᵗʰ
2689
- array element of `x3d` (an array of seven integers), and `x3d[i][j][k]`
2690
- is an lvalue denoting the `k`ᵗʰ array element of the `j`ᵗʰ array element
2691
- of the `i`ᵗʰ array element of `x3d` (an integer).
2692
-
2693
- — *end example*]
2694
-
2695
- The first subscript in the declaration helps determine the amount of
2696
- storage consumed by an array but plays no other part in subscript
2697
- calculations.
2698
-
2699
- — *end note*]
2700
-
2701
- [*Note 4*: Conversions affecting expressions of array type are
2702
- described in  [[conv.array]]. — *end note*]
2703
-
2704
- [*Note 5*: The subscript operator can be overloaded for a class
2705
- [[over.sub]]. For the operator’s built-in meaning, see
2706
- [[expr.sub]]. — *end note*]
2707
-
2708
- #### Functions <a id="dcl.fct">[[dcl.fct]]</a>
2709
-
2710
- In a declaration `T` `D` where `D` has the form
2711
-
2712
- ``` bnf
2713
- 'D1' '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
2714
- ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
2715
- ```
2716
-
2717
- and the type of the contained *declarator-id* in the declaration `T`
2718
- `D1` is “*derived-declarator-type-list* `T`”, the type of the
2719
- *declarator-id* in `D` is “*derived-declarator-type-list* `noexcept`ₒₚₜ
2720
- function of parameter-type-list *cv-qualifier-seq*ₒₚₜ
2721
- *ref-qualifier*ₒₚₜ returning `T`”, where
2722
-
2723
- - the parameter-type-list is derived from the
2724
- *parameter-declaration-clause* as described below and
2725
- - the optional `noexcept` is present if and only if the exception
2726
- specification [[except.spec]] is non-throwing.
2727
-
2728
- The optional *attribute-specifier-seq* appertains to the function type.
2729
-
2730
- In a declaration `T` `D` where `D` has the form
2731
-
2732
- ``` bnf
2733
- 'D1' '(' parameter-declaration-clause ')' cv-qualifier-seqₒₚₜ
2734
- ref-qualifierₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-type
2735
- ```
2736
-
2737
- and the type of the contained *declarator-id* in the declaration `T`
2738
- `D1` is “*derived-declarator-type-list* `T`”, `T` shall be the single
2739
- *type-specifier* `auto`. The type of the *declarator-id* in `D` is
2740
- “*derived-declarator-type-list* `noexcept`ₒₚₜ function of
2741
- parameter-type-list *cv-qualifier-seq*ₒₚₜ *ref-qualifier*ₒₚₜ returning
2742
- `U`”, where
2743
-
2744
- - the parameter-type-list is derived from the
2745
- *parameter-declaration-clause* as described below,
2746
- - `U` is the type specified by the *trailing-return-type*, and
2747
- - the optional `noexcept` is present if and only if the exception
2748
- specification is non-throwing.
2749
-
2750
- The optional *attribute-specifier-seq* appertains to the function type.
2751
-
2752
- A type of either form is a *function type*.[^2]
2753
-
2754
- ``` bnf
2755
- parameter-declaration-clause:
2756
- parameter-declaration-listₒₚₜ '...'ₒₚₜ
2757
- parameter-declaration-list ',' '...'
2758
- ```
2759
-
2760
- ``` bnf
2761
- parameter-declaration-list:
2762
- parameter-declaration
2763
- parameter-declaration-list ',' parameter-declaration
2764
- ```
2765
-
2766
- ``` bnf
2767
- parameter-declaration:
2768
- attribute-specifier-seqₒₚₜ thisₒₚₜ decl-specifier-seq declarator
2769
- attribute-specifier-seqₒₚₜ decl-specifier-seq declarator '=' initializer-clause
2770
- attribute-specifier-seqₒₚₜ thisₒₚₜ decl-specifier-seq abstract-declaratorₒₚₜ
2771
- attribute-specifier-seqₒₚₜ decl-specifier-seq abstract-declaratorₒₚₜ '=' initializer-clause
2772
- ```
2773
-
2774
- The optional *attribute-specifier-seq* in a *parameter-declaration*
2775
- appertains to the parameter.
2776
-
2777
- The *parameter-declaration-clause* determines the arguments that can be
2778
- specified, and their processing, when the function is called.
2779
-
2780
- [*Note 1*: The *parameter-declaration-clause* is used to convert the
2781
- arguments specified on the function call; see 
2782
- [[expr.call]]. — *end note*]
2783
-
2784
- If the *parameter-declaration-clause* is empty, the function takes no
2785
- arguments. A parameter list consisting of a single unnamed parameter of
2786
- non-dependent type `void` is equivalent to an empty parameter list.
2787
- Except for this special case, a parameter shall not have type cv `void`.
2788
- A parameter with volatile-qualified type is deprecated; see 
2789
- [[depr.volatile.type]]. If the *parameter-declaration-clause* terminates
2790
- with an ellipsis or a function parameter pack [[temp.variadic]], the
2791
- number of arguments shall be equal to or greater than the number of
2792
- parameters that do not have a default argument and are not function
2793
- parameter packs. Where syntactically correct and where “`...`” is not
2794
- part of an *abstract-declarator*, “`, ...`” is synonymous with “`...`”.
2795
-
2796
- [*Example 1*:
2797
-
2798
- The declaration
2799
-
2800
- ``` cpp
2801
- int printf(const char*, ...);
2802
- ```
2803
-
2804
- declares a function that can be called with varying numbers and types of
2805
- arguments.
2806
-
2807
- ``` cpp
2808
- printf("hello world");
2809
- printf("a=%d b=%d", a, b);
2810
- ```
2811
-
2812
- However, the first argument must be of a type that can be converted to a
2813
- `const` `char*`.
2814
-
2815
- — *end example*]
2816
-
2817
- [*Note 2*: The standard header `<cstdarg>` contains a mechanism for
2818
- accessing arguments passed using the ellipsis (see  [[expr.call]] and 
2819
- [[support.runtime]]). — *end note*]
2820
-
2821
- The type of a function is determined using the following rules. The type
2822
- of each parameter (including function parameter packs) is determined
2823
- from its own *parameter-declaration* [[dcl.decl]]. After determining the
2824
- type of each parameter, any parameter of type “array of `T`” or of
2825
- function type `T` is adjusted to be “pointer to `T`”. After producing
2826
- the list of parameter types, any top-level *cv-qualifier*s modifying a
2827
- parameter type are deleted when forming the function type. The resulting
2828
- list of transformed parameter types and the presence or absence of the
2829
- ellipsis or a function parameter pack is the function’s
2830
- *parameter-type-list*.
2831
-
2832
- [*Note 3*: This transformation does not affect the types of the
2833
- parameters. For example, `int(*)(const int p, decltype(p)*)` and
2834
- `int(*)(int, const int*)` are identical types. — *end note*]
2835
-
2836
- [*Example 2*:
2837
-
2838
- ``` cpp
2839
- void f(char*); // #1
2840
- void f(char[]) {} // defines #1
2841
- void f(const char*) {} // OK, another overload
2842
- void f(char *const) {} // error: redefines #1
2843
-
2844
- void g(char(*)[2]); // #2
2845
- void g(char[3][2]) {} // defines #2
2846
- void g(char[3][3]) {} // OK, another overload
2847
-
2848
- void h(int x(const int)); // #3
2849
- void h(int (*)(int)) {} // defines #3
2850
- ```
2851
-
2852
- — *end example*]
2853
-
2854
- An *explicit-object-parameter-declaration* is a *parameter-declaration*
2855
- with a `this` specifier. An explicit-object-parameter-declaration shall
2856
- appear only as the first *parameter-declaration* of a
2857
- *parameter-declaration-list* of either:
2858
-
2859
- - a *member-declarator* that declares a member function [[class.mem]],
2860
- or
2861
- - a *lambda-declarator* [[expr.prim.lambda]].
2862
-
2863
- A *member-declarator* with an explicit-object-parameter-declaration
2864
- shall not include a *ref-qualifier* or a *cv-qualifier-seq* and shall
2865
- not be declared `static` or `virtual`.
2866
-
2867
- [*Example 3*:
2868
-
2869
- ``` cpp
2870
- struct C {
2871
- void f(this C& self);
2872
- template <typename Self> void g(this Self&& self, int);
2873
-
2874
- void h(this C) const; // error: const not allowed here
2875
- };
2876
-
2877
- void test(C c) {
2878
- c.f(); // OK, calls C::f
2879
- c.g(42); // OK, calls C::g<C&>
2880
- std::move(c).g(42); // OK, calls C::g<C>
2881
- }
2882
- ```
2883
-
2884
- — *end example*]
2885
-
2886
- A function parameter declared with an
2887
- explicit-object-parameter-declaration is an *explicit object parameter*.
2888
- An explicit object parameter shall not be a function parameter pack
2889
- [[temp.variadic]]. An *explicit object member function* is a non-static
2890
- member function with an explicit object parameter. An
2891
- *implicit object member function* is a non-static member function
2892
- without an explicit object parameter.
2893
-
2894
- The *object parameter* of a non-static member function is either the
2895
- explicit object parameter or the implicit object parameter
2896
- [[over.match.funcs]].
2897
-
2898
- A *non-object parameter* is a function parameter that is not the
2899
- explicit object parameter. The *non-object-parameter-type-list* of a
2900
- member function is the parameter-type-list of that function with the
2901
- explicit object parameter, if any, omitted.
2902
-
2903
- [*Note 4*: The non-object-parameter-type-list consists of the adjusted
2904
- types of all the non-object parameters. — *end note*]
2905
-
2906
- A function type with a *cv-qualifier-seq* or a *ref-qualifier*
2907
- (including a type named by *typedef-name*
2908
- [[dcl.typedef]], [[temp.param]]) shall appear only as:
2909
-
2910
- - the function type for a non-static member function,
2911
- - the function type to which a pointer to member refers,
2912
- - the top-level function type of a function typedef declaration or
2913
- *alias-declaration*,
2914
- - the *type-id* in the default argument of a *type-parameter*
2915
- [[temp.param]], or
2916
- - the *type-id* of a *template-argument* for a *type-parameter*
2917
- [[temp.arg.type]].
2918
-
2919
- [*Example 4*:
2920
-
2921
- ``` cpp
2922
- typedef int FIC(int) const;
2923
- FIC f; // error: does not declare a member function
2924
- struct S {
2925
- FIC f; // OK
2926
- };
2927
- FIC S::*pm = &S::f; // OK
2928
- ```
2929
-
2930
- — *end example*]
2931
-
2932
- The effect of a *cv-qualifier-seq* in a function declarator is not the
2933
- same as adding cv-qualification on top of the function type. In the
2934
- latter case, the cv-qualifiers are ignored.
2935
-
2936
- [*Note 5*: A function type that has a *cv-qualifier-seq* is not a
2937
- cv-qualified type; there are no cv-qualified function
2938
- types. — *end note*]
2939
-
2940
- [*Example 5*:
2941
-
2942
- ``` cpp
2943
- typedef void F();
2944
- struct S {
2945
- const F f; // OK, equivalent to: void f();
2946
- };
2947
- ```
2948
-
2949
- — *end example*]
2950
-
2951
- The return type, the parameter-type-list, the *ref-qualifier*, the
2952
- *cv-qualifier-seq*, and the exception specification, but not the default
2953
- arguments [[dcl.fct.default]] or the trailing *requires-clause*
2954
- [[dcl.decl]], are part of the function type.
2955
-
2956
- [*Note 6*: Function types are checked during the assignments and
2957
- initializations of pointers to functions, references to functions, and
2958
- pointers to member functions. — *end note*]
2959
-
2960
- [*Example 6*:
2961
-
2962
- The declaration
2963
-
2964
- ``` cpp
2965
- int fseek(FILE*, long, int);
2966
- ```
2967
-
2968
- declares a function taking three arguments of the specified types, and
2969
- returning `int` [[dcl.type]].
2970
-
2971
- — *end example*]
2972
-
2973
- [*Note 7*: A single name can be used for several different functions in
2974
- a single scope; this is function overloading [[over]]. — *end note*]
2975
-
2976
- The return type shall be a non-array object type, a reference type, or
2977
- cv `void`.
2978
-
2979
- [*Note 8*: An array of placeholder type is considered an array
2980
- type. — *end note*]
2981
-
2982
- A volatile-qualified return type is deprecated; see 
2983
- [[depr.volatile.type]].
2984
-
2985
- Types shall not be defined in return or parameter types.
2986
-
2987
- A typedef of function type may be used to declare a function but shall
2988
- not be used to define a function [[dcl.fct.def]].
2989
-
2990
- [*Example 7*:
2991
-
2992
- ``` cpp
2993
- typedef void F();
2994
- F fv; // OK, equivalent to void fv();
2995
- F fv { } // error
2996
- void fv() { } // OK, definition of fv
2997
- ```
2998
-
2999
- — *end example*]
3000
-
3001
- An identifier can optionally be provided as a parameter name; if present
3002
- in a function definition [[dcl.fct.def]], it names a parameter.
3003
-
3004
- [*Note 9*: In particular, parameter names are also optional in function
3005
- definitions and names used for a parameter in different declarations and
3006
- the definition of a function need not be the same. — *end note*]
3007
-
3008
- [*Example 8*:
3009
-
3010
- The declaration
3011
-
3012
- ``` cpp
3013
- int i,
3014
- *pi,
3015
- f(),
3016
- *fpi(int),
3017
- (*pif)(const char*, const char*),
3018
- (*fpif(int))(int);
3019
- ```
3020
-
3021
- declares an integer `i`, a pointer `pi` to an integer, a function `f`
3022
- taking no arguments and returning an integer, a function `fpi` taking an
3023
- integer argument and returning a pointer to an integer, a pointer `pif`
3024
- to a function which takes two pointers to constant characters and
3025
- returns an integer, a function `fpif` taking an integer argument and
3026
- returning a pointer to a function that takes an integer argument and
3027
- returns an integer. It is especially useful to compare `fpi` and `pif`.
3028
- The binding of `*fpi(int)` is `*(fpi(int))`, so the declaration
3029
- suggests, and the same construction in an expression requires, the
3030
- calling of a function `fpi`, and then using indirection through the
3031
- (pointer) result to yield an integer. In the declarator
3032
- `(*pif)(const char*, const char*)`, the extra parentheses are necessary
3033
- to indicate that indirection through a pointer to a function yields a
3034
- function, which is then called.
3035
-
3036
- — *end example*]
3037
-
3038
- [*Note 10*:
3039
-
3040
- Typedefs and *trailing-return-type*s are sometimes convenient when the
3041
- return type of a function is complex. For example, the function `fpif`
3042
- above can be declared
3043
-
3044
- ``` cpp
3045
- typedef int IFUNC(int);
3046
- IFUNC* fpif(int);
3047
- ```
3048
-
3049
- or
3050
-
3051
- ``` cpp
3052
- auto fpif(int)->int(*)(int);
3053
- ```
3054
-
3055
- A *trailing-return-type* is most useful for a type that would be more
3056
- complicated to specify before the *declarator-id*:
3057
-
3058
- ``` cpp
3059
- template <class T, class U> auto add(T t, U u) -> decltype(t + u);
3060
- ```
3061
-
3062
- rather than
3063
-
3064
- ``` cpp
3065
- template <class T, class U> decltype((*(T*)0) + (*(U*)0)) add(T t, U u);
3066
- ```
3067
-
3068
- — *end note*]
3069
-
3070
- A *non-template function* is a function that is not a function template
3071
- specialization.
3072
-
3073
- [*Note 11*: A function template is not a function. — *end note*]
3074
-
3075
- An *abbreviated function template* is a function declaration that has
3076
- one or more generic parameter type placeholders [[dcl.spec.auto]]. An
3077
- abbreviated function template is equivalent to a function template
3078
- [[temp.fct]] whose *template-parameter-list* includes one invented type
3079
- *template-parameter* for each generic parameter type placeholder of the
3080
- function declaration, in order of appearance. For a
3081
- *placeholder-type-specifier* of the form `auto`, the invented parameter
3082
- is an unconstrained *type-parameter*. For a *placeholder-type-specifier*
3083
- of the form *type-constraint* `auto`, the invented parameter is a
3084
- *type-parameter* with that *type-constraint*. The invented type
3085
- *template-parameter* is a template parameter pack if the corresponding
3086
- *parameter-declaration* declares a function parameter pack. If the
3087
- placeholder contains `decltype(auto)`, the program is ill-formed. The
3088
- adjusted function parameters of an abbreviated function template are
3089
- derived from the *parameter-declaration-clause* by replacing each
3090
- occurrence of a placeholder with the name of the corresponding invented
3091
- *template-parameter*.
3092
-
3093
- [*Example 9*:
3094
-
3095
- ``` cpp
3096
- template<typename T> concept C1 = /* ... */;
3097
- template<typename T> concept C2 = /* ... */;
3098
- template<typename... Ts> concept C3 = /* ... */;
3099
-
3100
- void g1(const C1 auto*, C2 auto&);
3101
- void g2(C1 auto&...);
3102
- void g3(C3 auto...);
3103
- void g4(C3 auto);
3104
- ```
3105
-
3106
- The declarations above are functionally equivalent (but not equivalent)
3107
- to their respective declarations below:
3108
-
3109
- ``` cpp
3110
- template<C1 T, C2 U> void g1(const T*, U&);
3111
- template<C1... Ts> void g2(Ts&...);
3112
- template<C3... Ts> void g3(Ts...);
3113
- template<C3 T> void g4(T);
3114
- ```
3115
-
3116
- Abbreviated function templates can be specialized like all function
3117
- templates.
3118
-
3119
- ``` cpp
3120
- template<> void g1<int>(const int*, const double&); // OK, specialization of g1<int, const double>
3121
- ```
3122
-
3123
- — *end example*]
3124
-
3125
- An abbreviated function template can have a *template-head*. The
3126
- invented *template-parameter*s are appended to the
3127
- *template-parameter-list* after the explicitly declared
3128
- *template-parameter*s.
3129
-
3130
- [*Example 10*:
3131
-
3132
- ``` cpp
3133
- template<typename> concept C = /* ... */;
3134
-
3135
- template <typename T, C U>
3136
- void g(T x, U y, C auto z);
3137
- ```
3138
-
3139
- This is functionally equivalent to each of the following two
3140
- declarations.
3141
-
3142
- ``` cpp
3143
- template<typename T, C U, C W>
3144
- void g(T x, U y, W z);
3145
-
3146
- template<typename T, typename U, typename W>
3147
- requires C<U> && C<W>
3148
- void g(T x, U y, W z);
3149
- ```
3150
-
3151
- — *end example*]
3152
-
3153
- A function declaration at block scope shall not declare an abbreviated
3154
- function template.
3155
-
3156
- A *declarator-id* or *abstract-declarator* containing an ellipsis shall
3157
- only be used in a *parameter-declaration*. When it is part of a
3158
- *parameter-declaration-clause*, the *parameter-declaration* declares a
3159
- function parameter pack [[temp.variadic]]. Otherwise, the
3160
- *parameter-declaration* is part of a *template-parameter-list* and
3161
- declares a template parameter pack; see  [[temp.param]]. A function
3162
- parameter pack is a pack expansion [[temp.variadic]].
3163
-
3164
- [*Example 11*:
3165
-
3166
- ``` cpp
3167
- template<typename... T> void f(T (* ...t)(int, int));
3168
-
3169
- int add(int, int);
3170
- float subtract(int, int);
3171
-
3172
- void g() {
3173
- f(add, subtract);
3174
- }
3175
- ```
3176
-
3177
- — *end example*]
3178
-
3179
- There is a syntactic ambiguity when an ellipsis occurs at the end of a
3180
- *parameter-declaration-clause* without a preceding comma. In this case,
3181
- the ellipsis is parsed as part of the *abstract-declarator* if the type
3182
- of the parameter either names a template parameter pack that has not
3183
- been expanded or contains `auto`; otherwise, it is parsed as part of the
3184
- *parameter-declaration-clause*.[^3]
3185
-
3186
- #### Default arguments <a id="dcl.fct.default">[[dcl.fct.default]]</a>
3187
-
3188
- If an *initializer-clause* is specified in a *parameter-declaration*
3189
- this *initializer-clause* is used as a default argument.
3190
-
3191
- [*Note 1*: Default arguments will be used in calls where trailing
3192
- arguments are missing [[expr.call]]. — *end note*]
3193
-
3194
- [*Example 1*:
3195
-
3196
- The declaration
3197
-
3198
- ``` cpp
3199
- void point(int = 3, int = 4);
3200
- ```
3201
-
3202
- declares a function that can be called with zero, one, or two arguments
3203
- of type `int`. It can be called in any of these ways:
3204
-
3205
- ``` cpp
3206
- point(1,2); point(1); point();
3207
- ```
3208
-
3209
- The last two calls are equivalent to `point(1,4)` and `point(3,4)`,
3210
- respectively.
3211
-
3212
- — *end example*]
3213
-
3214
- A default argument shall be specified only in the
3215
- *parameter-declaration-clause* of a function declaration or
3216
- *lambda-declarator* or in a *template-parameter* [[temp.param]]; in the
3217
- latter case, the *initializer-clause* shall be an
3218
- *assignment-expression*. A default argument shall not be specified for a
3219
- template parameter pack or a function parameter pack. If it is specified
3220
- in a *parameter-declaration-clause*, it shall not occur within a
3221
- *declarator* or *abstract-declarator* of a *parameter-declaration*.[^4]
3222
-
3223
- For non-template functions, default arguments can be added in later
3224
- declarations of a function that inhabit the same scope. Declarations
3225
- that inhabit different scopes have completely distinct sets of default
3226
- arguments. That is, declarations in inner scopes do not acquire default
3227
- arguments from declarations in outer scopes, and vice versa. In a given
3228
- function declaration, each parameter subsequent to a parameter with a
3229
- default argument shall have a default argument supplied in this or a
3230
- previous declaration, unless the parameter was expanded from a parameter
3231
- pack, or shall be a function parameter pack.
3232
-
3233
- [*Note 2*: A default argument cannot be redefined by a later
3234
- declaration (not even to the same value)
3235
- [[basic.def.odr]]. — *end note*]
3236
-
3237
- [*Example 2*:
3238
-
3239
- ``` cpp
3240
- void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
3241
- // a parameter with a default argument
3242
- void f(int, int);
3243
- void f(int, int = 7);
3244
- void h() {
3245
- f(3); // OK, calls f(3, 7)
3246
- void f(int = 1, int); // error: does not use default from surrounding scope
3247
- }
3248
- void m() {
3249
- void f(int, int); // has no defaults
3250
- f(4); // error: wrong number of arguments
3251
- void f(int, int = 5); // OK
3252
- f(4); // OK, calls f(4, 5);
3253
- void f(int, int = 5); // error: cannot redefine, even to same value
3254
- }
3255
- void n() {
3256
- f(6); // OK, calls f(6, 7)
3257
- }
3258
- template<class ... T> struct C {
3259
- void f(int n = 0, T...);
3260
- };
3261
- C<int> c; // OK, instantiates declaration void C::f(int n = 0, int)
3262
- ```
3263
-
3264
- — *end example*]
3265
-
3266
- For a given inline function defined in different translation units, the
3267
- accumulated sets of default arguments at the end of the translation
3268
- units shall be the same; no diagnostic is required. If a friend
3269
- declaration D specifies a default argument expression, that declaration
3270
- shall be a definition and there shall be no other declaration of the
3271
- function or function template which is reachable from D or from which D
3272
- is reachable.
3273
-
3274
- The default argument has the same semantic constraints as the
3275
- initializer in a declaration of a variable of the parameter type, using
3276
- the copy-initialization semantics [[dcl.init]]. The names in the default
3277
- argument are looked up, and the semantic constraints are checked, at the
3278
- point where the default argument appears, except that an immediate
3279
- invocation [[expr.const]] that is a potentially-evaluated subexpression
3280
- [[intro.execution]] of the *initializer-clause* in a
3281
- *parameter-declaration* is neither evaluated nor checked for whether it
3282
- is a constant expression at that point. Name lookup and checking of
3283
- semantic constraints for default arguments of templated functions are
3284
- performed as described in  [[temp.inst]].
3285
-
3286
- [*Example 3*:
3287
-
3288
- In the following code, `g` will be called with the value `f(2)`:
3289
-
3290
- ``` cpp
3291
- int a = 1;
3292
- int f(int);
3293
- int g(int x = f(a)); // default argument: f(::a)
3294
-
3295
- void h() {
3296
- a = 2;
3297
- {
3298
- int a = 3;
3299
- g(); // g(f(::a))
3300
- }
3301
- }
3302
- ```
3303
-
3304
- — *end example*]
3305
-
3306
- [*Note 3*: A default argument is a complete-class context
3307
- [[class.mem]]. Access checking applies to names in default arguments as
3308
- described in [[class.access]]. — *end note*]
3309
-
3310
- Except for member functions of class templates, the default arguments in
3311
- a member function definition that appears outside of the class
3312
- definition are added to the set of default arguments provided by the
3313
- member function declaration in the class definition; the program is
3314
- ill-formed if a default constructor [[class.default.ctor]], copy or move
3315
- constructor [[class.copy.ctor]], or copy or move assignment operator
3316
- [[class.copy.assign]] is so declared. Default arguments for a member
3317
- function of a class template shall be specified on the initial
3318
- declaration of the member function within the class template.
3319
-
3320
- [*Example 4*:
3321
-
3322
- ``` cpp
3323
- class C {
3324
- void f(int i = 3);
3325
- void g(int i, int j = 99);
3326
- };
3327
-
3328
- void C::f(int i = 3) {} // error: default argument already specified in class scope
3329
- void C::g(int i = 88, int j) {} // in this translation unit, C::g can be called with no arguments
3330
- ```
3331
-
3332
- — *end example*]
3333
-
3334
- [*Note 4*: A local variable cannot be odr-used [[term.odr.use]] in a
3335
- default argument. — *end note*]
3336
-
3337
- [*Example 5*:
3338
-
3339
- ``` cpp
3340
- void f() {
3341
- int i;
3342
- extern void g(int x = i); // error
3343
- extern void h(int x = sizeof(i)); // OK
3344
- // ...
3345
- }
3346
- ```
3347
-
3348
- — *end example*]
3349
-
3350
- [*Note 5*:
3351
-
3352
- The keyword `this` cannot appear in a default argument of a member
3353
- function; see  [[expr.prim.this]].
3354
-
3355
- [*Example 6*:
3356
-
3357
- ``` cpp
3358
- class A {
3359
- void f(A* p = this) { } // error
3360
- };
3361
- ```
3362
-
3363
- — *end example*]
3364
-
3365
- — *end note*]
3366
-
3367
- A default argument is evaluated each time the function is called with no
3368
- argument for the corresponding parameter. A parameter shall not appear
3369
- as a potentially-evaluated expression in a default argument.
3370
-
3371
- [*Note 6*: Parameters of a function declared before a default argument
3372
- are in scope and can hide namespace and class member
3373
- names. — *end note*]
3374
-
3375
- [*Example 7*:
3376
-
3377
- ``` cpp
3378
- int a;
3379
- int f(int a, int b = a); // error: parameter a used as default argument
3380
- typedef int I;
3381
- int g(float I, int b = I(2)); // error: parameter I found
3382
- int h(int a, int b = sizeof(a)); // OK, unevaluated operand[term.unevaluated.operand]
3383
- ```
3384
-
3385
- — *end example*]
3386
-
3387
- A non-static member shall not appear in a default argument unless it
3388
- appears as the *id-expression* of a class member access expression
3389
- [[expr.ref]] or unless it is used to form a pointer to member
3390
- [[expr.unary.op]].
3391
-
3392
- [*Example 8*:
3393
-
3394
- The declaration of `X::mem1()` in the following example is ill-formed
3395
- because no object is supplied for the non-static member `X::a` used as
3396
- an initializer.
3397
-
3398
- ``` cpp
3399
- int b;
3400
- class X {
3401
- int a;
3402
- int mem1(int i = a); // error: non-static member a used as default argument
3403
- int mem2(int i = b); // OK; use X::b
3404
- static int b;
3405
- };
3406
- ```
3407
-
3408
- The declaration of `X::mem2()` is meaningful, however, since no object
3409
- is needed to access the static member `X::b`. Classes, objects, and
3410
- members are described in [[class]].
3411
-
3412
- — *end example*]
3413
-
3414
- A default argument is not part of the type of a function.
3415
-
3416
- [*Example 9*:
3417
-
3418
- ``` cpp
3419
- int f(int = 0);
3420
-
3421
- void h() {
3422
- int j = f(1);
3423
- int k = f(); // OK, means f(0)
3424
- }
3425
-
3426
- int (*p1)(int) = &f;
3427
- int (*p2)() = &f; // error: type mismatch
3428
- ```
3429
-
3430
- — *end example*]
3431
-
3432
- When an overload set contains a declaration of a function that inhabits
3433
- a scope S, any default argument associated with any reachable
3434
- declaration that inhabits S is available to the call.
3435
-
3436
- [*Note 7*: The candidate might have been found through a
3437
- *using-declarator* from which the declaration that provides the default
3438
- argument is not reachable. — *end note*]
3439
-
3440
- A virtual function call [[class.virtual]] uses the default arguments in
3441
- the declaration of the virtual function determined by the static type of
3442
- the pointer or reference denoting the object. An overriding function in
3443
- a derived class does not acquire default arguments from the function it
3444
- overrides.
3445
-
3446
- [*Example 10*:
3447
-
3448
- ``` cpp
3449
- struct A {
3450
- virtual void f(int a = 7);
3451
- };
3452
- struct B : public A {
3453
- void f(int a);
3454
- };
3455
- void m() {
3456
- B* pb = new B;
3457
- A* pa = pb;
3458
- pa->f(); // OK, calls pa->B::f(7)
3459
- pb->f(); // error: wrong number of arguments for B::f()
3460
- }
3461
- ```
3462
-
3463
- — *end example*]
3464
-
3465
- ## Initializers <a id="dcl.init">[[dcl.init]]</a>
3466
-
3467
- ### General <a id="dcl.init.general">[[dcl.init.general]]</a>
3468
-
3469
- The process of initialization described in [[dcl.init]] applies to all
3470
- initializations regardless of syntactic context, including the
3471
- initialization of a function parameter [[expr.call]], the initialization
3472
- of a return value [[stmt.return]], or when an initializer follows a
3473
- declarator.
3474
-
3475
- ``` bnf
3476
- initializer:
3477
- brace-or-equal-initializer
3478
- '(' expression-list ')'
3479
- ```
3480
-
3481
- ``` bnf
3482
- brace-or-equal-initializer:
3483
- '=' initializer-clause
3484
- braced-init-list
3485
- ```
3486
-
3487
- ``` bnf
3488
- initializer-clause:
3489
- assignment-expression
3490
- braced-init-list
3491
- ```
3492
-
3493
- ``` bnf
3494
- braced-init-list:
3495
- '{' initializer-list ','ₒₚₜ '}'
3496
- '{' designated-initializer-list ','ₒₚₜ '}'
3497
- '{' '}'
3498
- ```
3499
-
3500
- ``` bnf
3501
- initializer-list:
3502
- initializer-clause '...'ₒₚₜ
3503
- initializer-list ',' initializer-clause '...'ₒₚₜ
3504
- ```
3505
-
3506
- ``` bnf
3507
- designated-initializer-list:
3508
- designated-initializer-clause
3509
- designated-initializer-list ',' designated-initializer-clause
3510
- ```
3511
-
3512
- ``` bnf
3513
- designated-initializer-clause:
3514
- designator brace-or-equal-initializer
3515
- ```
3516
-
3517
- ``` bnf
3518
- designator:
3519
- '.' identifier
3520
- ```
3521
-
3522
- ``` bnf
3523
- expr-or-braced-init-list:
3524
- expression
3525
- braced-init-list
3526
- ```
3527
-
3528
- [*Note 1*: The rules in [[dcl.init]] apply even if the grammar permits
3529
- only the *brace-or-equal-initializer* form of *initializer* in a given
3530
- context. — *end note*]
3531
-
3532
- Except for objects declared with the `constexpr` specifier, for which
3533
- see  [[dcl.constexpr]], an *initializer* in the definition of a variable
3534
- can consist of arbitrary expressions involving literals and previously
3535
- declared variables and functions, regardless of the variable’s storage
3536
- duration.
3537
-
3538
- [*Example 1*:
3539
-
3540
- ``` cpp
3541
- int f(int);
3542
- int a = 2;
3543
- int b = f(a);
3544
- int c(b);
3545
- ```
3546
-
3547
- — *end example*]
3548
-
3549
- [*Note 2*: Default arguments are more restricted; see 
3550
- [[dcl.fct.default]]. — *end note*]
3551
-
3552
- [*Note 3*: The order of initialization of variables with static storage
3553
- duration is described in  [[basic.start]] and 
3554
- [[stmt.dcl]]. — *end note*]
3555
-
3556
- A declaration D of a variable with linkage shall not have an
3557
- *initializer* if D inhabits a block scope.
3558
-
3559
- To *zero-initialize* an object or reference of type `T` means:
3560
-
3561
- - if `T` is a scalar type [[term.scalar.type]], the object is
3562
- initialized to the value obtained by converting the integer literal
3563
- `0` (zero) to `T`;[^5]
3564
- - if `T` is a (possibly cv-qualified) non-union class type, its padding
3565
- bits [[term.padding.bits]] are initialized to zero bits and each
3566
- non-static data member, each non-virtual base class subobject, and, if
3567
- the object is not a base class subobject, each virtual base class
3568
- subobject is zero-initialized;
3569
- - if `T` is a (possibly cv-qualified) union type, its padding bits
3570
- [[term.padding.bits]] are initialized to zero bits and the object’s
3571
- first non-static named data member is zero-initialized;
3572
- - if `T` is an array type, each element is zero-initialized;
3573
- - if `T` is a reference type, no initialization is performed.
3574
-
3575
- To *default-initialize* an object of type `T` means:
3576
-
3577
- - If `T` is a (possibly cv-qualified) class type [[class]], constructors
3578
- are considered. The applicable constructors are enumerated
3579
- [[over.match.ctor]], and the best one for the *initializer* `()` is
3580
- chosen through overload resolution [[over.match]]. The constructor
3581
- thus selected is called, with an empty argument list, to initialize
3582
- the object.
3583
- - If `T` is an array type, each element is default-initialized.
3584
- - Otherwise, no initialization is performed.
3585
-
3586
- A class type `T` is *const-default-constructible* if
3587
- default-initialization of `T` would invoke a user-provided constructor
3588
- of `T` (not inherited from a base class) or if
3589
-
3590
- - each direct non-variant non-static data member `M` of `T` has a
3591
- default member initializer or, if `M` is of class type `X` (or array
3592
- thereof), `X` is const-default-constructible,
3593
- - if `T` is a union with at least one non-static data member, exactly
3594
- one variant member has a default member initializer,
3595
- - if `T` is not a union, for each anonymous union member with at least
3596
- one non-static data member (if any), exactly one non-static data
3597
- member has a default member initializer, and
3598
- - each potentially constructed base class of `T` is
3599
- const-default-constructible.
3600
-
3601
- If a program calls for the default-initialization of an object of a
3602
- const-qualified type `T`, `T` shall be a const-default-constructible
3603
- class type or array thereof.
3604
-
3605
- To *value-initialize* an object of type `T` means:
3606
-
3607
- - if `T` is a (possibly cv-qualified) class type [[class]], then
3608
- - if `T` has either no default constructor [[class.default.ctor]] or a
3609
- default constructor that is user-provided or deleted, then the
3610
- object is default-initialized;
3611
- - otherwise, the object is zero-initialized and the semantic
3612
- constraints for default-initialization are checked, and if `T` has a
3613
- non-trivial default constructor, the object is default-initialized;
3614
- - if `T` is an array type, then each element is value-initialized;
3615
- - otherwise, the object is zero-initialized.
3616
-
3617
- A program that calls for default-initialization or value-initialization
3618
- of an entity of reference type is ill-formed.
3619
-
3620
- [*Note 4*: For every object of static storage duration, static
3621
- initialization [[basic.start.static]] is performed at program startup
3622
- before any other initialization takes place. In some cases, additional
3623
- initialization is done later. — *end note*]
3624
-
3625
- If no initializer is specified for an object, the object is
3626
- default-initialized.
3627
-
3628
- If the entity being initialized does not have class type, the
3629
- *expression-list* in a parenthesized initializer shall be a single
3630
- expression.
3631
-
3632
- The initialization that occurs in the `=` form of a
3633
- *brace-or-equal-initializer* or *condition* [[stmt.select]], as well as
3634
- in argument passing, function return, throwing an exception
3635
- [[except.throw]], handling an exception [[except.handle]], and aggregate
3636
- member initialization other than by a *designated-initializer-clause*
3637
- [[dcl.init.aggr]], is called *copy-initialization*.
3638
-
3639
- [*Note 5*: Copy-initialization can invoke a move
3640
- [[class.copy.ctor]]. — *end note*]
3641
-
3642
- The initialization that occurs
3643
-
3644
- - for an *initializer* that is a parenthesized *expression-list* or a
3645
- *braced-init-list*,
3646
- - for a *new-initializer* [[expr.new]],
3647
- - in a `static_cast` expression [[expr.static.cast]],
3648
- - in a functional notation type conversion [[expr.type.conv]], and
3649
- - in the *braced-init-list* form of a *condition*
3650
-
3651
- is called *direct-initialization*.
3652
-
3653
- The semantics of initializers are as follows. The *destination type* is
3654
- the type of the object or reference being initialized and the *source
3655
- type* is the type of the initializer expression. If the initializer is
3656
- not a single (possibly parenthesized) expression, the source type is not
3657
- defined.
3658
-
3659
- - If the initializer is a (non-parenthesized) *braced-init-list* or is
3660
- `=` *braced-init-list*, the object or reference is list-initialized
3661
- [[dcl.init.list]].
3662
- - If the destination type is a reference type, see  [[dcl.init.ref]].
3663
- - If the destination type is an array of characters, an array of
3664
- `char8_t`, an array of `char16_t`, an array of `char32_t`, or an array
3665
- of `wchar_t`, and the initializer is a *string-literal*, see 
3666
- [[dcl.init.string]].
3667
- - If the initializer is `()`, the object is value-initialized.
3668
- \[*Note 6*:
3669
- Since `()` is not permitted by the syntax for *initializer*,
3670
- ``` cpp
3671
- X a();
3672
- ```
3673
-
3674
- is not the declaration of an object of class `X`, but the declaration
3675
- of a function taking no arguments and returning an `X`. The form `()`
3676
- is permitted in certain other initialization contexts
3677
- [[expr.new]], [[expr.type.conv]], [[class.base.init]].
3678
- — *end note*]
3679
- - Otherwise, if the destination type is an array, the object is
3680
- initialized as follows. Let x₁, …, xₖ be the elements of the
3681
- *expression-list*. If the destination type is an array of unknown
3682
- bound, it is defined as having k elements. Let n denote the array size
3683
- after this potential adjustment. If k is greater than n, the program
3684
- is ill-formed. Otherwise, the iᵗʰ array element is copy-initialized
3685
- with xᵢ for each 1 ≤ i ≤ k, and value-initialized for each k < i ≤ n.
3686
- For each 1 ≤ i < j ≤ n, every value computation and side effect
3687
- associated with the initialization of the iᵗʰ element of the array is
3688
- sequenced before those associated with the initialization of the jᵗʰ
3689
- element.
3690
- - Otherwise, if the destination type is a (possibly cv-qualified) class
3691
- type:
3692
- - If the initializer expression is a prvalue and the cv-unqualified
3693
- version of the source type is the same class as the class of the
3694
- destination, the initializer expression is used to initialize the
3695
- destination object. \[*Example 2*: `T x = T(T(T()));`
3696
- value-initializes `x`. — *end example*]
3697
- - Otherwise, if the initialization is direct-initialization, or if it
3698
- is copy-initialization where the cv-unqualified version of the
3699
- source type is the same class as, or a derived class of, the class
3700
- of the destination, constructors are considered. The applicable
3701
- constructors are enumerated [[over.match.ctor]], and the best one is
3702
- chosen through overload resolution [[over.match]]. Then:
3703
- - If overload resolution is successful, the selected constructor is
3704
- called to initialize the object, with the initializer expression
3705
- or *expression-list* as its argument(s).
3706
- - Otherwise, if no constructor is viable, the destination type is an
3707
- aggregate class, and the initializer is a parenthesized
3708
- *expression-list*, the object is initialized as follows. Let e₁,
3709
- …, eₙ be the elements of the aggregate [[dcl.init.aggr]]. Let x₁,
3710
- …, xₖ be the elements of the *expression-list*. If k is greater
3711
- than n, the program is ill-formed. The element eᵢ is
3712
- copy-initialized with xᵢ for 1 ≤ i ≤ k. The remaining elements are
3713
- initialized with their default member initializers, if any, and
3714
- otherwise are value-initialized. For each 1 ≤ i < j ≤ n, every
3715
- value computation and side effect associated with the
3716
- initialization of eᵢ is sequenced before those associated with the
3717
- initialization of eⱼ.
3718
- \[*Note 7*:
3719
- By contrast with direct-list-initialization, narrowing conversions
3720
- [[dcl.init.list]] are permitted, designators are not permitted, a
3721
- temporary object bound to a reference does not have its lifetime
3722
- extended [[class.temporary]], and there is no brace elision.
3723
- \[*Example 3*:
3724
- ``` cpp
3725
- struct A {
3726
- int a;
3727
- int&& r;
3728
- };
3729
-
3730
- int f();
3731
- int n = 10;
3732
-
3733
- A a1{1, f()}; // OK, lifetime is extended
3734
- A a2(1, f()); // well-formed, but dangling reference
3735
- A a3{1.0, 1}; // error: narrowing conversion
3736
- A a4(1.0, 1); // well-formed, but dangling reference
3737
- A a5(1.0, std::move(n)); // OK
3738
- ```
3739
-
3740
- — *end example*]
3741
- — *end note*]
3742
- - Otherwise, the initialization is ill-formed.
3743
- - Otherwise (i.e., for the remaining copy-initialization cases),
3744
- user-defined conversions that can convert from the source type to
3745
- the destination type or (when a conversion function is used) to a
3746
- derived class thereof are enumerated as described in 
3747
- [[over.match.copy]], and the best one is chosen through overload
3748
- resolution [[over.match]]. If the conversion cannot be done or is
3749
- ambiguous, the initialization is ill-formed. The function selected
3750
- is called with the initializer expression as its argument; if the
3751
- function is a constructor, the call is a prvalue of the
3752
- cv-unqualified version of the destination type whose result object
3753
- is initialized by the constructor. The call is used to
3754
- direct-initialize, according to the rules above, the object that is
3755
- the destination of the copy-initialization.
3756
- - Otherwise, if the source type is a (possibly cv-qualified) class type,
3757
- conversion functions are considered. The applicable conversion
3758
- functions are enumerated [[over.match.conv]], and the best one is
3759
- chosen through overload resolution [[over.match]]. The user-defined
3760
- conversion so selected is called to convert the initializer expression
3761
- into the object being initialized. If the conversion cannot be done or
3762
- is ambiguous, the initialization is ill-formed.
3763
- - Otherwise, if the initialization is direct-initialization, the source
3764
- type is `std::nullptr_t`, and the destination type is `bool`, the
3765
- initial value of the object being initialized is `false`.
3766
- - Otherwise, the initial value of the object being initialized is the
3767
- (possibly converted) value of the initializer expression. A standard
3768
- conversion sequence [[conv]] will be used, if necessary, to convert
3769
- the initializer expression to the cv-unqualified version of the
3770
- destination type; no user-defined conversions are considered. If the
3771
- conversion cannot be done, the initialization is ill-formed. When
3772
- initializing a bit-field with a value that it cannot represent, the
3773
- resulting value of the bit-field is *implementation-defined*.
3774
- \[*Note 8*:
3775
- An expression of type “*cv1* `T`” can initialize an object of type
3776
- “*cv2* `T`” independently of the cv-qualifiers *cv1* and *cv2*.
3777
- ``` cpp
3778
- int a;
3779
- const int b = a;
3780
- int c = b;
3781
- ```
3782
-
3783
- — *end note*]
3784
-
3785
- An immediate invocation [[expr.const]] that is not evaluated where it
3786
- appears [[dcl.fct.default]], [[class.mem.general]] is evaluated and
3787
- checked for whether it is a constant expression at the point where the
3788
- enclosing *initializer* is used in a function call, a constructor
3789
- definition, or an aggregate initialization.
3790
-
3791
- An *initializer-clause* followed by an ellipsis is a pack expansion
3792
- [[temp.variadic]].
3793
-
3794
- Initialization includes the evaluation of all subexpressions of each
3795
- *initializer-clause* of the initializer (possibly nested within
3796
- *braced-init-list*s) and the creation of any temporary objects for
3797
- function arguments or return values [[class.temporary]].
3798
-
3799
- If the initializer is a parenthesized *expression-list*, the expressions
3800
- are evaluated in the order specified for function calls [[expr.call]].
3801
-
3802
- The same *identifier* shall not appear in multiple *designator*s of a
3803
- *designated-initializer-list*.
3804
-
3805
- An object whose initialization has completed is deemed to be
3806
- constructed, even if the object is of non-class type or no constructor
3807
- of the object’s class is invoked for the initialization.
3808
-
3809
- [*Note 9*: Such an object might have been value-initialized or
3810
- initialized by aggregate initialization [[dcl.init.aggr]] or by an
3811
- inherited constructor [[class.inhctor.init]]. — *end note*]
3812
-
3813
- Destroying an object of class type invokes the destructor of the class.
3814
- Destroying a scalar type has no effect other than ending the lifetime of
3815
- the object [[basic.life]]. Destroying an array destroys each element in
3816
- reverse subscript order.
3817
-
3818
- A declaration that specifies the initialization of a variable, whether
3819
- from an explicit initializer or by default-initialization, is called the
3820
- *initializing declaration* of that variable.
3821
-
3822
- [*Note 10*: In most cases this is the defining declaration
3823
- [[basic.def]] of the variable, but the initializing declaration of a
3824
- non-inline static data member [[class.static.data]] can be the
3825
- declaration within the class definition and not the definition (if any)
3826
- outside it. — *end note*]
3827
-
3828
- ### Aggregates <a id="dcl.init.aggr">[[dcl.init.aggr]]</a>
3829
-
3830
- An *aggregate* is an array or a class [[class]] with
3831
-
3832
- - no user-declared or inherited constructors [[class.ctor]],
3833
- - no private or protected direct non-static data members
3834
- [[class.access]],
3835
- - no private or protected direct base classes [[class.access.base]], and
3836
- - no virtual functions [[class.virtual]] or virtual base classes
3837
- [[class.mi]].
3838
-
3839
- [*Note 1*: Aggregate initialization does not allow accessing protected
3840
- and private base class’ members or constructors. — *end note*]
3841
-
3842
- The *elements* of an aggregate are:
3843
-
3844
- - for an array, the array elements in increasing subscript order, or
3845
- - for a class, the direct base classes in declaration order, followed by
3846
- the direct non-static data members [[class.mem]] that are not members
3847
- of an anonymous union, in declaration order.
3848
-
3849
- When an aggregate is initialized by an initializer list as specified in 
3850
- [[dcl.init.list]], the elements of the initializer list are taken as
3851
- initializers for the elements of the aggregate. The *explicitly
3852
- initialized elements* of the aggregate are determined as follows:
3853
-
3854
- - If the initializer list is a brace-enclosed
3855
- *designated-initializer-list*, the aggregate shall be of class type,
3856
- the *identifier* in each *designator* shall name a direct non-static
3857
- data member of the class, and the explicitly initialized elements of
3858
- the aggregate are the elements that are, or contain, those members.
3859
- - If the initializer list is a brace-enclosed *initializer-list*, the
3860
- explicitly initialized elements of the aggregate are the first n
3861
- elements of the aggregate, where n is the number of elements in the
3862
- initializer list.
3863
- - Otherwise, the initializer list must be `{}`, and there are no
3864
- explicitly initialized elements.
3865
-
3866
- For each explicitly initialized element:
3867
-
3868
- - If the element is an anonymous union member and the initializer list
3869
- is a brace-enclosed *designated-initializer-list*, the element is
3870
- initialized by the *braced-init-list* `{ `*D*` }`, where *D* is the
3871
- *designated-initializer-clause* naming a member of the anonymous union
3872
- member. There shall be only one such *designated-initializer-clause*.
3873
- \[*Example 1*:
3874
- ``` cpp
3875
- struct C {
3876
- union {
3877
- int a;
3878
- const char* p;
3879
- };
3880
- int x;
3881
- } c = { .a = 1, .x = 3 };
3882
- ```
3883
-
3884
- initializes `c.a` with 1 and `c.x` with 3.
3885
- — *end example*]
3886
- - Otherwise, the element is copy-initialized from the corresponding
3887
- *initializer-clause* or is initialized with the
3888
- *brace-or-equal-initializer* of the corresponding
3889
- *designated-initializer-clause*. If that initializer is of the form
3890
- *assignment-expression* or `= `*assignment-expression* and a narrowing
3891
- conversion [[dcl.init.list]] is required to convert the expression,
3892
- the program is ill-formed.
3893
- \[*Note 2*: If the initialization is by
3894
- *designated-initializer-clause*, its form determines whether
3895
- copy-initialization or direct-initialization is
3896
- performed. — *end note*]
3897
- \[*Note 3*: If an initializer is itself an initializer list, the
3898
- element is list-initialized, which will result in a recursive
3899
- application of the rules in this subclause if the element is an
3900
- aggregate. — *end note*]
3901
- \[*Example 2*:
3902
- ``` cpp
3903
- struct A {
3904
- int x;
3905
- struct B {
3906
- int i;
3907
- int j;
3908
- } b;
3909
- } a = { 1, { 2, 3 } };
3910
- ```
3911
-
3912
- initializes `a.x` with 1, `a.b.i` with 2, `a.b.j` with 3.
3913
- ``` cpp
3914
- struct base1 { int b1, b2 = 42; };
3915
- struct base2 {
3916
- base2() {
3917
- b3 = 42;
3918
- }
3919
- int b3;
3920
- };
3921
- struct derived : base1, base2 {
3922
- int d;
3923
- };
3924
-
3925
- derived d1{{1, 2}, {}, 4};
3926
- derived d2{{}, {}, 4};
3927
- ```
3928
-
3929
- initializes `d1.b1` with 1, `d1.b2` with 2, `d1.b3` with 42, `d1.d`
3930
- with 4, and `d2.b1` with 0, `d2.b2` with 42, `d2.b3` with 42, `d2.d`
3931
- with 4.
3932
- — *end example*]
3933
-
3934
- For a non-union aggregate, each element that is not an explicitly
3935
- initialized element is initialized as follows:
3936
-
3937
- - If the element has a default member initializer [[class.mem]], the
3938
- element is initialized from that initializer.
3939
- - Otherwise, if the element is not a reference, the element is
3940
- copy-initialized from an empty initializer list [[dcl.init.list]].
3941
- - Otherwise, the program is ill-formed.
3942
-
3943
- If the aggregate is a union and the initializer list is empty, then
3944
-
3945
- - if any variant member has a default member initializer, that member is
3946
- initialized from its default member initializer;
3947
- - otherwise, the first member of the union (if any) is copy-initialized
3948
- from an empty initializer list.
3949
-
3950
- [*Example 3*:
3951
-
3952
- ``` cpp
3953
- struct S { int a; const char* b; int c; int d = b[a]; };
3954
- S ss = { 1, "asdf" };
3955
- ```
3956
-
3957
- initializes `ss.a` with 1, `ss.b` with `"asdf"`, `ss.c` with the value
3958
- of an expression of the form `int{}` (that is, `0`), and `ss.d` with the
3959
- value of `ss.b[ss.a]` (that is, `'s'`), and in
3960
-
3961
- ``` cpp
3962
- struct X { int i, j, k = 42; };
3963
- X a[] = { 1, 2, 3, 4, 5, 6 };
3964
- X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
3965
- ```
3966
-
3967
- `a` and `b` have the same value
3968
-
3969
- ``` cpp
3970
- struct A {
3971
- string a;
3972
- int b = 42;
3973
- int c = -1;
3974
- };
3975
- ```
3976
-
3977
- `A{.c=21}` has the following steps:
3978
-
3979
- - Initialize `a` with `{}`
3980
- - Initialize `b` with `= 42`
3981
- - Initialize `c` with `= 21`
3982
-
3983
- — *end example*]
3984
-
3985
- The initializations of the elements of the aggregate are evaluated in
3986
- the element order. That is, all value computations and side effects
3987
- associated with a given element are sequenced before those of any
3988
- element that follows it in order.
3989
-
3990
- An aggregate that is a class can also be initialized with a single
3991
- expression not enclosed in braces, as described in  [[dcl.init]].
3992
-
3993
- The destructor for each element of class type is potentially invoked
3994
- [[class.dtor]] from the context where the aggregate initialization
3995
- occurs.
3996
-
3997
- [*Note 4*: This provision ensures that destructors can be called for
3998
- fully-constructed subobjects in case an exception is thrown
3999
- [[except.ctor]]. — *end note*]
4000
-
4001
- An array of unknown bound initialized with a brace-enclosed
4002
- *initializer-list* containing `n` *initializer-clause*s is defined as
4003
- having `n` elements [[dcl.array]].
4004
-
4005
- [*Example 4*:
4006
-
4007
- ``` cpp
4008
- int x[] = { 1, 3, 5 };
4009
- ```
4010
-
4011
- declares and initializes `x` as a one-dimensional array that has three
4012
- elements since no size was specified and there are three initializers.
4013
-
4014
- — *end example*]
4015
-
4016
- An array of unknown bound shall not be initialized with an empty
4017
- *braced-init-list* `{}`.[^6]
4018
-
4019
- [*Note 5*:
4020
-
4021
- A default member initializer does not determine the bound for a member
4022
- array of unknown bound. Since the default member initializer is ignored
4023
- if a suitable *mem-initializer* is present [[class.base.init]], the
4024
- default member initializer is not considered to initialize the array of
4025
- unknown bound.
4026
-
4027
- [*Example 5*:
4028
-
4029
- ``` cpp
4030
- struct S {
4031
- int y[] = { 0 }; // error: non-static data member of incomplete type
4032
- };
4033
- ```
4034
-
4035
- — *end example*]
4036
-
4037
- — *end note*]
4038
-
4039
- [*Note 6*:
4040
-
4041
- Static data members, non-static data members of anonymous union members,
4042
- and unnamed bit-fields are not considered elements of the aggregate.
4043
-
4044
- [*Example 6*:
4045
-
4046
- ``` cpp
4047
- struct A {
4048
- int i;
4049
- static int s;
4050
- int j;
4051
- int :17;
4052
- int k;
4053
- } a = { 1, 2, 3 };
4054
- ```
4055
-
4056
- Here, the second initializer 2 initializes `a.j` and not the static data
4057
- member `A::s`, and the third initializer 3 initializes `a.k` and not the
4058
- unnamed bit-field before it.
4059
-
4060
- — *end example*]
4061
-
4062
- — *end note*]
4063
-
4064
- An *initializer-list* is ill-formed if the number of
4065
- *initializer-clause*s exceeds the number of elements of the aggregate.
4066
-
4067
- [*Example 7*:
4068
-
4069
- ``` cpp
4070
- char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error
4071
- ```
4072
-
4073
- is ill-formed.
4074
-
4075
- — *end example*]
4076
-
4077
- If a member has a default member initializer and a potentially-evaluated
4078
- subexpression thereof is an aggregate initialization that would use that
4079
- default member initializer, the program is ill-formed.
4080
-
4081
- [*Example 8*:
4082
-
4083
- ``` cpp
4084
- struct A;
4085
- extern A a;
4086
- struct A {
4087
- const A& a1 { A{a,a} }; // OK
4088
- const A& a2 { A{} }; // error
4089
- };
4090
- A a{a,a}; // OK
4091
-
4092
- struct B {
4093
- int n = B{}.n; // error
4094
- };
4095
- ```
4096
-
4097
- — *end example*]
4098
-
4099
- If an aggregate class `C` contains a subaggregate element `e` with no
4100
- elements, the *initializer-clause* for `e` shall not be omitted from an
4101
- *initializer-list* for an object of type `C` unless the
4102
- *initializer-clause*s for all elements of `C` following `e` are also
4103
- omitted.
4104
-
4105
- [*Example 9*:
4106
-
4107
- ``` cpp
4108
- struct S { } s;
4109
- struct A {
4110
- S s1;
4111
- int i1;
4112
- S s2;
4113
- int i2;
4114
- S s3;
4115
- int i3;
4116
- } a = {
4117
- { }, // Required initialization
4118
- 0,
4119
- s, // Required initialization
4120
- 0
4121
- }; // Initialization not required for A::s3 because A::i3 is also not initialized
4122
- ```
4123
-
4124
- — *end example*]
4125
-
4126
- When initializing a multidimensional array, the *initializer-clause*s
4127
- initialize the elements with the last (rightmost) index of the array
4128
- varying the fastest [[dcl.array]].
4129
-
4130
- [*Example 10*:
4131
-
4132
- ``` cpp
4133
- int x[2][2] = { 3, 1, 4, 2 };
4134
- ```
4135
-
4136
- initializes `x[0][0]` to `3`, `x[0][1]` to `1`, `x[1][0]` to `4`, and
4137
- `x[1][1]` to `2`. On the other hand,
4138
-
4139
- ``` cpp
4140
- float y[4][3] = {
4141
- { 1 }, { 2 }, { 3 }, { 4 }
4142
- };
4143
- ```
4144
-
4145
- initializes the first column of `y` (regarded as a two-dimensional
4146
- array) and leaves the rest zero.
4147
-
4148
- — *end example*]
4149
-
4150
- Braces can be elided in an *initializer-list* as follows. If the
4151
- *initializer-list* begins with a left brace, then the succeeding
4152
- comma-separated list of *initializer-clause*s initializes the elements
4153
- of a subaggregate; it is erroneous for there to be more
4154
- *initializer-clause*s than elements. If, however, the *initializer-list*
4155
- for a subaggregate does not begin with a left brace, then only enough
4156
- *initializer-clause*s from the list are taken to initialize the elements
4157
- of the subaggregate; any remaining *initializer-clause*s are left to
4158
- initialize the next element of the aggregate of which the current
4159
- subaggregate is an element.
4160
-
4161
- [*Example 11*:
4162
-
4163
- ``` cpp
4164
- float y[4][3] = {
4165
- { 1, 3, 5 },
4166
- { 2, 4, 6 },
4167
- { 3, 5, 7 },
4168
- };
4169
- ```
4170
-
4171
- is a completely-braced initialization: 1, 3, and 5 initialize the first
4172
- row of the array `y[0]`, namely `y[0][0]`, `y[0][1]`, and `y[0][2]`.
4173
- Likewise the next two lines initialize `y[1]` and `y[2]`. The
4174
- initializer ends early and therefore `y[3]`s elements are initialized as
4175
- if explicitly initialized with an expression of the form `float()`, that
4176
- is, are initialized with `0.0`. In the following example, braces in the
4177
- *initializer-list* are elided; however the *initializer-list* has the
4178
- same effect as the completely-braced *initializer-list* of the above
4179
- example,
4180
-
4181
- ``` cpp
4182
- float y[4][3] = {
4183
- 1, 3, 5, 2, 4, 6, 3, 5, 7
4184
- };
4185
- ```
4186
-
4187
- The initializer for `y` begins with a left brace, but the one for `y[0]`
4188
- does not, therefore three elements from the list are used. Likewise the
4189
- next three are taken successively for `y[1]` and `y[2]`.
4190
-
4191
- — *end example*]
4192
-
4193
- All implicit type conversions [[conv]] are considered when initializing
4194
- the element with an *assignment-expression*. If the
4195
- *assignment-expression* can initialize an element, the element is
4196
- initialized. Otherwise, if the element is itself a subaggregate, brace
4197
- elision is assumed and the *assignment-expression* is considered for the
4198
- initialization of the first element of the subaggregate.
4199
-
4200
- [*Note 7*: As specified above, brace elision cannot apply to
4201
- subaggregates with no elements; an *initializer-clause* for the entire
4202
- subobject is required. — *end note*]
4203
-
4204
- [*Example 12*:
4205
-
4206
- ``` cpp
4207
- struct A {
4208
- int i;
4209
- operator int();
4210
- };
4211
- struct B {
4212
- A a1, a2;
4213
- int z;
4214
- };
4215
- A a;
4216
- B b = { 4, a, a };
4217
- ```
4218
-
4219
- Braces are elided around the *initializer-clause* for `b.a1.i`. `b.a1.i`
4220
- is initialized with 4, `b.a2` is initialized with `a`, `b.z` is
4221
- initialized with whatever `a.operator int()` returns.
4222
-
4223
- — *end example*]
4224
-
4225
- [*Note 8*: An aggregate array or an aggregate class can contain
4226
- elements of a class type with a user-declared constructor
4227
- [[class.ctor]]. Initialization of these aggregate objects is described
4228
- in  [[class.expl.init]]. — *end note*]
4229
-
4230
- [*Note 9*: Whether the initialization of aggregates with static storage
4231
- duration is static or dynamic is specified in  [[basic.start.static]],
4232
- [[basic.start.dynamic]], and  [[stmt.dcl]]. — *end note*]
4233
-
4234
- When a union is initialized with an initializer list, there shall not be
4235
- more than one explicitly initialized element.
4236
-
4237
- [*Example 13*:
4238
-
4239
- ``` cpp
4240
- union u { int a; const char* b; };
4241
- u a = { 1 };
4242
- u b = a;
4243
- u c = 1; // error
4244
- u d = { 0, "asdf" }; // error
4245
- u e = { "asdf" }; // error
4246
- u f = { .b = "asdf" };
4247
- u g = { .a = 1, .b = "asdf" }; // error
4248
- ```
4249
-
4250
- — *end example*]
4251
-
4252
- [*Note 10*: As described above, the braces around the
4253
- *initializer-clause* for a union member can be omitted if the union is a
4254
- member of another aggregate. — *end note*]
4255
-
4256
- ### Character arrays <a id="dcl.init.string">[[dcl.init.string]]</a>
4257
-
4258
- An array of ordinary character type [[basic.fundamental]], `char8_t`
4259
- array, `char16_t` array, `char32_t` array, or `wchar_t` array may be
4260
- initialized by an ordinary string literal, UTF-8 string literal, UTF-16
4261
- string literal, UTF-32 string literal, or wide string literal,
4262
- respectively, or by an appropriately-typed *string-literal* enclosed in
4263
- braces [[lex.string]]. Additionally, an array of `char` or
4264
- `unsigned char` may be initialized by a UTF-8 string literal, or by such
4265
- a string literal enclosed in braces. Successive characters of the value
4266
- of the *string-literal* initialize the elements of the array, with an
4267
- integral conversion [[conv.integral]] if necessary for the source and
4268
- destination value.
4269
-
4270
- [*Example 1*:
4271
-
4272
- ``` cpp
4273
- char msg[] = "Syntax error on line %s\n";
4274
- ```
4275
-
4276
- shows a character array whose members are initialized with a
4277
- *string-literal*. Note that because `'\n'` is a single character and
4278
- because a trailing `'\0'` is appended, `sizeof(msg)` is `25`.
4279
-
4280
- — *end example*]
4281
-
4282
- There shall not be more initializers than there are array elements.
4283
-
4284
- [*Example 2*:
4285
-
4286
- ``` cpp
4287
- char cv[4] = "asdf"; // error
4288
- ```
4289
-
4290
- is ill-formed since there is no space for the implied trailing `'\0'`.
4291
-
4292
- — *end example*]
4293
-
4294
- If there are fewer initializers than there are array elements, each
4295
- element not explicitly initialized shall be zero-initialized
4296
- [[dcl.init]].
4297
-
4298
- ### References <a id="dcl.init.ref">[[dcl.init.ref]]</a>
4299
-
4300
- A variable whose declared type is “reference to `T`” [[dcl.ref]] shall
4301
- be initialized.
4302
-
4303
- [*Example 1*:
4304
-
4305
- ``` cpp
4306
- int g(int) noexcept;
4307
- void f() {
4308
- int i;
4309
- int& r = i; // r refers to i
4310
- r = 1; // the value of i becomes 1
4311
- int* p = &r; // p points to i
4312
- int& rr = r; // rr refers to what r refers to, that is, to i
4313
- int (&rg)(int) = g; // rg refers to the function g
4314
- rg(i); // calls function g
4315
- int a[3];
4316
- int (&ra)[3] = a; // ra refers to the array a
4317
- ra[1] = i; // modifies a[1]
4318
- }
4319
- ```
4320
-
4321
- — *end example*]
4322
-
4323
- A reference cannot be changed to refer to another object after
4324
- initialization.
4325
-
4326
- [*Note 1*: Assignment to a reference assigns to the object referred to
4327
- by the reference [[expr.ass]]. — *end note*]
4328
-
4329
- Argument passing [[expr.call]] and function value return [[stmt.return]]
4330
- are initializations.
4331
-
4332
- The initializer can be omitted for a reference only in a parameter
4333
- declaration [[dcl.fct]], in the declaration of a function return type,
4334
- in the declaration of a class member within its class definition
4335
- [[class.mem]], and where the `extern` specifier is explicitly used.
4336
-
4337
- [*Example 2*:
4338
-
4339
- ``` cpp
4340
- int& r1; // error: initializer missing
4341
- extern int& r2; // OK
4342
- ```
4343
-
4344
- — *end example*]
4345
-
4346
- Given types “*cv1* `T1`” and “*cv2* `T2`”, “*cv1* `T1`” is
4347
- *reference-related* to “*cv2* `T2`” if `T1` is similar [[conv.qual]] to
4348
- `T2`, or `T1` is a base class of `T2`. “*cv1* `T1`” is
4349
- *reference-compatible* with “*cv2* `T2`” if a prvalue of type “pointer
4350
- to *cv2* `T2`” can be converted to the type “pointer to *cv1* `T1`” via
4351
- a standard conversion sequence [[conv]]. In all cases where the
4352
- reference-compatible relationship of two types is used to establish the
4353
- validity of a reference binding and the standard conversion sequence
4354
- would be ill-formed, a program that necessitates such a binding is
4355
- ill-formed.
4356
-
4357
- A reference to type “*cv1* `T1`” is initialized by an expression of type
4358
- “*cv2* `T2`” as follows:
4359
-
4360
- - If the reference is an lvalue reference and the initializer expression
4361
- - is an lvalue (but is not a bit-field), and “*cv1* `T1`” is
4362
- reference-compatible with “*cv2* `T2`”, or
4363
- - has a class type (i.e., `T2` is a class type), where `T1` is not
4364
- reference-related to `T2`, and can be converted to an lvalue of type
4365
- “*cv3* `T3`”, where “*cv1* `T1`” is reference-compatible with “*cv3*
4366
- `T3`”[^7] (this conversion is selected by enumerating the applicable
4367
- conversion functions [[over.match.ref]] and choosing the best one
4368
- through overload resolution [[over.match]]),
4369
-
4370
- then the reference binds to the initializer expression lvalue in the
4371
- first case and to the lvalue result of the conversion in the second
4372
- case (or, in either case, to the appropriate base class subobject of
4373
- the object).
4374
- \[*Note 2*: The usual lvalue-to-rvalue [[conv.lval]], array-to-pointer
4375
- [[conv.array]], and function-to-pointer [[conv.func]] standard
4376
- conversions are not needed, and therefore are suppressed, when such
4377
- direct bindings to lvalues are done. — *end note*]
4378
- \[*Example 3*:
4379
- ``` cpp
4380
- double d = 2.0;
4381
- double& rd = d; // rd refers to d
4382
- const double& rcd = d; // rcd refers to d
4383
-
4384
- struct A { };
4385
- struct B : A { operator int&(); } b;
4386
- A& ra = b; // ra refers to A subobject in b
4387
- const A& rca = b; // rca refers to A subobject in b
4388
- int& ir = B(); // ir refers to the result of B::operator int&
4389
- ```
4390
-
4391
- — *end example*]
4392
- - Otherwise, if the reference is an lvalue reference to a type that is
4393
- not const-qualified or is volatile-qualified, the program is
4394
- ill-formed.
4395
- \[*Example 4*:
4396
- ``` cpp
4397
- double& rd2 = 2.0; // error: not an lvalue and reference not const
4398
- int i = 2;
4399
- double& rd3 = i; // error: type mismatch and reference not const
4400
- ```
4401
-
4402
- — *end example*]
4403
- - Otherwise, if the initializer expression
4404
- - is an rvalue (but not a bit-field) or function lvalue and “*cv1*
4405
- `T1`” is reference-compatible with “*cv2* `T2`”, or
4406
- - has a class type (i.e., `T2` is a class type), where `T1` is not
4407
- reference-related to `T2`, and can be converted to an rvalue or
4408
- function lvalue of type “*cv3* `T3`”, where “*cv1* `T1`” is
4409
- reference-compatible with “*cv3* `T3`” (see  [[over.match.ref]]),
4410
-
4411
- then the initializer expression in the first case and the converted
4412
- expression in the second case is called the converted initializer. If
4413
- the converted initializer is a prvalue, its type `T4` is adjusted to
4414
- type “*cv1* `T4`” [[conv.qual]] and the temporary materialization
4415
- conversion [[conv.rval]] is applied. In any case, the reference binds
4416
- to the resulting glvalue (or to an appropriate base class subobject).
4417
- \[*Example 5*:
4418
- ``` cpp
4419
- struct A { };
4420
- struct B : A { } b;
4421
- extern B f();
4422
- const A& rca2 = f(); // binds to the A subobject of the B rvalue.
4423
- A&& rra = f(); // same as above
4424
- struct X {
4425
- operator B();
4426
- operator int&();
4427
- } x;
4428
- const A& r = x; // binds to the A subobject of the result of the conversion
4429
- int i2 = 42;
4430
- int&& rri = static_cast<int&&>(i2); // binds directly to i2
4431
- B&& rrb = x; // binds directly to the result of operator B
4432
- ```
4433
-
4434
- — *end example*]
4435
- - Otherwise:
4436
- - If `T1` or `T2` is a class type and `T1` is not reference-related to
4437
- `T2`, user-defined conversions are considered using the rules for
4438
- copy-initialization of an object of type “*cv1* `T1`” by
4439
- user-defined conversion
4440
- [[dcl.init]], [[over.match.copy]], [[over.match.conv]]; the program
4441
- is ill-formed if the corresponding non-reference copy-initialization
4442
- would be ill-formed. The result of the call to the conversion
4443
- function, as described for the non-reference copy-initialization, is
4444
- then used to direct-initialize the reference. For this
4445
- direct-initialization, user-defined conversions are not considered.
4446
- - Otherwise, the initializer expression is implicitly converted to a
4447
- prvalue of type “`T1`”. The temporary materialization conversion is
4448
- applied, considering the type of the prvalue to be “*cv1* `T1`”, and
4449
- the reference is bound to the result.
4450
-
4451
- If `T1` is reference-related to `T2`:
4452
- - *cv1* shall be the same cv-qualification as, or greater
4453
- cv-qualification than, *cv2*; and
4454
- - if the reference is an rvalue reference, the initializer expression
4455
- shall not be an lvalue. \[*Note 3*: This can be affected by whether
4456
- the initializer expression is move-eligible
4457
- [[expr.prim.id.unqual]]. — *end note*]
4458
-
4459
- \[*Example 6*:
4460
- ``` cpp
4461
- struct Banana { };
4462
- struct Enigma { operator const Banana(); };
4463
- struct Alaska { operator Banana&(); };
4464
- void enigmatic() {
4465
- typedef const Banana ConstBanana;
4466
- Banana &&banana1 = ConstBanana(); // error
4467
- Banana &&banana2 = Enigma(); // error
4468
- Banana &&banana3 = Alaska(); // error
4469
- }
4470
-
4471
- const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
4472
- double&& rrd = 2; // rrd refers to temporary with value 2.0
4473
- const volatile int cvi = 1;
4474
- const int& r2 = cvi; // error: cv-qualifier dropped
4475
- struct A { operator volatile int&(); } a;
4476
- const int& r3 = a; // error: cv-qualifier dropped
4477
- // from result of conversion function
4478
- double d2 = 1.0;
4479
- double&& rrd2 = d2; // error: initializer is lvalue of related type
4480
- struct X { operator int&(); };
4481
- int&& rri2 = X(); // error: result of conversion function is lvalue of related type
4482
- int i3 = 2;
4483
- double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
4484
- ```
4485
-
4486
- — *end example*]
4487
-
4488
- In all cases except the last (i.e., implicitly converting the
4489
- initializer expression to the referenced type), the reference is said to
4490
- *bind directly* to the initializer expression.
4491
-
4492
- [*Note 4*: [[class.temporary]] describes the lifetime of temporaries
4493
- bound to references. — *end note*]
4494
-
4495
- ### List-initialization <a id="dcl.init.list">[[dcl.init.list]]</a>
4496
-
4497
- *List-initialization* is initialization of an object or reference from a
4498
- *braced-init-list*. Such an initializer is called an *initializer list*,
4499
- and the comma-separated *initializer-clause*s of the *initializer-list*
4500
- or *designated-initializer-clause*s of the *designated-initializer-list*
4501
- are called the *elements* of the initializer list. An initializer list
4502
- may be empty. List-initialization can occur in direct-initialization or
4503
- copy-initialization contexts; list-initialization in a
4504
- direct-initialization context is called *direct-list-initialization* and
4505
- list-initialization in a copy-initialization context is called
4506
- *copy-list-initialization*.
4507
-
4508
- [*Note 1*:
4509
-
4510
- List-initialization can be used
4511
-
4512
- - as the initializer in a variable definition [[dcl.init]]
4513
- - as the initializer in a *new-expression* [[expr.new]]
4514
- - in a `return` statement [[stmt.return]]
4515
- - as a *for-range-initializer* [[stmt.iter]]
4516
- - as a function argument [[expr.call]]
4517
- - as a subscript [[expr.sub]]
4518
- - as an argument to a constructor invocation
4519
- [[dcl.init]], [[expr.type.conv]]
4520
- - as an initializer for a non-static data member [[class.mem]]
4521
- - in a *mem-initializer* [[class.base.init]]
4522
- - on the right-hand side of an assignment [[expr.ass]]
4523
-
4524
- [*Example 1*:
4525
-
4526
- ``` cpp
4527
- int a = {1};
4528
- std::complex<double> z{1,2};
4529
- new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
4530
- f( {"Nicholas","Annemarie"} ); // pass list of two elements
4531
- return { "Norah" }; // return list of one element
4532
- int* e {}; // initialization to zero / null pointer
4533
- x = double{1}; // explicitly construct a double
4534
- std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
4535
- ```
4536
-
4537
- — *end example*]
4538
-
4539
- — *end note*]
4540
-
4541
- A constructor is an *initializer-list constructor* if its first
4542
- parameter is of type `std::initializer_list<E>` or reference to
4543
- cv `std::initializer_list<E>` for some type `E`, and either there are no
4544
- other parameters or else all other parameters have default arguments
4545
- [[dcl.fct.default]].
4546
-
4547
- [*Note 2*: Initializer-list constructors are favored over other
4548
- constructors in list-initialization [[over.match.list]]. Passing an
4549
- initializer list as the argument to the constructor template
4550
- `template<class T> C(T)` of a class `C` does not create an
4551
- initializer-list constructor, because an initializer list argument
4552
- causes the corresponding parameter to be a non-deduced context
4553
- [[temp.deduct.call]]. — *end note*]
4554
-
4555
- The template `std::initializer_list` is not predefined; if a standard
4556
- library declaration [[initializer.list.syn]], [[std.modules]] of
4557
- `std::initializer_list` is not reachable from [[module.reach]] a use of
4558
- `std::initializer_list` — even an implicit use in which the type is not
4559
- named [[dcl.spec.auto]] — the program is ill-formed.
4560
-
4561
- List-initialization of an object or reference of type `T` is defined as
4562
- follows:
4563
-
4564
- - If the *braced-init-list* contains a *designated-initializer-list*,
4565
- `T` shall be an aggregate class. The ordered *identifier*s in the
4566
- *designator*s of the *designated-initializer-list* shall form a
4567
- subsequence of the ordered *identifier*s in the direct non-static data
4568
- members of `T`. Aggregate initialization is performed
4569
- [[dcl.init.aggr]].
4570
- \[*Example 2*:
4571
- ``` cpp
4572
- struct A { int x; int y; int z; };
4573
- A a{.y = 2, .x = 1}; // error: designator order does not match declaration order
4574
- A b{.x = 1, .z = 2}; // OK, b.y initialized to 0
4575
- ```
4576
-
4577
- — *end example*]
4578
- - If `T` is an aggregate class and the initializer list has a single
4579
- element of type *cv* `U`, where `U` is `T` or a class derived from
4580
- `T`, the object is initialized from that element (by
4581
- copy-initialization for copy-list-initialization, or by
4582
- direct-initialization for direct-list-initialization).
4583
- - Otherwise, if `T` is a character array and the initializer list has a
4584
- single element that is an appropriately-typed *string-literal*
4585
- [[dcl.init.string]], initialization is performed as described in that
4586
- subclause.
4587
- - Otherwise, if `T` is an aggregate, aggregate initialization is
4588
- performed [[dcl.init.aggr]].
4589
- \[*Example 3*:
4590
- ``` cpp
4591
- double ad[] = { 1, 2.0 }; // OK
4592
- int ai[] = { 1, 2.0 }; // error: narrowing
4593
-
4594
- struct S2 {
4595
- int m1;
4596
- double m2, m3;
4597
- };
4598
- S2 s21 = { 1, 2, 3.0 }; // OK
4599
- S2 s22 { 1.0, 2, 3 }; // error: narrowing
4600
- S2 s23 { }; // OK, default to 0,0,0
4601
- ```
4602
-
4603
- — *end example*]
4604
- - Otherwise, if the initializer list has no elements and `T` is a class
4605
- type with a default constructor, the object is value-initialized.
4606
- - Otherwise, if `T` is a specialization of `std::initializer_list<E>`,
4607
- the object is constructed as described below.
4608
- - Otherwise, if `T` is a class type, constructors are considered. The
4609
- applicable constructors are enumerated and the best one is chosen
4610
- through overload resolution [[over.match]], [[over.match.list]]. If a
4611
- narrowing conversion (see below) is required to convert any of the
4612
- arguments, the program is ill-formed.
4613
- \[*Example 4*:
4614
- ``` cpp
4615
- struct S {
4616
- S(std::initializer_list<double>); // #1
4617
- S(std::initializer_list<int>); // #2
4618
- S(); // #3
4619
- // ...
4620
- };
4621
- S s1 = { 1.0, 2.0, 3.0 }; // invoke #1
4622
- S s2 = { 1, 2, 3 }; // invoke #2
4623
- S s3 = { }; // invoke #3
4624
- ```
4625
-
4626
- — *end example*]
4627
- \[*Example 5*:
4628
- ``` cpp
4629
- struct Map {
4630
- Map(std::initializer_list<std::pair<std::string,int>>);
4631
- };
4632
- Map ship = {{"Sophie",14}, {"Surprise",28}};
4633
- ```
4634
-
4635
- — *end example*]
4636
- \[*Example 6*:
4637
- ``` cpp
4638
- struct S {
4639
- // no initializer-list constructors
4640
- S(int, double, double); // #1
4641
- S(); // #2
4642
- // ...
4643
- };
4644
- S s1 = { 1, 2, 3.0 }; // OK, invoke #1
4645
- S s2 { 1.0, 2, 3 }; // error: narrowing
4646
- S s3 { }; // OK, invoke #2
4647
- ```
4648
-
4649
- — *end example*]
4650
- - Otherwise, if `T` is an enumeration with a fixed underlying type
4651
- [[dcl.enum]] `U`, the *initializer-list* has a single element `v`, `v`
4652
- can be implicitly converted to `U`, and the initialization is
4653
- direct-list-initialization, the object is initialized with the value
4654
- `T(v)` [[expr.type.conv]]; if a narrowing conversion is required to
4655
- convert `v` to `U`, the program is ill-formed.
4656
- \[*Example 7*:
4657
- ``` cpp
4658
- enum byte : unsigned char { };
4659
- byte b { 42 }; // OK
4660
- byte c = { 42 }; // error
4661
- byte d = byte{ 42 }; // OK; same value as b
4662
- byte e { -1 }; // error
4663
-
4664
- struct A { byte b; };
4665
- A a1 = { { 42 } }; // error
4666
- A a2 = { byte{ 42 } }; // OK
4667
-
4668
- void f(byte);
4669
- f({ 42 }); // error
4670
-
4671
- enum class Handle : uint32_t { Invalid = 0 };
4672
- Handle h { 42 }; // OK
4673
- ```
4674
-
4675
- — *end example*]
4676
- - Otherwise, if the initializer list has a single element of type `E`
4677
- and either `T` is not a reference type or its referenced type is
4678
- reference-related to `E`, the object or reference is initialized from
4679
- that element (by copy-initialization for copy-list-initialization, or
4680
- by direct-initialization for direct-list-initialization); if a
4681
- narrowing conversion (see below) is required to convert the element to
4682
- `T`, the program is ill-formed.
4683
- \[*Example 8*:
4684
- ``` cpp
4685
- int x1 {2}; // OK
4686
- int x2 {2.0}; // error: narrowing
4687
- ```
4688
-
4689
- — *end example*]
4690
- - Otherwise, if `T` is a reference type, a prvalue is generated. The
4691
- prvalue initializes its result object by copy-list-initialization from
4692
- the initializer list. The prvalue is then used to direct-initialize
4693
- the reference. The type of the prvalue is the type referenced by `T`,
4694
- unless `T` is “reference to array of unknown bound of `U`”, in which
4695
- case the type of the prvalue is the type of `x` in the declaration
4696
- `U x[] H`, where H is the initializer list.
4697
- \[*Note 3*: As usual, the binding will fail and the program is
4698
- ill-formed if the reference type is an lvalue reference to a non-const
4699
- type. — *end note*]
4700
- \[*Example 9*:
4701
- ``` cpp
4702
- struct S {
4703
- S(std::initializer_list<double>); // #1
4704
- S(const std::string&); // #2
4705
- // ...
4706
- };
4707
- const S& r1 = { 1, 2, 3.0 }; // OK, invoke #1
4708
- const S& r2 { "Spinach" }; // OK, invoke #2
4709
- S& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
4710
- const int& i1 = { 1 }; // OK
4711
- const int& i2 = { 1.1 }; // error: narrowing
4712
- const int (&iar)[2] = { 1, 2 }; // OK, iar is bound to temporary array
4713
-
4714
- struct A { } a;
4715
- struct B { explicit B(const A&); };
4716
- const B& b2{a}; // error: cannot copy-list-initialize B temporary from A
4717
- ```
4718
-
4719
- — *end example*]
4720
- - Otherwise, if the initializer list has no elements, the object is
4721
- value-initialized.
4722
- \[*Example 10*:
4723
- ``` cpp
4724
- int** pp {}; // initialized to null pointer
4725
- ```
4726
-
4727
- — *end example*]
4728
- - Otherwise, the program is ill-formed.
4729
- \[*Example 11*:
4730
- ``` cpp
4731
- struct A { int i; int j; };
4732
- A a1 { 1, 2 }; // aggregate initialization
4733
- A a2 { 1.2 }; // error: narrowing
4734
- struct B {
4735
- B(std::initializer_list<int>);
4736
- };
4737
- B b1 { 1, 2 }; // creates initializer_list<int> and calls constructor
4738
- B b2 { 1, 2.0 }; // error: narrowing
4739
- struct C {
4740
- C(int i, double j);
4741
- };
4742
- C c1 = { 1, 2.2 }; // calls constructor with arguments (1, 2.2)
4743
- C c2 = { 1.1, 2 }; // error: narrowing
4744
-
4745
- int j { 1 }; // initialize to 1
4746
- int k { }; // initialize to 0
4747
- ```
4748
-
4749
- — *end example*]
4750
-
4751
- Within the *initializer-list* of a *braced-init-list*, the
4752
- *initializer-clause*s, including any that result from pack expansions
4753
- [[temp.variadic]], are evaluated in the order in which they appear. That
4754
- is, every value computation and side effect associated with a given
4755
- *initializer-clause* is sequenced before every value computation and
4756
- side effect associated with any *initializer-clause* that follows it in
4757
- the comma-separated list of the *initializer-list*.
4758
-
4759
- [*Note 4*: This evaluation ordering holds regardless of the semantics
4760
- of the initialization; for example, it applies when the elements of the
4761
- *initializer-list* are interpreted as arguments of a constructor call,
4762
- even though ordinarily there are no sequencing constraints on the
4763
- arguments of a call. — *end note*]
4764
-
4765
- An object of type `std::initializer_list<E>` is constructed from an
4766
- initializer list as if the implementation generated and materialized
4767
- [[conv.rval]] a prvalue of type “array of N `const E`”, where N is the
4768
- number of elements in the initializer list. Each element of that array
4769
- is copy-initialized with the corresponding element of the initializer
4770
- list, and the `std::initializer_list<E>` object is constructed to refer
4771
- to that array.
4772
-
4773
- [*Note 5*: A constructor or conversion function selected for the copy
4774
- is required to be accessible [[class.access]] in the context of the
4775
- initializer list. — *end note*]
4776
-
4777
- If a narrowing conversion is required to initialize any of the elements,
4778
- the program is ill-formed.
4779
-
4780
- [*Example 12*:
4781
-
4782
- ``` cpp
4783
- struct X {
4784
- X(std::initializer_list<double> v);
4785
- };
4786
- X x{ 1,2,3 };
4787
- ```
4788
-
4789
- The initialization will be implemented in a way roughly equivalent to
4790
- this:
4791
-
4792
- ``` cpp
4793
- const double __a[3] = {double{1}, double{2}, double{3}};
4794
- X x(std::initializer_list<double>(__a, __a+3));
4795
- ```
4796
-
4797
- assuming that the implementation can construct an `initializer_list`
4798
- object with a pair of pointers.
4799
-
4800
- — *end example*]
4801
-
4802
- The array has the same lifetime as any other temporary object
4803
- [[class.temporary]], except that initializing an `initializer_list`
4804
- object from the array extends the lifetime of the array exactly like
4805
- binding a reference to a temporary.
4806
-
4807
- [*Example 13*:
4808
-
4809
- ``` cpp
4810
- typedef std::complex<double> cmplx;
4811
- std::vector<cmplx> v1 = { 1, 2, 3 };
4812
-
4813
- void f() {
4814
- std::vector<cmplx> v2{ 1, 2, 3 };
4815
- std::initializer_list<int> i3 = { 1, 2, 3 };
4816
- }
4817
-
4818
- struct A {
4819
- std::initializer_list<int> i4;
4820
- A() : i4{ 1, 2, 3 } {} // ill-formed, would create a dangling reference
4821
- };
4822
- ```
4823
-
4824
- For `v1` and `v2`, the `initializer_list` object is a parameter in a
4825
- function call, so the array created for `{ 1, 2, 3 }` has
4826
- full-expression lifetime. For `i3`, the `initializer_list` object is a
4827
- variable, so the array persists for the lifetime of the variable. For
4828
- `i4`, the `initializer_list` object is initialized in the constructor’s
4829
- *ctor-initializer* as if by binding a temporary array to a reference
4830
- member, so the program is ill-formed [[class.base.init]].
4831
-
4832
- — *end example*]
4833
-
4834
- [*Note 6*: The implementation is free to allocate the array in
4835
- read-only memory if an explicit array with the same initializer can be
4836
- so allocated. — *end note*]
4837
-
4838
- A *narrowing conversion* is an implicit conversion
4839
-
4840
- - from a floating-point type to an integer type, or
4841
- - from a floating-point type `T` to another floating-point type whose
4842
- floating-point conversion rank is neither greater than nor equal to
4843
- that of `T`, except where the source is a constant expression and the
4844
- actual value after conversion is within the range of values that can
4845
- be represented (even if it cannot be represented exactly), or
4846
- - from an integer type or unscoped enumeration type to a floating-point
4847
- type, except where the source is a constant expression and the actual
4848
- value after conversion will fit into the target type and will produce
4849
- the original value when converted back to the original type, or
4850
- - from an integer type or unscoped enumeration type to an integer type
4851
- that cannot represent all the values of the original type, except
4852
- where
4853
- - the source is a bit-field whose width w is less than that of its
4854
- type (or, for an enumeration type, its underlying type) and the
4855
- target type can represent all the values of a hypothetical extended
4856
- integer type with width w and with the same signedness as the
4857
- original type or
4858
- - the source is a constant expression whose value after integral
4859
- promotions will fit into the target type, or
4860
- - from a pointer type or a pointer-to-member type to `bool`.
4861
-
4862
- [*Note 7*: As indicated above, such conversions are not allowed at the
4863
- top level in list-initializations. — *end note*]
4864
-
4865
- [*Example 14*:
4866
-
4867
- ``` cpp
4868
- int x = 999; // x is not a constant expression
4869
- const int y = 999;
4870
- const int z = 99;
4871
- char c1 = x; // OK, though it potentially narrows (in this case, it does narrow)
4872
- char c2{x}; // error: potentially narrows
4873
- char c3{y}; // error: narrows (assuming char is 8 bits)
4874
- char c4{z}; // OK, no narrowing needed
4875
- unsigned char uc1 = {5}; // OK, no narrowing needed
4876
- unsigned char uc2 = {-1}; // error: narrows
4877
- unsigned int ui1 = {-1}; // error: narrows
4878
- signed int si1 =
4879
- { (unsigned int)-1 }; // error: narrows
4880
- int ii = {2.0}; // error: narrows
4881
- float f1 { x }; // error: potentially narrows
4882
- float f2 { 7 }; // OK, 7 can be exactly represented as a float
4883
- bool b = {"meow"}; // error: narrows
4884
- int f(int);
4885
- int a[] = { 2, f(2), f(2.0) }; // OK, the double-to-int conversion is not at the top level
4886
- ```
4887
-
4888
- — *end example*]
4889
-
4890
- ## Function definitions <a id="dcl.fct.def">[[dcl.fct.def]]</a>
4891
-
4892
- ### In general <a id="dcl.fct.def.general">[[dcl.fct.def.general]]</a>
4893
-
4894
- Function definitions have the form
4895
-
4896
- ``` bnf
4897
- function-definition:
4898
- attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator virt-specifier-seqₒₚₜ function-body
4899
- attribute-specifier-seqₒₚₜ decl-specifier-seqₒₚₜ declarator requires-clause function-body
4900
- ```
4901
-
4902
- ``` bnf
4903
- function-body:
4904
- ctor-initializerₒₚₜ compound-statement
4905
- function-try-block
4906
- '=' default ';'
4907
- '=' delete ';'
4908
- ```
4909
-
4910
- Any informal reference to the body of a function should be interpreted
4911
- as a reference to the non-terminal *function-body*. The optional
4912
- *attribute-specifier-seq* in a *function-definition* appertains to the
4913
- function. A *virt-specifier-seq* can be part of a *function-definition*
4914
- only if it is a *member-declaration* [[class.mem]].
4915
-
4916
- In a *function-definition*, either `void` *declarator* `;` or
4917
- *declarator* `;` shall be a well-formed function declaration as
4918
- described in  [[dcl.fct]]. A function shall be defined only in namespace
4919
- or class scope. The type of a parameter or the return type for a
4920
- function definition shall not be a (possibly cv-qualified) class type
4921
- that is incomplete or abstract within the function body unless the
4922
- function is deleted [[dcl.fct.def.delete]].
4923
-
4924
- [*Example 1*:
4925
-
4926
- A simple example of a complete function definition is
4927
-
4928
- ``` cpp
4929
- int max(int a, int b, int c) {
4930
- int m = (a > b) ? a : b;
4931
- return (m > c) ? m : c;
4932
- }
4933
- ```
4934
-
4935
- Here `int` is the *decl-specifier-seq*; `max(int` `a,` `int` `b,` `int`
4936
- `c)` is the *declarator*; `{ /* ... */ }` is the *function-body*.
4937
-
4938
- — *end example*]
4939
-
4940
- A *ctor-initializer* is used only in a constructor; see  [[class.ctor]]
4941
- and  [[class.init]].
4942
-
4943
- [*Note 1*: A *cv-qualifier-seq* affects the type of `this` in the body
4944
- of a member function; see  [[expr.prim.this]]. — *end note*]
4945
-
4946
- [*Note 2*:
4947
-
4948
- Unused parameters need not be named. For example,
4949
-
4950
- ``` cpp
4951
- void print(int a, int) {
4952
- std::printf("a = %d\n",a);
4953
- }
4954
- ```
4955
-
4956
- — *end note*]
4957
-
4958
- A *function-local predefined variable* is a variable with static storage
4959
- duration that is implicitly defined in a function parameter scope.
4960
-
4961
- The function-local predefined variable `__func__` is defined as if a
4962
- definition of the form
4963
-
4964
- ``` cpp
4965
- static const char __func__[] = "function-name";
4966
- ```
4967
-
4968
- had been provided, where `function-name` is an *implementation-defined*
4969
- string. It is unspecified whether such a variable has an address
4970
- distinct from that of any other object in the program.[^8]
4971
-
4972
- [*Example 2*:
4973
-
4974
- ``` cpp
4975
- struct S {
4976
- S() : s(__func__) { } // OK
4977
- const char* s;
4978
- };
4979
- void f(const char* s = __func__); // error: __func__ is undeclared
4980
- ```
4981
-
4982
- — *end example*]
4983
-
4984
- ### Explicitly-defaulted functions <a id="dcl.fct.def.default">[[dcl.fct.def.default]]</a>
4985
-
4986
- A function definition whose *function-body* is of the form `= default ;`
4987
- is called an *explicitly-defaulted* definition. A function that is
4988
- explicitly defaulted shall
4989
-
4990
- - be a special member function or a comparison operator function
4991
- [[over.binary]], and
4992
- - not have default arguments.
4993
-
4994
- An explicitly defaulted special member function `F₁` is allowed to
4995
- differ from the corresponding special member function `F₂` that would
4996
- have been implicitly declared, as follows:
4997
-
4998
- - `F₁` and `F₂` may have differing *ref-qualifier*s;
4999
- - if `F₂` has an implicit object parameter of type “reference to `C`”,
5000
- `F₁` may be an explicit object member function whose explicit object
5001
- parameter is of type “reference to `C`”, in which case the type of
5002
- `F₁` would differ from the type of `F₂` in that the type of `F₁` has
5003
- an additional parameter;
5004
- - `F₁` and `F₂` may have differing exception specifications; and
5005
- - if `F₂` has a non-object parameter of type `const C&`, the
5006
- corresponding non-object parameter of `F₁` may be of type `C&`.
5007
-
5008
- If the type of `F₁` differs from the type of `F₂` in a way other than as
5009
- allowed by the preceding rules, then:
5010
-
5011
- - if `F₁` is an assignment operator, and the return type of `F₁` differs
5012
- from the return type of `F₂` or `F₁`’s non-object parameter type is
5013
- not a reference, the program is ill-formed;
5014
- - otherwise, if `F₁` is explicitly defaulted on its first declaration,
5015
- it is defined as deleted;
5016
- - otherwise, the program is ill-formed.
5017
-
5018
- A function explicitly defaulted on its first declaration is implicitly
5019
- inline [[dcl.inline]], and is implicitly constexpr [[dcl.constexpr]] if
5020
- it is constexpr-suitable.
5021
-
5022
- [*Example 1*:
5023
-
5024
- ``` cpp
5025
- struct S {
5026
- S(int a = 0) = default; // error: default argument
5027
- void operator=(const S&) = default; // error: non-matching return type
5028
- ~S() noexcept(false) = default; // OK, despite mismatched exception specification
5029
- private:
5030
- int i;
5031
- S(S&); // OK, private copy constructor
5032
- };
5033
- S::S(S&) = default; // OK, defines copy constructor
5034
-
5035
- struct T {
5036
- T();
5037
- T(T &&) noexcept(false);
5038
- };
5039
- struct U {
5040
- T t;
5041
- U();
5042
- U(U &&) noexcept = default;
5043
- };
5044
- U u1;
5045
- U u2 = static_cast<U&&>(u1); // OK, calls std::terminate if T::T(T&&) throws
5046
- ```
5047
-
5048
- — *end example*]
5049
-
5050
- Explicitly-defaulted functions and implicitly-declared functions are
5051
- collectively called *defaulted* functions, and the implementation shall
5052
- provide implicit definitions for them
5053
- [[class.ctor]], [[class.dtor]], [[class.copy.ctor]], [[class.copy.assign]]
5054
- as described below, including possibly defining them as deleted. A
5055
- defaulted prospective destructor [[class.dtor]] that is not a destructor
5056
- is defined as deleted. A defaulted special member function that is
5057
- neither a prospective destructor nor an eligible special member function
5058
- [[special]] is defined as deleted. A function is *user-provided* if it
5059
- is user-declared and not explicitly defaulted or deleted on its first
5060
- declaration. A user-provided explicitly-defaulted function (i.e.,
5061
- explicitly defaulted after its first declaration) is implicitly defined
5062
- at the point where it is explicitly defaulted; if such a function is
5063
- implicitly defined as deleted, the program is ill-formed. A
5064
- non-user-provided defaulted function (i.e., implicitly declared or
5065
- explicitly defaulted in the class) that is not defined as deleted is
5066
- implicitly defined when it is odr-used [[basic.def.odr]] or needed for
5067
- constant evaluation [[expr.const]].
5068
-
5069
- [*Note 1*: Declaring a function as defaulted after its first
5070
- declaration can provide efficient execution and concise definition while
5071
- enabling a stable binary interface to an evolving code
5072
- base. — *end note*]
5073
-
5074
- [*Example 2*:
5075
-
5076
- ``` cpp
5077
- struct trivial {
5078
- trivial() = default;
5079
- trivial(const trivial&) = default;
5080
- trivial(trivial&&) = default;
5081
- trivial& operator=(const trivial&) = default;
5082
- trivial& operator=(trivial&&) = default;
5083
- ~trivial() = default;
5084
- };
5085
-
5086
- struct nontrivial1 {
5087
- nontrivial1();
5088
- };
5089
- nontrivial1::nontrivial1() = default; // not first declaration
5090
- ```
5091
-
5092
- — *end example*]
5093
-
5094
- ### Deleted definitions <a id="dcl.fct.def.delete">[[dcl.fct.def.delete]]</a>
5095
-
5096
- A *deleted definition* of a function is a function definition whose
5097
- *function-body* is of the form `= delete ;` or an explicitly-defaulted
5098
- definition of the function where the function is defined as deleted. A
5099
- *deleted function* is a function with a deleted definition or a function
5100
- that is implicitly defined as deleted.
5101
-
5102
- A program that refers to a deleted function implicitly or explicitly,
5103
- other than to declare it, is ill-formed.
5104
-
5105
- [*Note 1*: This includes calling the function implicitly or explicitly
5106
- and forming a pointer or pointer-to-member to the function. It applies
5107
- even for references in expressions that are not potentially-evaluated.
5108
- For an overload set, only the function selected by overload resolution
5109
- is referenced. The implicit odr-use [[term.odr.use]] of a virtual
5110
- function does not, by itself, constitute a reference. — *end note*]
5111
-
5112
- [*Example 1*:
5113
-
5114
- One can prevent default initialization and initialization by
5115
- non-`double`s with
5116
-
5117
- ``` cpp
5118
- struct onlydouble {
5119
- onlydouble() = delete; // OK, but redundant
5120
- template<class T>
5121
- onlydouble(T) = delete;
5122
- onlydouble(double);
5123
- };
5124
- ```
5125
-
5126
- — *end example*]
5127
-
5128
- [*Example 2*:
5129
-
5130
- One can prevent use of a class in certain *new-expression*s by using
5131
- deleted definitions of a user-declared `operator new` for that class.
5132
-
5133
- ``` cpp
5134
- struct sometype {
5135
- void* operator new(std::size_t) = delete;
5136
- void* operator new[](std::size_t) = delete;
5137
- };
5138
- sometype* p = new sometype; // error: deleted class operator new
5139
- sometype* q = new sometype[3]; // error: deleted class operator new[]
5140
- ```
5141
-
5142
- — *end example*]
5143
-
5144
- [*Example 3*:
5145
-
5146
- One can make a class uncopyable, i.e., move-only, by using deleted
5147
- definitions of the copy constructor and copy assignment operator, and
5148
- then providing defaulted definitions of the move constructor and move
5149
- assignment operator.
5150
-
5151
- ``` cpp
5152
- struct moveonly {
5153
- moveonly() = default;
5154
- moveonly(const moveonly&) = delete;
5155
- moveonly(moveonly&&) = default;
5156
- moveonly& operator=(const moveonly&) = delete;
5157
- moveonly& operator=(moveonly&&) = default;
5158
- ~moveonly() = default;
5159
- };
5160
- moveonly* p;
5161
- moveonly q(*p); // error: deleted copy constructor
5162
- ```
5163
-
5164
- — *end example*]
5165
-
5166
- A deleted function is implicitly an inline function [[dcl.inline]].
5167
-
5168
- [*Note 2*: The one-definition rule [[basic.def.odr]] applies to deleted
5169
- definitions. — *end note*]
5170
-
5171
- A deleted definition of a function shall be the first declaration of the
5172
- function or, for an explicit specialization of a function template, the
5173
- first declaration of that specialization. An implicitly declared
5174
- allocation or deallocation function [[basic.stc.dynamic]] shall not be
5175
- defined as deleted.
5176
-
5177
- [*Example 4*:
5178
-
5179
- ``` cpp
5180
- struct sometype {
5181
- sometype();
5182
- };
5183
- sometype::sometype() = delete; // error: not first declaration
5184
- ```
5185
-
5186
- — *end example*]
5187
-
5188
- ### Coroutine definitions <a id="dcl.fct.def.coroutine">[[dcl.fct.def.coroutine]]</a>
5189
-
5190
- A function is a *coroutine* if its *function-body* encloses a
5191
- *coroutine-return-statement* [[stmt.return.coroutine]], an
5192
- *await-expression* [[expr.await]], or a *yield-expression*
5193
- [[expr.yield]]. The *parameter-declaration-clause* of the coroutine
5194
- shall not terminate with an ellipsis that is not part of a
5195
- *parameter-declaration*.
5196
-
5197
- [*Example 1*:
5198
-
5199
- ``` cpp
5200
- task<int> f();
5201
-
5202
- task<void> g1() {
5203
- int i = co_await f();
5204
- std::cout << "f() => " << i << std::endl;
5205
- }
5206
-
5207
- template <typename... Args>
5208
- task<void> g2(Args&&...) { // OK, ellipsis is a pack expansion
5209
- int i = co_await f();
5210
- std::cout << "f() => " << i << std::endl;
5211
- }
5212
-
5213
- task<void> g3(int a, ...) { // error: variable parameter list not allowed
5214
- int i = co_await f();
5215
- std::cout << "f() => " << i << std::endl;
5216
- }
5217
- ```
5218
-
5219
- — *end example*]
5220
-
5221
- The *promise type* of a coroutine is
5222
- `std::coroutine_traits<R, P₁, …, Pₙ>::promise_type`, where `R` is the
5223
- return type of the function, and `P₁` … `Pₙ` is the sequence of types of
5224
- the non-object function parameters, preceded by the type of the object
5225
- parameter [[dcl.fct]] if the coroutine is a non-static member function.
5226
- The promise type shall be a class type.
5227
-
5228
- In the following, `pᵢ` is an lvalue of type `Pᵢ`, where `p₁` denotes the
5229
- object parameter and `p_i+1` denotes the iᵗʰ non-object function
5230
- parameter for a non-static member function, and `pᵢ` denotes the iᵗʰ
5231
- function parameter otherwise. For a non-static member function, `q₁` is
5232
- an lvalue that denotes `*this`; any other `qᵢ` is an lvalue that denotes
5233
- the parameter copy corresponding to `pᵢ`, as described below.
5234
-
5235
- A coroutine behaves as if its *function-body* were replaced by:
5236
-
5237
- ``` bnf
5238
- '{'
5239
- *promise-type* promise *promise-constructor-arguments* ';'
5240
- % FIXME: promise'.get_return_object()' ';'
5241
- % ... except that it's not a discarded-value expression
5242
- 'try' '{'
5243
- 'co_await' 'promise.initial_suspend()' ';'
5244
- function-body
5245
- '} catch ( ... ) {'
5246
- 'if (!initial-await-resume-called)'
5247
- 'throw' ';'
5248
- 'promise.unhandled_exception()' ';'
5249
- '}'
5250
- final-suspend ':'
5251
- 'co_await' 'promise.final_suspend()' ';'
5252
- '}'
5253
- ```
5254
-
5255
- where
5256
-
5257
- - the *await-expression* containing the call to `initial_suspend` is the
5258
- *initial await expression*, and
5259
- - the *await-expression* containing the call to `final_suspend` is the
5260
- *final await expression*, and
5261
- - *initial-await-resume-called* is initially `false` and is set to
5262
- `true` immediately before the evaluation of the *await-resume*
5263
- expression [[expr.await]] of the initial await expression, and
5264
- - *promise-type* denotes the promise type, and
5265
- - the object denoted by the exposition-only name *`promise`* is the
5266
- *promise object* of the coroutine, and
5267
- - the label denoted by the name *`final-suspend`* is defined for
5268
- exposition only [[stmt.return.coroutine]], and
5269
- - *promise-constructor-arguments* is determined as follows: overload
5270
- resolution is performed on a promise constructor call created by
5271
- assembling an argument list `q₁` … `qₙ`. If a viable constructor is
5272
- found [[over.match.viable]], then *promise-constructor-arguments* is
5273
- `(q₁, …, qₙ)`, otherwise *promise-constructor-arguments* is empty, and
5274
- - a coroutine is suspended at the *initial suspend point* if it is
5275
- suspended at the initial await expression, and
5276
- - a coroutine is suspended at a *final suspend point* if it is suspended
5277
- - at a final await expression or
5278
- - due to an exception exiting from `unhandled_exception()`.
5279
-
5280
- If searches for the names `return_void` and `return_value` in the scope
5281
- of the promise type each find any declarations, the program is
5282
- ill-formed.
5283
-
5284
- [*Note 1*: If `return_void` is found, flowing off the end of a
5285
- coroutine is equivalent to a `co_return` with no operand. Otherwise,
5286
- flowing off the end of a coroutine results in undefined behavior
5287
- [[stmt.return.coroutine]]. — *end note*]
5288
-
5289
- The expression `promise.get_return_object()` is used to initialize the
5290
- returned reference or prvalue result object of a call to a coroutine.
5291
- The call to `get_return_object` is sequenced before the call to
5292
- `initial_suspend` and is invoked at most once.
5293
-
5294
- A suspended coroutine can be resumed to continue execution by invoking a
5295
- resumption member function [[coroutine.handle.resumption]] of a
5296
- coroutine handle [[coroutine.handle]] that refers to the coroutine. The
5297
- evaluation that invoked a resumption member function is called the
5298
- *resumer*. Invoking a resumption member function for a coroutine that is
5299
- not suspended results in undefined behavior.
5300
-
5301
- An implementation may need to allocate additional storage for a
5302
- coroutine. This storage is known as the *coroutine state* and is
5303
- obtained by calling a non-array allocation function
5304
- [[basic.stc.dynamic.allocation]]. The allocation function’s name is
5305
- looked up by searching for it in the scope of the promise type.
5306
-
5307
- - If the search finds any declarations, overload resolution is performed
5308
- on a function call created by assembling an argument list. The first
5309
- argument is the amount of space requested, and is a prvalue of type
5310
- `std::size_t`. The lvalues `p₁` … `pₙ` are the successive arguments.
5311
- If no viable function is found [[over.match.viable]], overload
5312
- resolution is performed again on a function call created by passing
5313
- just the amount of space required as a prvalue of type `std::size_t`.
5314
- - If the search finds no declarations, a search is performed in the
5315
- global scope. Overload resolution is performed on a function call
5316
- created by passing the amount of space required as a prvalue of type
5317
- `std::size_t`.
5318
-
5319
- If a search for the name `get_return_object_on_allocation_failure` in
5320
- the scope of the promise type [[class.member.lookup]] finds any
5321
- declarations, then the result of a call to an allocation function used
5322
- to obtain storage for the coroutine state is assumed to return `nullptr`
5323
- if it fails to obtain storage, and if a global allocation function is
5324
- selected, the `::operator new(size_t, nothrow_t)` form is used. The
5325
- allocation function used in this case shall have a non-throwing
5326
- *noexcept-specifier*. If the allocation function returns `nullptr`, the
5327
- coroutine returns control to the caller of the coroutine and the return
5328
- value is obtained by a call to
5329
- `T::get_return_object_on_allocation_failure()`, where `T` is the promise
5330
- type.
5331
-
5332
- [*Example 2*:
5333
-
5334
- ``` cpp
5335
- #include <iostream>
5336
- #include <coroutine>
5337
-
5338
- // ::operator new(size_t, nothrow_t) will be used if allocation is needed
5339
- struct generator {
5340
- struct promise_type;
5341
- using handle = std::coroutine_handle<promise_type>;
5342
- struct promise_type {
5343
- int current_value;
5344
- static auto get_return_object_on_allocation_failure() { return generator{nullptr}; }
5345
- auto get_return_object() { return generator{handle::from_promise(*this)}; }
5346
- auto initial_suspend() { return std::suspend_always{}; }
5347
- auto final_suspend() noexcept { return std::suspend_always{}; }
5348
- void unhandled_exception() { std::terminate(); }
5349
- void return_void() {}
5350
- auto yield_value(int value) {
5351
- current_value = value;
5352
- return std::suspend_always{};
5353
- }
5354
- };
5355
- bool move_next() { return coro ? (coro.resume(), !coro.done()) : false; }
5356
- int current_value() { return coro.promise().current_value; }
5357
- generator(generator const&) = delete;
5358
- generator(generator && rhs) : coro(rhs.coro) { rhs.coro = nullptr; }
5359
- ~generator() { if (coro) coro.destroy(); }
5360
- private:
5361
- generator(handle h) : coro(h) {}
5362
- handle coro;
5363
- };
5364
- generator f() { co_yield 1; co_yield 2; }
5365
- int main() {
5366
- auto g = f();
5367
- while (g.move_next()) std::cout << g.current_value() << std::endl;
5368
- }
5369
- ```
5370
-
5371
- — *end example*]
5372
-
5373
- The coroutine state is destroyed when control flows off the end of the
5374
- coroutine or the `destroy` member function
5375
- [[coroutine.handle.resumption]] of a coroutine handle
5376
- [[coroutine.handle]] that refers to the coroutine is invoked. In the
5377
- latter case, control in the coroutine is considered to be transferred
5378
- out of the function [[stmt.dcl]]. The storage for the coroutine state is
5379
- released by calling a non-array deallocation function
5380
- [[basic.stc.dynamic.deallocation]]. If `destroy` is called for a
5381
- coroutine that is not suspended, the program has undefined behavior.
5382
-
5383
- The deallocation function’s name is looked up by searching for it in the
5384
- scope of the promise type. If nothing is found, a search is performed in
5385
- the global scope. If both a usual deallocation function with only a
5386
- pointer parameter and a usual deallocation function with both a pointer
5387
- parameter and a size parameter are found, then the selected deallocation
5388
- function shall be the one with two parameters. Otherwise, the selected
5389
- deallocation function shall be the function with one parameter. If no
5390
- usual deallocation function is found, the program is ill-formed. The
5391
- selected deallocation function shall be called with the address of the
5392
- block of storage to be reclaimed as its first argument. If a
5393
- deallocation function with a parameter of type `std::size_t` is used,
5394
- the size of the block is passed as the corresponding argument.
5395
-
5396
- When a coroutine is invoked, after initializing its parameters
5397
- [[expr.call]], a copy is created for each coroutine parameter. For a
5398
- parameter of type cv `T`, the copy is a variable of type cv `T` with
5399
- automatic storage duration that is direct-initialized from an xvalue of
5400
- type `T` referring to the parameter.
5401
-
5402
- [*Note 2*: An original parameter object is never a const or volatile
5403
- object [[basic.type.qualifier]]. — *end note*]
5404
-
5405
- The initialization and destruction of each parameter copy occurs in the
5406
- context of the called coroutine. Initializations of parameter copies are
5407
- sequenced before the call to the coroutine promise constructor and
5408
- indeterminately sequenced with respect to each other. The lifetime of
5409
- parameter copies ends immediately after the lifetime of the coroutine
5410
- promise object ends.
5411
-
5412
- [*Note 3*: If a coroutine has a parameter passed by reference, resuming
5413
- the coroutine after the lifetime of the entity referred to by that
5414
- parameter has ended is likely to result in undefined
5415
- behavior. — *end note*]
5416
-
5417
- If the evaluation of the expression `promise.unhandled_exception()`
5418
- exits via an exception, the coroutine is considered suspended at the
5419
- final suspend point and the exception propagates to the caller or
5420
- resumer.
5421
-
5422
- The expression `co_await` `promise.final_suspend()` shall not be
5423
- potentially-throwing [[except.spec]].
5424
-
5425
- ## Structured binding declarations <a id="dcl.struct.bind">[[dcl.struct.bind]]</a>
5426
-
5427
- A structured binding declaration introduces the *identifier*s `v₀`,
5428
- `v₁`, `v₂`, … of the *identifier-list* as names of *structured
5429
- binding*s. Let cv denote the *cv-qualifier*s in the *decl-specifier-seq*
5430
- and *S* consist of the *storage-class-specifier*s of the
5431
- *decl-specifier-seq* (if any). A cv that includes `volatile` is
5432
- deprecated; see  [[depr.volatile.type]]. First, a variable with a unique
5433
- name *`e`* is introduced. If the *assignment-expression* in the
5434
- *initializer* has array type *cv1* `A` and no *ref-qualifier* is
5435
- present, *`e`* is defined by
5436
-
5437
- ``` bnf
5438
- attribute-specifier-seqₒₚₜ *S* cv 'A' e ';'
5439
- ```
5440
-
5441
- and each element is copy-initialized or direct-initialized from the
5442
- corresponding element of the *assignment-expression* as specified by the
5443
- form of the *initializer*. Otherwise, *`e`* is defined as-if by
5444
-
5445
- ``` bnf
5446
- attribute-specifier-seqₒₚₜ decl-specifier-seq ref-qualifierₒₚₜ e initializer ';'
5447
- ```
5448
-
5449
- where the declaration is never interpreted as a function declaration and
5450
- the parts of the declaration other than the *declarator-id* are taken
5451
- from the corresponding structured binding declaration. The type of the
5452
- *id-expression* *`e`* is called `E`.
5453
-
5454
- [*Note 1*: `E` is never a reference type [[expr.prop]]. — *end note*]
5455
-
5456
- If the *initializer* refers to one of the names introduced by the
5457
- structured binding declaration, the program is ill-formed.
5458
-
5459
- If `E` is an array type with element type `T`, the number of elements in
5460
- the *identifier-list* shall be equal to the number of elements of `E`.
5461
- Each `vᵢ` is the name of an lvalue that refers to the element i of the
5462
- array and whose type is `T`; the referenced type is `T`.
5463
-
5464
- [*Note 2*: The top-level cv-qualifiers of `T` are cv. — *end note*]
5465
-
5466
- [*Example 1*:
5467
-
5468
- ``` cpp
5469
- auto f() -> int(&)[2];
5470
- auto [ x, y ] = f(); // x and y refer to elements in a copy of the array return value
5471
- auto& [ xr, yr ] = f(); // xr and yr refer to elements in the array referred to by f's return value
5472
- ```
5473
-
5474
- — *end example*]
5475
-
5476
- Otherwise, if the *qualified-id* `std::tuple_size<E>` names a complete
5477
- class type with a member named `value`, the expression
5478
- `std::tuple_size<E>::value` shall be a well-formed integral constant
5479
- expression and the number of elements in the *identifier-list* shall be
5480
- equal to the value of that expression. Let `i` be an index prvalue of
5481
- type `std::size_t` corresponding to `vᵢ`. If a search for the name `get`
5482
- in the scope of `E` [[class.member.lookup]] finds at least one
5483
- declaration that is a function template whose first template parameter
5484
- is a non-type parameter, the initializer is `e.get<i>()`. Otherwise, the
5485
- initializer is `get<i>(e)`, where `get` undergoes argument-dependent
5486
- lookup [[basic.lookup.argdep]]. In either case, `get<i>` is interpreted
5487
- as a *template-id*.
5488
-
5489
- [*Note 3*: Ordinary unqualified lookup [[basic.lookup.unqual]] is not
5490
- performed. — *end note*]
5491
-
5492
- In either case, *`e`* is an lvalue if the type of the entity *`e`* is an
5493
- lvalue reference and an xvalue otherwise. Given the type `Tᵢ` designated
5494
- by `std::tuple_element<i, E>::type` and the type `Uᵢ` designated by
5495
- either `Tᵢ&` or `Tᵢ&&`, where `Uᵢ` is an lvalue reference if the
5496
- initializer is an lvalue and an rvalue reference otherwise, variables
5497
- are introduced with unique names `rᵢ` as follows:
5498
-
5499
- ``` bnf
5500
- *S* 'Uᵢ rᵢ =' initializer ';'
5501
- ```
5502
-
5503
- Each `vᵢ` is the name of an lvalue of type `Tᵢ` that refers to the
5504
- object bound to `rᵢ`; the referenced type is `Tᵢ`.
5505
-
5506
- Otherwise, all of `E`’s non-static data members shall be direct members
5507
- of `E` or of the same base class of `E`, well-formed when named as
5508
- `e.name` in the context of the structured binding, `E` shall not have an
5509
- anonymous union member, and the number of elements in the
5510
- *identifier-list* shall be equal to the number of non-static data
5511
- members of `E`. Designating the non-static data members of `E` as `m₀`,
5512
- `m₁`, `m₂`, … (in declaration order), each `vᵢ` is the name of an lvalue
5513
- that refers to the member `m`ᵢ of *`e`* and whose type is that of `e.mᵢ`
5514
- [[expr.ref]]; the referenced type is the declared type of `mᵢ` if that
5515
- type is a reference type, or the type of `e.mᵢ` otherwise. The lvalue is
5516
- a bit-field if that member is a bit-field.
5517
-
5518
- [*Example 2*:
5519
-
5520
- ``` cpp
5521
- struct S { mutable int x1 : 2; volatile double y1; };
5522
- S f();
5523
- const auto [ x, y ] = f();
5524
- ```
5525
-
5526
- The type of the *id-expression* `x` is “`int`”, the type of the
5527
- *id-expression* `y` is “`const volatile double`”.
5528
-
5529
- — *end example*]
5530
-
5531
- ## Enumerations <a id="enum">[[enum]]</a>
5532
-
5533
- ### Enumeration declarations <a id="dcl.enum">[[dcl.enum]]</a>
5534
-
5535
- An enumeration is a distinct type [[basic.compound]] with named
5536
- constants. Its name becomes an *enum-name* within its scope.
5537
-
5538
- ``` bnf
5539
- enum-name:
5540
- identifier
5541
- ```
5542
-
5543
- ``` bnf
5544
- enum-specifier:
5545
- enum-head '{' enumerator-listₒₚₜ '}'
5546
- enum-head '{' enumerator-list ',' '}'
5547
- ```
5548
-
5549
- ``` bnf
5550
- enum-head:
5551
- enum-key attribute-specifier-seqₒₚₜ enum-head-nameₒₚₜ enum-baseₒₚₜ
5552
- ```
5553
-
5554
- ``` bnf
5555
- enum-head-name:
5556
- nested-name-specifierₒₚₜ identifier
5557
- ```
5558
-
5559
- ``` bnf
5560
- opaque-enum-declaration:
5561
- enum-key attribute-specifier-seqₒₚₜ enum-head-name enum-baseₒₚₜ ';'
5562
- ```
5563
-
5564
- ``` bnf
5565
- enum-key:
5566
- enum
5567
- enum class
5568
- enum struct
5569
- ```
5570
-
5571
- ``` bnf
5572
- enum-base:
5573
- ':' type-specifier-seq
5574
- ```
5575
-
5576
- ``` bnf
5577
- enumerator-list:
5578
- enumerator-definition
5579
- enumerator-list ',' enumerator-definition
5580
- ```
5581
-
5582
- ``` bnf
5583
- enumerator-definition:
5584
- enumerator
5585
- enumerator '=' constant-expression
5586
- ```
5587
-
5588
- ``` bnf
5589
- enumerator:
5590
- identifier attribute-specifier-seqₒₚₜ
5591
- ```
5592
-
5593
- The optional *attribute-specifier-seq* in the *enum-head* and the
5594
- *opaque-enum-declaration* appertains to the enumeration; the attributes
5595
- in that *attribute-specifier-seq* are thereafter considered attributes
5596
- of the enumeration whenever it is named. A `:` following “`enum`
5597
- *nested-name-specifier*ₒₚₜ *identifier*” within the
5598
- *decl-specifier-seq* of a *member-declaration* is parsed as part of an
5599
- *enum-base*.
5600
-
5601
- [*Note 1*:
5602
-
5603
- This resolves a potential ambiguity between the declaration of an
5604
- enumeration with an *enum-base* and the declaration of an unnamed
5605
- bit-field of enumeration type.
5606
-
5607
- [*Example 1*:
5608
-
5609
- ``` cpp
5610
- struct S {
5611
- enum E : int {};
5612
- enum E : int {}; // error: redeclaration of enumeration
5613
- };
5614
- ```
5615
-
5616
- — *end example*]
5617
-
5618
- — *end note*]
5619
-
5620
- The *identifier* in an *enum-head-name* is not looked up and is
5621
- introduced by the *enum-specifier* or *opaque-enum-declaration*. If the
5622
- *enum-head-name* of an *opaque-enum-declaration* contains a
5623
- *nested-name-specifier*, the declaration shall be an explicit
5624
- specialization [[temp.expl.spec]].
5625
-
5626
- The enumeration type declared with an *enum-key* of only `enum` is an
5627
- *unscoped enumeration*, and its *enumerator*s are *unscoped
5628
- enumerators*. The *enum-key*s `enum class` and `enum struct` are
5629
- semantically equivalent; an enumeration type declared with one of these
5630
- is a *scoped enumeration*, and its *enumerator*s are *scoped
5631
- enumerators*. The optional *enum-head-name* shall not be omitted in the
5632
- declaration of a scoped enumeration. The *type-specifier-seq* of an
5633
- *enum-base* shall name an integral type; any cv-qualification is
5634
- ignored. An *opaque-enum-declaration* declaring an unscoped enumeration
5635
- shall not omit the *enum-base*. The identifiers in an *enumerator-list*
5636
- are declared as constants, and can appear wherever constants are
5637
- required. The same identifier shall not appear as the name of multiple
5638
- enumerators in an *enumerator-list*. An *enumerator-definition* with `=`
5639
- gives the associated *enumerator* the value indicated by the
5640
- *constant-expression*. If the first *enumerator* has no *initializer*,
5641
- the value of the corresponding constant is zero. An
5642
- *enumerator-definition* without an *initializer* gives the *enumerator*
5643
- the value obtained by increasing the value of the previous *enumerator*
5644
- by one.
5645
-
5646
- [*Example 2*:
5647
-
5648
- ``` cpp
5649
- enum { a, b, c=0 };
5650
- enum { d, e, f=e+2 };
5651
- ```
5652
-
5653
- defines `a`, `c`, and `d` to be zero, `b` and `e` to be `1`, and `f` to
5654
- be `3`.
5655
-
5656
- — *end example*]
5657
-
5658
- The optional *attribute-specifier-seq* in an *enumerator* appertains to
5659
- that enumerator.
5660
-
5661
- An *opaque-enum-declaration* is either a redeclaration of an enumeration
5662
- in the current scope or a declaration of a new enumeration.
5663
-
5664
- [*Note 2*: An enumeration declared by an *opaque-enum-declaration* has
5665
- a fixed underlying type and is a complete type. The list of enumerators
5666
- can be provided in a later redeclaration with an
5667
- *enum-specifier*. — *end note*]
5668
-
5669
- A scoped enumeration shall not be later redeclared as unscoped or with a
5670
- different underlying type. An unscoped enumeration shall not be later
5671
- redeclared as scoped and each redeclaration shall include an *enum-base*
5672
- specifying the same underlying type as in the original declaration.
5673
-
5674
- If an *enum-head-name* contains a *nested-name-specifier*, the enclosing
5675
- *enum-specifier* or *opaque-enum-declaration* D shall not inhabit a
5676
- class scope and shall correspond to one or more declarations nominable
5677
- in the class, class template, or namespace to which the
5678
- *nested-name-specifier* refers [[basic.scope.scope]]. All those
5679
- declarations shall have the same target scope; the target scope of D is
5680
- that scope.
5681
-
5682
- Each enumeration defines a type that is different from all other types.
5683
- Each enumeration also has an *underlying type*. The underlying type can
5684
- be explicitly specified using an *enum-base*. For a scoped enumeration
5685
- type, the underlying type is `int` if it is not explicitly specified. In
5686
- both of these cases, the underlying type is said to be *fixed*.
5687
- Following the closing brace of an *enum-specifier*, each enumerator has
5688
- the type of its enumeration. If the underlying type is fixed, the type
5689
- of each enumerator prior to the closing brace is the underlying type and
5690
- the *constant-expression* in the *enumerator-definition* shall be a
5691
- converted constant expression of the underlying type [[expr.const]]. If
5692
- the underlying type is not fixed, the type of each enumerator prior to
5693
- the closing brace is determined as follows:
5694
-
5695
- - If an initializer is specified for an enumerator, the
5696
- *constant-expression* shall be an integral constant expression
5697
- [[expr.const]]. If the expression has unscoped enumeration type, the
5698
- enumerator has the underlying type of that enumeration type, otherwise
5699
- it has the same type as the expression.
5700
- - If no initializer is specified for the first enumerator, its type is
5701
- an unspecified signed integral type.
5702
- - Otherwise the type of the enumerator is the same as that of the
5703
- preceding enumerator unless the incremented value is not representable
5704
- in that type, in which case the type is an unspecified integral type
5705
- sufficient to contain the incremented value. If no such type exists,
5706
- the program is ill-formed.
5707
-
5708
- An enumeration whose underlying type is fixed is an incomplete type
5709
- until immediately after its *enum-base* (if any), at which point it
5710
- becomes a complete type. An enumeration whose underlying type is not
5711
- fixed is an incomplete type until the closing `}` of its
5712
- *enum-specifier*, at which point it becomes a complete type.
5713
-
5714
- For an enumeration whose underlying type is not fixed, the underlying
5715
- type is an integral type that can represent all the enumerator values
5716
- defined in the enumeration. If no integral type can represent all the
5717
- enumerator values, the enumeration is ill-formed. It is
5718
- *implementation-defined* which integral type is used as the underlying
5719
- type except that the underlying type shall not be larger than `int`
5720
- unless the value of an enumerator cannot fit in an `int` or
5721
- `unsigned int`. If the *enumerator-list* is empty, the underlying type
5722
- is as if the enumeration had a single enumerator with value 0.
5723
-
5724
- For an enumeration whose underlying type is fixed, the values of the
5725
- enumeration are the values of the underlying type. Otherwise, the values
5726
- of the enumeration are the values representable by a hypothetical
5727
- integer type with minimal width M such that all enumerators can be
5728
- represented. The width of the smallest bit-field large enough to hold
5729
- all the values of the enumeration type is M. It is possible to define an
5730
- enumeration that has values not defined by any of its enumerators. If
5731
- the *enumerator-list* is empty, the values of the enumeration are as if
5732
- the enumeration had a single enumerator with value 0.[^9]
5733
-
5734
- An enumeration has the same size, value representation, and alignment
5735
- requirements [[basic.align]] as its underlying type. Furthermore, each
5736
- value of an enumeration has the same representation as the corresponding
5737
- value of the underlying type.
5738
-
5739
- Two enumeration types are *layout-compatible enumerations* if they have
5740
- the same underlying type.
5741
-
5742
- The value of an enumerator or an object of an unscoped enumeration type
5743
- is converted to an integer by integral promotion [[conv.prom]].
5744
-
5745
- [*Example 3*:
5746
-
5747
- ``` cpp
5748
- enum color { red, yellow, green=20, blue };
5749
- color col = red;
5750
- color* cp = &col;
5751
- if (*cp == blue) // ...
5752
- ```
5753
-
5754
- makes `color` a type describing various colors, and then declares `col`
5755
- as an object of that type, and `cp` as a pointer to an object of that
5756
- type. The possible values of an object of type `color` are `red`,
5757
- `yellow`, `green`, `blue`; these values can be converted to the integral
5758
- values `0`, `1`, `20`, and `21`. Since enumerations are distinct types,
5759
- objects of type `color` can be assigned only values of type `color`.
5760
-
5761
- ``` cpp
5762
- color c = 1; // error: type mismatch, no conversion from int to color
5763
- int i = yellow; // OK, yellow converted to integral value 1, integral promotion
5764
- ```
5765
-
5766
- Note that this implicit `enum` to `int` conversion is not provided for a
5767
- scoped enumeration:
5768
-
5769
- ``` cpp
5770
- enum class Col { red, yellow, green };
5771
- int x = Col::red; // error: no Col to int conversion
5772
- Col y = Col::red;
5773
- if (y) { } // error: no Col to bool conversion
5774
- ```
5775
-
5776
- — *end example*]
5777
-
5778
- The name of each unscoped enumerator is also bound in the scope that
5779
- immediately contains the *enum-specifier*. An unnamed enumeration that
5780
- does not have a typedef name for linkage purposes [[dcl.typedef]] and
5781
- that has a first enumerator is denoted, for linkage purposes
5782
- [[basic.link]], by its underlying type and its first enumerator; such an
5783
- enumeration is said to have an enumerator as a name for linkage
5784
- purposes.
5785
-
5786
- [*Note 3*: Each unnamed enumeration with no enumerators is a distinct
5787
- type. — *end note*]
5788
-
5789
- [*Example 4*:
5790
-
5791
- ``` cpp
5792
- enum direction { left='l', right='r' };
5793
-
5794
- void g() {
5795
- direction d; // OK
5796
- d = left; // OK
5797
- d = direction::right; // OK
5798
- }
5799
-
5800
- enum class altitude { high='h', low='l' };
5801
-
5802
- void h() {
5803
- altitude a; // OK
5804
- a = high; // error: high not in scope
5805
- a = altitude::low; // OK
5806
- }
5807
- ```
5808
-
5809
- — *end example*]
5810
-
5811
- ### The `using enum` declaration <a id="enum.udecl">[[enum.udecl]]</a>
5812
-
5813
- ``` bnf
5814
- using-enum-declaration:
5815
- using enum using-enum-declarator ';'
5816
- ```
5817
-
5818
- ``` bnf
5819
- using-enum-declarator:
5820
- nested-name-specifierₒₚₜ identifier
5821
- nested-name-specifierₒₚₜ simple-template-id
5822
- ```
5823
-
5824
- A *using-enum-declarator* names the set of declarations found by lookup
5825
- [[basic.lookup.unqual]], [[basic.lookup.qual]] for the
5826
- *using-enum-declarator*. The *using-enum-declarator* shall designate a
5827
- non-dependent type with a reachable *enum-specifier*.
5828
-
5829
- A *using-enum-declaration* is equivalent to a *using-declaration* for
5830
- each enumerator.
5831
-
5832
- [*Note 1*:
5833
-
5834
- A *using-enum-declaration* in class scope makes the enumerators of the
5835
- named enumeration available via member lookup.
5836
-
5837
- [*Example 1*:
5838
-
5839
- ``` cpp
5840
- enum class fruit { orange, apple };
5841
- struct S {
5842
- using enum fruit; // OK, introduces orange and apple into S
5843
- };
5844
- void f() {
5845
- S s;
5846
- s.orange; // OK, names fruit::orange
5847
- S::orange; // OK, names fruit::orange
5848
- }
5849
- ```
5850
-
5851
- — *end example*]
5852
-
5853
- — *end note*]
5854
-
5855
- [*Note 2*:
5856
-
5857
- Two *using-enum-declaration*s that introduce two enumerators of the same
5858
- name conflict.
5859
-
5860
- [*Example 2*:
5861
-
5862
- ``` cpp
5863
- enum class fruit { orange, apple };
5864
- enum class color { red, orange };
5865
- void f() {
5866
- using enum fruit; // OK
5867
- using enum color; // error: color::orange and fruit::orange conflict
5868
- }
5869
- ```
5870
-
5871
- — *end example*]
5872
-
5873
- — *end note*]
5874
-
5875
- ## Namespaces <a id="basic.namespace">[[basic.namespace]]</a>
5876
-
5877
- ### General <a id="basic.namespace.general">[[basic.namespace.general]]</a>
5878
-
5879
- A namespace is an optionally-named entity whose scope can contain
5880
- declarations of any kind of entity. The name of a namespace can be used
5881
- to access entities that belong to that namespace; that is, the *members*
5882
- of the namespace. Unlike other entities, the definition of a namespace
5883
- can be split over several parts of one or more translation units and
5884
- modules.
5885
-
5886
- [*Note 1*: A *namespace-definition* is exported if it contains any
5887
- *export-declaration*s [[module.interface]]. A namespace is never
5888
- attached to a named module and never has a name with module
5889
- linkage. — *end note*]
5890
-
5891
- [*Example 1*:
5892
-
5893
- ``` cpp
5894
- export module M;
5895
- namespace N1 {} // N1 is not exported
5896
- export namespace N2 {} // N2 is exported
5897
- namespace N3 { export int n; } // N3 is exported
5898
- ```
5899
-
5900
- — *end example*]
5901
-
5902
- There is a *global namespace* with no declaration; see 
5903
- [[basic.scope.namespace]]. The global namespace belongs to the global
5904
- scope; it is not an unnamed namespace [[namespace.unnamed]].
5905
-
5906
- [*Note 2*: Lacking a declaration, it cannot be found by name
5907
- lookup. — *end note*]
5908
-
5909
- ### Namespace definition <a id="namespace.def">[[namespace.def]]</a>
5910
-
5911
- #### General <a id="namespace.def.general">[[namespace.def.general]]</a>
5912
-
5913
- ``` bnf
5914
- namespace-name:
5915
- identifier
5916
- namespace-alias
5917
- ```
5918
-
5919
- ``` bnf
5920
- namespace-definition:
5921
- named-namespace-definition
5922
- unnamed-namespace-definition
5923
- nested-namespace-definition
5924
- ```
5925
-
5926
- ``` bnf
5927
- named-namespace-definition:
5928
- inlineₒₚₜ namespace attribute-specifier-seqₒₚₜ identifier '{' namespace-body '}'
5929
- ```
5930
-
5931
- ``` bnf
5932
- unnamed-namespace-definition:
5933
- inlineₒₚₜ namespace attribute-specifier-seqₒₚₜ '{' namespace-body '}'
5934
- ```
5935
-
5936
- ``` bnf
5937
- nested-namespace-definition:
5938
- namespace enclosing-namespace-specifier '::' inlineₒₚₜ identifier '{' namespace-body '}'
5939
- ```
5940
-
5941
- ``` bnf
5942
- enclosing-namespace-specifier:
5943
- identifier
5944
- enclosing-namespace-specifier '::' inlineₒₚₜ identifier
5945
- ```
5946
-
5947
- ``` bnf
5948
- namespace-body:
5949
- declaration-seqₒₚₜ
5950
- ```
5951
-
5952
- Every *namespace-definition* shall inhabit a namespace scope
5953
- [[basic.scope.namespace]].
5954
-
5955
- In a *named-namespace-definition* D, the *identifier* is the name of the
5956
- namespace. The *identifier* is looked up by searching for it in the
5957
- scopes of the namespace A in which D appears and of every element of the
5958
- inline namespace set of A. If the lookup finds a *namespace-definition*
5959
- for a namespace N, D *extends* N, and the target scope of D is the scope
5960
- to which N belongs. If the lookup finds nothing, the *identifier* is
5961
- introduced as a *namespace-name* into A.
5962
-
5963
- Because a *namespace-definition* contains *declaration*s in its
5964
- *namespace-body* and a *namespace-definition* is itself a *declaration*,
5965
- it follows that *namespace-definition*s can be nested.
5966
-
5967
- [*Example 1*:
5968
-
5969
- ``` cpp
5970
- namespace Outer {
5971
- int i;
5972
- namespace Inner {
5973
- void f() { i++; } // Outer::i
5974
- int i;
5975
- void g() { i++; } // Inner::i
5976
- }
5977
- }
5978
- ```
5979
-
5980
- — *end example*]
5981
-
5982
- If the optional initial `inline` keyword appears in a
5983
- *namespace-definition* for a particular namespace, that namespace is
5984
- declared to be an *inline namespace*. The `inline` keyword may be used
5985
- on a *namespace-definition* that extends a namespace only if it was
5986
- previously used on the *namespace-definition* that initially declared
5987
- the *namespace-name* for that namespace.
5988
-
5989
- The optional *attribute-specifier-seq* in a *named-namespace-definition*
5990
- appertains to the namespace being defined or extended.
5991
-
5992
- Members of an inline namespace can be used in most respects as though
5993
- they were members of the innermost enclosing namespace. Specifically,
5994
- the inline namespace and its enclosing namespace are both added to the
5995
- set of associated namespaces used in argument-dependent lookup
5996
- [[basic.lookup.argdep]] whenever one of them is, and a *using-directive*
5997
- [[namespace.udir]] that names the inline namespace is implicitly
5998
- inserted into the enclosing namespace as for an unnamed namespace
5999
- [[namespace.unnamed]]. Furthermore, each member of the inline namespace
6000
- can subsequently be partially specialized [[temp.spec.partial]],
6001
- explicitly instantiated [[temp.explicit]], or explicitly specialized
6002
- [[temp.expl.spec]] as though it were a member of the enclosing
6003
- namespace. Finally, looking up a name in the enclosing namespace via
6004
- explicit qualification [[namespace.qual]] will include members of the
6005
- inline namespace even if there are declarations of that name in the
6006
- enclosing namespace.
6007
-
6008
- These properties are transitive: if a namespace `N` contains an inline
6009
- namespace `M`, which in turn contains an inline namespace `O`, then the
6010
- members of `O` can be used as though they were members of `M` or `N`.
6011
- The *inline namespace set* of `N` is the transitive closure of all
6012
- inline namespaces in `N`.
6013
-
6014
- A *nested-namespace-definition* with an *enclosing-namespace-specifier*
6015
- `E`, *identifier* `I` and *namespace-body* `B` is equivalent to
6016
-
6017
- ``` cpp
6018
- namespace E { \opt{inline} namespace I { B } }
6019
- ```
6020
-
6021
- where the optional `inline` is present if and only if the *identifier*
6022
- `I` is preceded by `inline`.
6023
-
6024
- [*Example 2*:
6025
-
6026
- ``` cpp
6027
- namespace A::inline B::C {
6028
- int i;
6029
- }
6030
- ```
6031
-
6032
- The above has the same effect as:
6033
-
6034
- ``` cpp
6035
- namespace A {
6036
- inline namespace B {
6037
- namespace C {
6038
- int i;
6039
- }
6040
- }
6041
- }
6042
- ```
6043
-
6044
- — *end example*]
6045
-
6046
- #### Unnamed namespaces <a id="namespace.unnamed">[[namespace.unnamed]]</a>
6047
-
6048
- An *unnamed-namespace-definition* behaves as if it were replaced by
6049
-
6050
- ``` bnf
6051
- inlineₒₚₜ namespace unique '{' '/* empty body */' '}'
6052
- using namespace unique ';'
6053
- namespace unique '{' namespace-body '}'
6054
- ```
6055
-
6056
- where `inline` appears if and only if it appears in the
6057
- *unnamed-namespace-definition* and all occurrences of *`unique`* in a
6058
- translation unit are replaced by the same identifier, and this
6059
- identifier differs from all other identifiers in the translation unit.
6060
- The optional *attribute-specifier-seq* in the
6061
- *unnamed-namespace-definition* appertains to *`unique`*.
6062
-
6063
- [*Example 1*:
6064
-
6065
- ``` cpp
6066
- namespace { int i; } // unique::i
6067
- void f() { i++; } // unique::i++
6068
-
6069
- namespace A {
6070
- namespace {
6071
- int i; // A::unique::i
6072
- int j; // A::unique::j
6073
- }
6074
- void g() { i++; } // A::unique::i++
6075
- }
6076
-
6077
- using namespace A;
6078
- void h() {
6079
- i++; // error: unique::i or A::unique::i
6080
- A::i++; // A::unique::i
6081
- j++; // A::unique::j
6082
- }
6083
- ```
6084
-
6085
- — *end example*]
6086
-
6087
- ### Namespace alias <a id="namespace.alias">[[namespace.alias]]</a>
6088
-
6089
- A *namespace-alias-definition* declares an alternate name for a
6090
- namespace according to the following grammar:
6091
-
6092
- ``` bnf
6093
- namespace-alias:
6094
- identifier
6095
- ```
6096
-
6097
- ``` bnf
6098
- namespace-alias-definition:
6099
- namespace identifier '=' qualified-namespace-specifier ';'
6100
- ```
6101
-
6102
- ``` bnf
6103
- qualified-namespace-specifier:
6104
- nested-name-specifierₒₚₜ namespace-name
6105
- ```
6106
-
6107
- The *identifier* in a *namespace-alias-definition* becomes a
6108
- *namespace-alias* and denotes the namespace denoted by the
6109
- *qualified-namespace-specifier*.
6110
-
6111
- [*Note 1*: When looking up a *namespace-name* in a
6112
- *namespace-alias-definition*, only namespace names are considered, see 
6113
- [[basic.lookup.udir]]. — *end note*]
6114
-
6115
- ### Using namespace directive <a id="namespace.udir">[[namespace.udir]]</a>
6116
-
6117
- ``` bnf
6118
- using-directive:
6119
- attribute-specifier-seqₒₚₜ using namespace nested-name-specifierₒₚₜ namespace-name ';'
6120
- ```
6121
-
6122
- A *using-directive* shall not appear in class scope, but may appear in
6123
- namespace scope or in block scope.
6124
-
6125
- [*Note 1*: When looking up a *namespace-name* in a *using-directive*,
6126
- only namespace names are considered, see 
6127
- [[basic.lookup.udir]]. — *end note*]
6128
-
6129
- The optional *attribute-specifier-seq* appertains to the
6130
- *using-directive*.
6131
-
6132
- [*Note 2*: A *using-directive* makes the names in the nominated
6133
- namespace usable in the scope in which the *using-directive* appears
6134
- after the *using-directive* [[basic.lookup.unqual]], [[namespace.qual]].
6135
- During unqualified name lookup, the names appear as if they were
6136
- declared in the nearest enclosing namespace which contains both the
6137
- *using-directive* and the nominated namespace. — *end note*]
6138
-
6139
- [*Note 3*: A *using-directive* does not introduce any
6140
- names. — *end note*]
6141
-
6142
- [*Example 1*:
6143
-
6144
- ``` cpp
6145
- namespace A {
6146
- int i;
6147
- namespace B {
6148
- namespace C {
6149
- int i;
6150
- }
6151
- using namespace A::B::C;
6152
- void f1() {
6153
- i = 5; // OK, C::i visible in B and hides A::i
6154
- }
6155
- }
6156
- namespace D {
6157
- using namespace B;
6158
- using namespace C;
6159
- void f2() {
6160
- i = 5; // ambiguous, B::C::i or A::i?
6161
- }
6162
- }
6163
- void f3() {
6164
- i = 5; // uses A::i
6165
- }
6166
- }
6167
- void f4() {
6168
- i = 5; // error: neither i is visible
6169
- }
6170
- ```
6171
-
6172
- — *end example*]
6173
-
6174
- [*Note 4*: A *using-directive* is transitive: if a scope contains a
6175
- *using-directive* that nominates a namespace that itself contains
6176
- *using-directive*s, the namespaces nominated by those *using-directive*s
6177
- are also eligible to be considered. — *end note*]
6178
-
6179
- [*Example 2*:
6180
-
6181
- ``` cpp
6182
- namespace M {
6183
- int i;
6184
- }
6185
-
6186
- namespace N {
6187
- int i;
6188
- using namespace M;
6189
- }
6190
-
6191
- void f() {
6192
- using namespace N;
6193
- i = 7; // error: both M::i and N::i are visible
6194
- }
6195
- ```
6196
-
6197
- For another example,
6198
-
6199
- ``` cpp
6200
- namespace A {
6201
- int i;
6202
- }
6203
- namespace B {
6204
- int i;
6205
- int j;
6206
- namespace C {
6207
- namespace D {
6208
- using namespace A;
6209
- int j;
6210
- int k;
6211
- int a = i; // B::i hides A::i
6212
- }
6213
- using namespace D;
6214
- int k = 89; // no problem yet
6215
- int l = k; // ambiguous: C::k or D::k
6216
- int m = i; // B::i hides A::i
6217
- int n = j; // D::j hides B::j
6218
- }
6219
- }
6220
- ```
6221
-
6222
- — *end example*]
6223
-
6224
- [*Note 5*: Declarations in a namespace that appear after a
6225
- *using-directive* for that namespace can be found through that
6226
- *using-directive* after they appear. — *end note*]
6227
-
6228
- [*Note 6*:
6229
-
6230
- If name lookup finds a declaration for a name in two different
6231
- namespaces, and the declarations do not declare the same entity and do
6232
- not declare functions or function templates, the use of the name is
6233
- ill-formed [[basic.lookup]]. In particular, the name of a variable,
6234
- function or enumerator does not hide the name of a class or enumeration
6235
- declared in a different namespace. For example,
6236
-
6237
- ``` cpp
6238
- namespace A {
6239
- class X { };
6240
- extern "C" int g();
6241
- extern "C++" int h();
6242
- }
6243
- namespace B {
6244
- void X(int);
6245
- extern "C" int g();
6246
- extern "C++" int h(int);
6247
- }
6248
- using namespace A;
6249
- using namespace B;
6250
-
6251
- void f() {
6252
- X(1); // error: name X found in two namespaces
6253
- g(); // OK, name g refers to the same entity
6254
- h(); // OK, overload resolution selects A::h
6255
- }
6256
- ```
6257
-
6258
- — *end note*]
6259
-
6260
- [*Note 7*:
6261
-
6262
- The order in which namespaces are considered and the relationships among
6263
- the namespaces implied by the *using-directive*s do not affect overload
6264
- resolution. Neither is any function excluded because another has the
6265
- same signature, even if one is in a namespace reachable through
6266
- *using-directive*s in the namespace of the other.[^10]
6267
-
6268
- — *end note*]
6269
-
6270
- [*Example 3*:
6271
-
6272
- ``` cpp
6273
- namespace D {
6274
- int d1;
6275
- void f(char);
6276
- }
6277
- using namespace D;
6278
-
6279
- int d1; // OK, no conflict with D::d1
6280
-
6281
- namespace E {
6282
- int e;
6283
- void f(int);
6284
- }
6285
-
6286
- namespace D { // namespace extension
6287
- int d2;
6288
- using namespace E;
6289
- void f(int);
6290
- }
6291
-
6292
- void f() {
6293
- d1++; // error: ambiguous ::d1 or D::d1?
6294
- ::d1++; // OK
6295
- D::d1++; // OK
6296
- d2++; // OK, D::d2
6297
- e++; // OK, E::e
6298
- f(1); // error: ambiguous: D::f(int) or E::f(int)?
6299
- f('a'); // OK, D::f(char)
6300
- }
6301
- ```
6302
-
6303
- — *end example*]
6304
-
6305
- ## The `using` declaration <a id="namespace.udecl">[[namespace.udecl]]</a>
6306
-
6307
- ``` bnf
6308
- using-declaration:
6309
- using using-declarator-list ';'
6310
- ```
6311
-
6312
- ``` bnf
6313
- using-declarator-list:
6314
- using-declarator '...'ₒₚₜ
6315
- using-declarator-list ',' using-declarator '...'ₒₚₜ
6316
- ```
6317
-
6318
- ``` bnf
6319
- using-declarator:
6320
- typenameₒₚₜ nested-name-specifier unqualified-id
6321
- ```
6322
-
6323
- The component names of a *using-declarator* are those of its
6324
- *nested-name-specifier* and *unqualified-id*. Each *using-declarator* in
6325
- a *using-declaration*[^11]
6326
-
6327
- names the set of declarations found by lookup [[basic.lookup.qual]] for
6328
- the *using-declarator*, except that class and enumeration declarations
6329
- that would be discarded are merely ignored when checking for ambiguity
6330
- [[basic.lookup]], conversion function templates with a dependent return
6331
- type are ignored, and certain functions are hidden as described below.
6332
- If the terminal name of the *using-declarator* is dependent
6333
- [[temp.dep.type]], the *using-declarator* is considered to name a
6334
- constructor if and only if the *nested-name-specifier* has a terminal
6335
- name that is the same as the *unqualified-id*. If the lookup in any
6336
- instantiation finds that a *using-declarator* that is not considered to
6337
- name a constructor does do so, or that a *using-declarator* that is
6338
- considered to name a constructor does not, the program is ill-formed.
6339
-
6340
- If the *using-declarator* names a constructor, it declares that the
6341
- class *inherits* the named set of constructor declarations from the
6342
- nominated base class.
6343
-
6344
- [*Note 1*: Otherwise, the *unqualified-id* in the *using-declarator* is
6345
- bound to the *using-declarator*, which is replaced during name lookup
6346
- with the declarations it names [[basic.lookup]]. If such a declaration
6347
- is of an enumeration, the names of its enumerators are not bound. For
6348
- the keyword `typename`, see [[temp.res]]. — *end note*]
6349
-
6350
- In a *using-declaration* used as a *member-declaration*, each
6351
- *using-declarator* shall either name an enumerator or have a
6352
- *nested-name-specifier* naming a base class of the current class
6353
- [[expr.prim.this]].
6354
-
6355
- [*Example 1*:
6356
-
6357
- ``` cpp
6358
- enum class button { up, down };
6359
- struct S {
6360
- using button::up;
6361
- button b = up; // OK
6362
- };
6363
- ```
6364
-
6365
- — *end example*]
6366
-
6367
- If a *using-declarator* names a constructor, its *nested-name-specifier*
6368
- shall name a direct base class of the current class. If the immediate
6369
- (class) scope is associated with a class template, it shall derive from
6370
- the specified base class or have at least one dependent base class.
6371
-
6372
- [*Example 2*:
6373
-
6374
- ``` cpp
6375
- struct B {
6376
- void f(char);
6377
- enum E { e };
6378
- union { int x; };
6379
- };
6380
-
6381
- struct C {
6382
- int f();
6383
- };
6384
-
6385
- struct D : B {
6386
- using B::f; // OK, B is a base of D
6387
- using B::e; // OK, e is an enumerator of base B
6388
- using B::x; // OK, x is a union member of base B
6389
- using C::f; // error: C isn't a base of D
6390
- void f(int) { f('c'); } // calls B::f(char)
6391
- void g(int) { g('c'); } // recursively calls D::g(int)
6392
- };
6393
- template <typename... bases>
6394
- struct X : bases... {
6395
- using bases::f...;
6396
- };
6397
- X<B, C> x; // OK, B::f and C::f named
6398
- ```
6399
-
6400
- — *end example*]
6401
-
6402
- [*Note 2*: Since destructors do not have names, a *using-declaration*
6403
- cannot refer to a destructor for a base class. — *end note*]
6404
-
6405
- If a constructor or assignment operator brought from a base class into a
6406
- derived class has the signature of a copy/move constructor or assignment
6407
- operator for the derived class
6408
- [[class.copy.ctor]], [[class.copy.assign]], the *using-declaration* does
6409
- not by itself suppress the implicit declaration of the derived class
6410
- member; the member from the base class is hidden or overridden by the
6411
- implicitly-declared copy/move constructor or assignment operator of the
6412
- derived class, as described below.
6413
-
6414
- A *using-declaration* shall not name a *template-id*.
6415
-
6416
- [*Example 3*:
6417
-
6418
- ``` cpp
6419
- struct A {
6420
- template <class T> void f(T);
6421
- template <class T> struct X { };
6422
- };
6423
- struct B : A {
6424
- using A::f<double>; // error
6425
- using A::X<int>; // error
6426
- };
6427
- ```
6428
-
6429
- — *end example*]
6430
-
6431
- A *using-declaration* shall not name a namespace.
6432
-
6433
- A *using-declaration* that names a class member other than an enumerator
6434
- shall be a *member-declaration*.
6435
-
6436
- [*Example 4*:
6437
-
6438
- ``` cpp
6439
- struct X {
6440
- int i;
6441
- static int s;
6442
- };
6443
-
6444
- void f() {
6445
- using X::i; // error: X::i is a class member and this is not a member declaration.
6446
- using X::s; // error: X::s is a class member and this is not a member declaration.
6447
- }
6448
- ```
6449
-
6450
- — *end example*]
6451
-
6452
- If a declaration is named by two *using-declarator*s that inhabit the
6453
- same class scope, the program is ill-formed.
6454
-
6455
- [*Note 3*: A *using-declarator* whose *nested-name-specifier* names a
6456
- namespace does not name declarations added to the namespace after it.
6457
- Thus, additional overloads added after the *using-declaration* are
6458
- ignored, but default function arguments [[dcl.fct.default]], default
6459
- template arguments [[temp.param]], and template specializations
6460
- [[temp.spec.partial]], [[temp.expl.spec]] are considered. — *end note*]
6461
-
6462
- [*Example 5*:
6463
-
6464
- ``` cpp
6465
- namespace A {
6466
- void f(int);
6467
- }
6468
-
6469
- using A::f; // f is a synonym for A::f; that is, for A::f(int).
6470
- namespace A {
6471
- void f(char);
6472
- }
6473
-
6474
- void foo() {
6475
- f('a'); // calls f(int), even though f(char) exists.
6476
- }
6477
-
6478
- void bar() {
6479
- using A::f; // f is a synonym for A::f; that is, for A::f(int) and A::f(char).
6480
- f('a'); // calls f(char)
6481
- }
6482
- ```
6483
-
6484
- — *end example*]
6485
-
6486
- If a declaration named by a *using-declaration* that inhabits the target
6487
- scope of another declaration potentially conflicts with it
6488
- [[basic.scope.scope]], and either is reachable from the other, the
6489
- program is ill-formed. If two declarations named by *using-declaration*s
6490
- that inhabit the same scope potentially conflict, either is reachable
6491
- from the other, and they do not both declare functions or function
6492
- templates, the program is ill-formed.
6493
-
6494
- [*Note 4*: Overload resolution possibly cannot distinguish between
6495
- conflicting function declarations. — *end note*]
6496
-
6497
- [*Example 6*:
6498
-
6499
- ``` cpp
6500
- namespace A {
6501
- int x;
6502
- int f(int);
6503
- int g;
6504
- void h();
6505
- }
6506
-
6507
- namespace B {
6508
- int i;
6509
- struct g { };
6510
- struct x { };
6511
- void f(int);
6512
- void f(double);
6513
- void g(char); // OK, hides struct g
6514
- }
6515
-
6516
- void func() {
6517
- int i;
6518
- using B::i; // error: conflicts
6519
- void f(char);
6520
- using B::f; // OK, each f is a function
6521
- using A::f; // OK, but interferes with B::f(int)
6522
- f(1); // error: ambiguous
6523
- static_cast<int(*)(int)>(f)(1); // OK, calls A::f
6524
- f(3.5); // calls B::f(double)
6525
- using B::g;
6526
- g('a'); // calls B::g(char)
6527
- struct g g1; // g1 has class type B::g
6528
- using A::g; // error: conflicts with B::g
6529
- void h();
6530
- using A::h; // error: conflicts
6531
- using B::x;
6532
- using A::x; // OK, hides struct B::x
6533
- x = 99; // assigns to A::x
6534
- struct x x1; // x1 has class type B::x
6535
- }
6536
- ```
6537
-
6538
- — *end example*]
6539
-
6540
- The set of declarations named by a *using-declarator* that inhabits a
6541
- class `C` does not include member functions and member function
6542
- templates of a base class that correspond to (and thus would conflict
6543
- with) a declaration of a function or function template in `C`.
6544
-
6545
- [*Example 7*:
6546
-
6547
- ``` cpp
6548
- struct B {
6549
- virtual void f(int);
6550
- virtual void f(char);
6551
- void g(int);
6552
- void h(int);
6553
- };
6554
-
6555
- struct D : B {
6556
- using B::f;
6557
- void f(int); // OK, D::f(int) overrides B::f(int);
6558
-
6559
- using B::g;
6560
- void g(char); // OK
6561
-
6562
- using B::h;
6563
- void h(int); // OK, D::h(int) hides B::h(int)
6564
- };
6565
-
6566
- void k(D* p)
6567
- {
6568
- p->f(1); // calls D::f(int)
6569
- p->f('a'); // calls B::f(char)
6570
- p->g(1); // calls B::g(int)
6571
- p->g('a'); // calls D::g(char)
6572
- }
6573
-
6574
- struct B1 {
6575
- B1(int);
6576
- };
6577
-
6578
- struct B2 {
6579
- B2(int);
6580
- };
6581
-
6582
- struct D1 : B1, B2 {
6583
- using B1::B1;
6584
- using B2::B2;
6585
- };
6586
- D1 d1(0); // error: ambiguous
6587
-
6588
- struct D2 : B1, B2 {
6589
- using B1::B1;
6590
- using B2::B2;
6591
- D2(int); // OK, D2::D2(int) hides B1::B1(int) and B2::B2(int)
6592
- };
6593
- D2 d2(0); // calls D2::D2(int)
6594
- ```
6595
-
6596
- — *end example*]
6597
-
6598
- [*Note 5*: For the purpose of forming a set of candidates during
6599
- overload resolution, the functions named by a *using-declaration* in a
6600
- derived class are treated as though they were direct members of the
6601
- derived class. In particular, the implicit object parameter is treated
6602
- as if it were a reference to the derived class rather than to the base
6603
- class [[over.match.funcs]]. This has no effect on the type of the
6604
- function, and in all other respects the function remains part of the
6605
- base class. — *end note*]
6606
-
6607
- Constructors that are named by a *using-declaration* are treated as
6608
- though they were constructors of the derived class when looking up the
6609
- constructors of the derived class [[class.qual]] or forming a set of
6610
- overload candidates
6611
- [[over.match.ctor]], [[over.match.copy]], [[over.match.list]].
6612
-
6613
- [*Note 6*: If such a constructor is selected to perform the
6614
- initialization of an object of class type, all subobjects other than the
6615
- base class from which the constructor originated are implicitly
6616
- initialized [[class.inhctor.init]]. A constructor of a derived class is
6617
- sometimes preferred to a constructor of a base class if they would
6618
- otherwise be ambiguous [[over.match.best]]. — *end note*]
6619
-
6620
- In a *using-declarator* that does not name a constructor, every
6621
- declaration named shall be accessible. In a *using-declarator* that
6622
- names a constructor, no access check is performed.
6623
-
6624
- [*Note 7*:
6625
-
6626
- Because a *using-declarator* designates a base class member (and not a
6627
- member subobject or a member function of a base class subobject), a
6628
- *using-declarator* cannot be used to resolve inherited member
6629
- ambiguities.
6630
-
6631
- [*Example 8*:
6632
-
6633
- ``` cpp
6634
- struct A { int x(); };
6635
- struct B : A { };
6636
- struct C : A {
6637
- using A::x;
6638
- int x(int);
6639
- };
6640
-
6641
- struct D : B, C {
6642
- using C::x;
6643
- int x(double);
6644
- };
6645
- int f(D* d) {
6646
- return d->x(); // error: overload resolution selects A::x, but A is an ambiguous base class
6647
- }
6648
- ```
6649
-
6650
- — *end example*]
6651
-
6652
- — *end note*]
6653
-
6654
- A *using-declaration* has the usual accessibility for a
6655
- *member-declaration*. Base-class constructors considered because of a
6656
- *using-declarator* are accessible if they would be accessible when used
6657
- to construct an object of the base class; the accessibility of the
6658
- *using-declaration* is ignored.
6659
-
6660
- [*Example 9*:
6661
-
6662
- ``` cpp
6663
- class A {
6664
- private:
6665
- void f(char);
6666
- public:
6667
- void f(int);
6668
- protected:
6669
- void g();
6670
- };
6671
-
6672
- class B : public A {
6673
- using A::f; // error: A::f(char) is inaccessible
6674
- public:
6675
- using A::g; // B::g is a public synonym for A::g
6676
- };
6677
- ```
6678
-
6679
- — *end example*]
6680
-
6681
- ## The `asm` declaration <a id="dcl.asm">[[dcl.asm]]</a>
6682
-
6683
- An `asm` declaration has the form
6684
-
6685
- ``` bnf
6686
- asm-declaration:
6687
- attribute-specifier-seqₒₚₜ asm '(' string-literal ')' ';'
6688
- ```
6689
-
6690
- The `asm` declaration is conditionally-supported; its meaning is
6691
- *implementation-defined*. The optional *attribute-specifier-seq* in an
6692
- *asm-declaration* appertains to the `asm` declaration.
6693
-
6694
- [*Note 1*: Typically it is used to pass information through the
6695
- implementation to an assembler. — *end note*]
6696
-
6697
- ## Linkage specifications <a id="dcl.link">[[dcl.link]]</a>
6698
-
6699
- All functions and variables whose names have external linkage and all
6700
- function types have a *language linkage*.
6701
-
6702
- [*Note 1*: Some of the properties associated with an entity with
6703
- language linkage are specific to each implementation and are not
6704
- described here. For example, a particular language linkage might be
6705
- associated with a particular form of representing names of objects and
6706
- functions with external linkage, or with a particular calling
6707
- convention, etc. — *end note*]
6708
-
6709
- The default language linkage of all function types, functions, and
6710
- variables is C++ language linkage. Two function types with different
6711
- language linkages are distinct types even if they are otherwise
6712
- identical.
6713
-
6714
- Linkage [[basic.link]] between C++ and non-C++ code fragments can be
6715
- achieved using a *linkage-specification*:
6716
-
6717
- ``` bnf
6718
- linkage-specification:
6719
- extern string-literal '{' declaration-seqₒₚₜ '}'
6720
- extern string-literal name-declaration
6721
- ```
6722
-
6723
- The *string-literal* indicates the required language linkage. This
6724
- document specifies the semantics for the *string-literal*s `"C"` and
6725
- `"C++"`. Use of a *string-literal* other than `"C"` or `"C++"` is
6726
- conditionally-supported, with *implementation-defined* semantics.
6727
-
6728
- [*Note 2*: Therefore, a linkage-specification with a *string-literal*
6729
- that is unknown to the implementation requires a
6730
- diagnostic. — *end note*]
6731
-
6732
- *Recommended practice:* The spelling of the *string-literal* should be
6733
- taken from the document defining that language. For example, `Ada` (not
6734
- `ADA`) and `Fortran` or `FORTRAN`, depending on the vintage.
6735
-
6736
- Every implementation shall provide for linkage to the C programming
6737
- language, `"C"`, and C++, `"C++"`.
6738
-
6739
- [*Example 1*:
6740
-
6741
- ``` cpp
6742
- complex sqrt(complex); // C++{} language linkage by default
6743
- extern "C" {
6744
- double sqrt(double); // C language linkage
6745
- }
6746
- ```
6747
-
6748
- — *end example*]
6749
-
6750
- A *module-import-declaration* appearing in a linkage specification with
6751
- other than C++ language linkage is conditionally-supported with
6752
- *implementation-defined* semantics.
6753
-
6754
- Linkage specifications nest. When linkage specifications nest, the
6755
- innermost one determines the language linkage.
6756
-
6757
- [*Note 3*: A linkage specification does not establish a
6758
- scope. — *end note*]
6759
-
6760
- A *linkage-specification* shall inhabit a namespace scope. In a
6761
- *linkage-specification*, the specified language linkage applies to the
6762
- function types of all function declarators and to all functions and
6763
- variables whose names have external linkage.
6764
-
6765
- [*Example 2*:
6766
-
6767
- ``` cpp
6768
- extern "C" // f1 and its function type have C language linkage;
6769
- void f1(void(*pf)(int)); // pf is a pointer to a C function
6770
-
6771
- extern "C" typedef void FUNC();
6772
- FUNC f2; // f2 has C++{} language linkage and
6773
- // its type has C language linkage
6774
-
6775
- extern "C" FUNC f3; // f3 and its type have C language linkage
6776
-
6777
- void (*pf2)(FUNC*); // the variable pf2 has C++{} language linkage; its type
6778
- // is ``pointer to C++{} function that takes one parameter of type
6779
- // pointer to C function''
6780
- extern "C" {
6781
- static void f4(); // the name of the function f4 has internal linkage,
6782
- // so f4 has no language linkage; its type has C language linkage
6783
- }
6784
-
6785
- extern "C" void f5() {
6786
- extern void f4(); // OK, name linkage (internal) and function type linkage (C language linkage)
6787
- // obtained from previous declaration.
6788
- }
6789
-
6790
- extern void f4(); // OK, name linkage (internal) and function type linkage (C language linkage)
6791
- // obtained from previous declaration.
6792
-
6793
- void f6() {
6794
- extern void f4(); // OK, name linkage (internal) and function type linkage (C language linkage)
6795
- // obtained from previous declaration.
6796
- }
6797
- ```
6798
-
6799
- — *end example*]
6800
-
6801
- A C language linkage is ignored in determining the language linkage of
6802
- class members, friend functions with a trailing *requires-clause*, and
6803
- the function type of non-static class member functions.
6804
-
6805
- [*Example 3*:
6806
-
6807
- ``` cpp
6808
- extern "C" typedef void FUNC_c();
6809
-
6810
- class C {
6811
- void mf1(FUNC_c*); // the function mf1 and its type have C++{} language linkage;
6812
- // the parameter has type ``pointer to C function''
6813
-
6814
- FUNC_c mf2; // the function mf2 and its type have C++{} language linkage
6815
-
6816
- static FUNC_c* q; // the data member q has C++{} language linkage;
6817
- // its type is ``pointer to C function''
6818
- };
6819
-
6820
- extern "C" {
6821
- class X {
6822
- void mf(); // the function mf and its type have C++{} language linkage
6823
- void mf2(void(*)()); // the function mf2 has C++{} language linkage;
6824
- // the parameter has type ``pointer to C function''
6825
- };
6826
- }
6827
- ```
6828
-
6829
- — *end example*]
6830
-
6831
- If two declarations of an entity give it different language linkages,
6832
- the program is ill-formed; no diagnostic is required if neither
6833
- declaration is reachable from the other. A redeclaration of an entity
6834
- without a linkage specification inherits the language linkage of the
6835
- entity and (if applicable) its type.
6836
-
6837
- Two declarations declare the same entity if they (re)introduce the same
6838
- name, one declares a function or variable with C language linkage, and
6839
- the other declares such an entity or declares a variable that belongs to
6840
- the global scope.
6841
-
6842
- [*Example 4*:
6843
-
6844
- ``` cpp
6845
- int x;
6846
- namespace A {
6847
- extern "C" int f();
6848
- extern "C" int g() { return 1; }
6849
- extern "C" int h();
6850
- extern "C" int x(); // error: same name as global-space object x
6851
- }
6852
-
6853
- namespace B {
6854
- extern "C" int f(); // A::f and B::f refer to the same function
6855
- extern "C" int g() { return 1; } // error: the function g with C language linkage has two definitions
6856
- }
6857
-
6858
- int A::f() { return 98; } // definition for the function f with C language linkage
6859
- extern "C" int h() { return 97; } // definition for the function h with C language linkage
6860
- // A::h and ::h refer to the same function
6861
- ```
6862
-
6863
- — *end example*]
6864
-
6865
- A declaration directly contained in a *linkage-specification* is treated
6866
- as if it contains the `extern` specifier [[dcl.stc]] for the purpose of
6867
- determining the linkage of the declared name and whether it is a
6868
- definition. Such a declaration shall not specify a storage class.
6869
-
6870
- [*Example 5*:
6871
-
6872
- ``` cpp
6873
- extern "C" double f();
6874
- static double f(); // error
6875
- extern "C" int i; // declaration
6876
- extern "C" {
6877
- int i; // definition
6878
- }
6879
- extern "C" static void g(); // error
6880
- ```
6881
-
6882
- — *end example*]
6883
-
6884
- [*Note 4*: Because the language linkage is part of a function type,
6885
- when indirecting through a pointer to C function, the function to which
6886
- the resulting lvalue refers is considered a C function. — *end note*]
6887
-
6888
- Linkage from C++ to objects defined in other languages and to objects
6889
- defined in C++ from other languages is *implementation-defined* and
6890
- language-dependent. Only where the object layout strategies of two
6891
- language implementations are similar enough can such linkage be
6892
- achieved.
6893
-
6894
- ## Attributes <a id="dcl.attr">[[dcl.attr]]</a>
6895
-
6896
- ### Attribute syntax and semantics <a id="dcl.attr.grammar">[[dcl.attr.grammar]]</a>
6897
-
6898
- Attributes specify additional information for various source constructs
6899
- such as types, variables, names, blocks, or translation units.
6900
-
6901
- ``` bnf
6902
- attribute-specifier-seq:
6903
- attribute-specifier-seqₒₚₜ attribute-specifier
6904
- ```
6905
-
6906
- ``` bnf
6907
- attribute-specifier:
6908
- '[' '[' attribute-using-prefixₒₚₜ attribute-list ']' ']'
6909
- alignment-specifier
6910
- ```
6911
-
6912
- ``` bnf
6913
- alignment-specifier:
6914
- alignas '(' type-id '...'ₒₚₜ ')'
6915
- alignas '(' constant-expression '...'ₒₚₜ ')'
6916
- ```
6917
-
6918
- ``` bnf
6919
- attribute-using-prefix:
6920
- using attribute-namespace ':'
6921
- ```
6922
-
6923
- ``` bnf
6924
- attribute-list:
6925
- attributeₒₚₜ
6926
- attribute-list ',' attributeₒₚₜ
6927
- attribute '...'
6928
- attribute-list ',' attribute '...'
6929
- ```
6930
-
6931
- ``` bnf
6932
- attribute:
6933
- attribute-token attribute-argument-clauseₒₚₜ
6934
- ```
6935
-
6936
- ``` bnf
6937
- attribute-token:
6938
- identifier
6939
- attribute-scoped-token
6940
- ```
6941
-
6942
- ``` bnf
6943
- attribute-scoped-token:
6944
- attribute-namespace '::' identifier
6945
- ```
6946
-
6947
- ``` bnf
6948
- attribute-namespace:
6949
- identifier
6950
- ```
6951
-
6952
- ``` bnf
6953
- attribute-argument-clause:
6954
- '(' balanced-token-seqₒₚₜ ')'
6955
- ```
6956
-
6957
- ``` bnf
6958
- balanced-token-seq:
6959
- balanced-token
6960
- balanced-token-seq balanced-token
6961
- ```
6962
-
6963
- ``` bnf
6964
- balanced-token:
6965
- '(' balanced-token-seqₒₚₜ ')'
6966
- '[' balanced-token-seqₒₚₜ ']'
6967
- '{' balanced-token-seqₒₚₜ '}'
6968
- any *token* other than a parenthesis, a bracket, or a brace
6969
- ```
6970
-
6971
- If an *attribute-specifier* contains an *attribute-using-prefix*, the
6972
- *attribute-list* following that *attribute-using-prefix* shall not
6973
- contain an *attribute-scoped-token* and every *attribute-token* in that
6974
- *attribute-list* is treated as if its *identifier* were prefixed with
6975
- `N::`, where `N` is the *attribute-namespace* specified in the
6976
- *attribute-using-prefix*.
6977
-
6978
- [*Note 1*: This rule imposes no constraints on how an
6979
- *attribute-using-prefix* affects the tokens in an
6980
- *attribute-argument-clause*. — *end note*]
6981
-
6982
- [*Example 1*:
6983
-
6984
- ``` cpp
6985
- [[using CC: opt(1), debug]] // same as [[CC::opt(1), CC::debug]]
6986
- void f() {}
6987
- [[using CC: opt(1)]] [[CC::debug]] // same as [[CC::opt(1)]] [[CC::debug]]
6988
- void g() {}
6989
- [[using CC: CC::opt(1)]] // error: cannot combine using and scoped attribute token
6990
- void h() {}
6991
- ```
6992
-
6993
- — *end example*]
6994
-
6995
- [*Note 2*: For each individual attribute, the form of the
6996
- *balanced-token-seq* will be specified. — *end note*]
6997
-
6998
- In an *attribute-list*, an ellipsis may appear only if that
6999
- *attribute*’s specification permits it. An *attribute* followed by an
7000
- ellipsis is a pack expansion [[temp.variadic]]. An *attribute-specifier*
7001
- that contains no *attribute*s has no effect. The order in which the
7002
- *attribute-token*s appear in an *attribute-list* is not significant. If
7003
- a keyword [[lex.key]] or an alternative token [[lex.digraph]] that
7004
- satisfies the syntactic requirements of an *identifier* [[lex.name]] is
7005
- contained in an *attribute-token*, it is considered an identifier. No
7006
- name lookup [[basic.lookup]] is performed on any of the identifiers
7007
- contained in an *attribute-token*. The *attribute-token* determines
7008
- additional requirements on the *attribute-argument-clause* (if any).
7009
-
7010
- Each *attribute-specifier-seq* is said to *appertain* to some entity or
7011
- statement, identified by the syntactic context where it appears
7012
- [[stmt.stmt]], [[dcl.dcl]], [[dcl.decl]]. If an
7013
- *attribute-specifier-seq* that appertains to some entity or statement
7014
- contains an *attribute* or *alignment-specifier* that is not allowed to
7015
- apply to that entity or statement, the program is ill-formed. If an
7016
- *attribute-specifier-seq* appertains to a friend declaration
7017
- [[class.friend]], that declaration shall be a definition.
7018
-
7019
- [*Note 3*: An *attribute-specifier-seq* cannot appeartain to an
7020
- explicit instantiation [[temp.explicit]]. — *end note*]
7021
-
7022
- For an *attribute-token* (including an *attribute-scoped-token*) not
7023
- specified in this document, the behavior is *implementation-defined*;
7024
- any such *attribute-token* that is not recognized by the implementation
7025
- is ignored.
7026
-
7027
- [*Note 4*: A program is ill-formed if it contains an *attribute*
7028
- specified in [[dcl.attr]] that violates the rules specifying to which
7029
- entity or statement the attribute can apply or the syntax rules for the
7030
- attribute’s *attribute-argument-clause*, if any. — *end note*]
7031
-
7032
- [*Note 5*: The *attribute*s specified in [[dcl.attr]] have optional
7033
- semantics: given a well-formed program, removing all instances of any
7034
- one of those *attribute*s results in a program whose set of possible
7035
- executions [[intro.abstract]] for a given input is a subset of those of
7036
- the original program for the same input, absent implementation-defined
7037
- guarantees with respect to that *attribute*. — *end note*]
7038
-
7039
- An *attribute-token* is reserved for future standardization if
7040
-
7041
- - it is not an *attribute-scoped-token* and is not specified in this
7042
- document, or
7043
- - it is an *attribute-scoped-token* and its *attribute-namespace* is
7044
- `std` followed by zero or more digits.
7045
-
7046
- Each implementation should choose a distinctive name for the
7047
- *attribute-namespace* in an *attribute-scoped-token*.
7048
-
7049
- Two consecutive left square bracket tokens shall appear only when
7050
- introducing an *attribute-specifier* or within the *balanced-token-seq*
7051
- of an *attribute-argument-clause*.
7052
-
7053
- [*Note 6*: If two consecutive left square brackets appear where an
7054
- *attribute-specifier* is not allowed, the program is ill-formed even if
7055
- the brackets match an alternative grammar production. — *end note*]
7056
-
7057
- [*Example 2*:
7058
-
7059
- ``` cpp
7060
- int p[10];
7061
- void f() {
7062
- int x = 42, y[5];
7063
- int(p[[x] { return x; }()]); // error: invalid attribute on a nested declarator-id and
7064
- // not a function-style cast of an element of p.
7065
- y[[] { return 2; }()] = 2; // error even though attributes are not allowed in this context.
7066
- int i [[vendor::attr([[]])]]; // well-formed implementation-defined attribute.
7067
- }
7068
- ```
7069
-
7070
- — *end example*]
7071
-
7072
- ### Alignment specifier <a id="dcl.align">[[dcl.align]]</a>
7073
-
7074
- An *alignment-specifier* may be applied to a variable or to a class data
7075
- member, but it shall not be applied to a bit-field, a function
7076
- parameter, or an *exception-declaration* [[except.handle]]. An
7077
- *alignment-specifier* may also be applied to the declaration of a class
7078
- (in an *elaborated-type-specifier* [[dcl.type.elab]] or *class-head*
7079
- [[class]], respectively). An *alignment-specifier* with an ellipsis is a
7080
- pack expansion [[temp.variadic]].
7081
-
7082
- When the *alignment-specifier* is of the form `alignas(`
7083
- *constant-expression* `)`:
7084
-
7085
- - the *constant-expression* shall be an integral constant expression
7086
- - if the constant expression does not evaluate to an alignment value
7087
- [[basic.align]], or evaluates to an extended alignment and the
7088
- implementation does not support that alignment in the context of the
7089
- declaration, the program is ill-formed.
7090
-
7091
- An *alignment-specifier* of the form `alignas(` *type-id* `)` has the
7092
- same effect as `alignas({}alignof(` *type-id* `))` [[expr.alignof]].
7093
-
7094
- The alignment requirement of an entity is the strictest nonzero
7095
- alignment specified by its *alignment-specifier*s, if any; otherwise,
7096
- the *alignment-specifier*s have no effect.
7097
-
7098
- The combined effect of all *alignment-specifier*s in a declaration shall
7099
- not specify an alignment that is less strict than the alignment that
7100
- would be required for the entity being declared if all
7101
- *alignment-specifier*s appertaining to that entity were omitted.
7102
-
7103
- [*Example 1*:
7104
-
7105
- ``` cpp
7106
- struct alignas(8) S {};
7107
- struct alignas(1) U {
7108
- S s;
7109
- }; // error: U specifies an alignment that is less strict than if the alignas(1) were omitted.
7110
- ```
7111
-
7112
- — *end example*]
7113
-
7114
- If the defining declaration of an entity has an *alignment-specifier*,
7115
- any non-defining declaration of that entity shall either specify
7116
- equivalent alignment or have no *alignment-specifier*. Conversely, if
7117
- any declaration of an entity has an *alignment-specifier*, every
7118
- defining declaration of that entity shall specify an equivalent
7119
- alignment. No diagnostic is required if declarations of an entity have
7120
- different *alignment-specifier*s in different translation units.
7121
-
7122
- [*Example 2*:
7123
-
7124
- ``` cpp
7125
- // Translation unit #1:
7126
- struct S { int x; } s, *p = &s;
7127
-
7128
- // Translation unit #2:
7129
- struct alignas(16) S; // ill-formed, no diagnostic required: definition of S lacks alignment
7130
- extern S* p;
7131
- ```
7132
-
7133
- — *end example*]
7134
-
7135
- [*Example 3*:
7136
-
7137
- An aligned buffer with an alignment requirement of `A` and holding `N`
7138
- elements of type `T` can be declared as:
7139
-
7140
- ``` cpp
7141
- alignas(T) alignas(A) T buffer[N];
7142
- ```
7143
-
7144
- Specifying `alignas(T)` ensures that the final requested alignment will
7145
- not be weaker than `alignof(T)`, and therefore the program will not be
7146
- ill-formed.
7147
-
7148
- — *end example*]
7149
-
7150
- [*Example 4*:
7151
-
7152
- ``` cpp
7153
- alignas(double) void f(); // error: alignment applied to function
7154
- alignas(double) unsigned char c[sizeof(double)]; // array of characters, suitably aligned for a double
7155
- extern unsigned char c[sizeof(double)]; // no alignas necessary
7156
- alignas(float)
7157
- extern unsigned char c[sizeof(double)]; // error: different alignment in declaration
7158
- ```
7159
-
7160
- — *end example*]
7161
-
7162
- ### Assumption attribute <a id="dcl.attr.assume">[[dcl.attr.assume]]</a>
7163
-
7164
- The *attribute-token* `assume` may be applied to a null statement; such
7165
- a statement is an *assumption*. An *attribute-argument-clause* shall be
7166
- present and shall have the form:
7167
-
7168
- ``` bnf
7169
- '(' conditional-expression ')'
7170
- ```
7171
-
7172
- The expression is contextually converted to `bool` [[conv.general]]. The
7173
- expression is not evaluated. If the converted expression would evaluate
7174
- to `true` at the point where the assumption appears, the assumption has
7175
- no effect. Otherwise, the behavior is undefined.
7176
-
7177
- [*Note 1*: The expression is potentially evaluated [[basic.def.odr]].
7178
- The use of assumptions is intended to allow implementations to analyze
7179
- the form of the expression and deduce information used to optimize the
7180
- program. Implementations are not required to deduce any information from
7181
- any particular assumption. — *end note*]
7182
-
7183
- [*Example 1*:
7184
-
7185
- ``` cpp
7186
- int divide_by_32(int x) {
7187
- [[assume(x >= 0)]];
7188
- return x/32; // The instructions produced for the division
7189
- // may omit handling of negative values.
7190
- }
7191
- int f(int y) {
7192
- [[assume(++y == 43)]]; // y is not incremented
7193
- return y; // statement may be replaced with return 42;
7194
- }
7195
- ```
7196
-
7197
- — *end example*]
7198
-
7199
- ### Carries dependency attribute <a id="dcl.attr.depend">[[dcl.attr.depend]]</a>
7200
-
7201
- The *attribute-token* `carries_dependency` specifies dependency
7202
- propagation into and out of functions. No *attribute-argument-clause*
7203
- shall be present. The attribute may be applied to a parameter of a
7204
- function or lambda, in which case it specifies that the initialization
7205
- of the parameter carries a dependency to [[intro.multithread]] each
7206
- lvalue-to-rvalue conversion [[conv.lval]] of that object. The attribute
7207
- may also be applied to a function or a lambda call operator, in which
7208
- case it specifies that the return value, if any, carries a dependency to
7209
- the evaluation of the function call expression.
7210
-
7211
- The first declaration of a function shall specify the
7212
- `carries_dependency` attribute for its *declarator-id* if any
7213
- declaration of the function specifies the `carries_dependency`
7214
- attribute. Furthermore, the first declaration of a function shall
7215
- specify the `carries_dependency` attribute for a parameter if any
7216
- declaration of that function specifies the `carries_dependency`
7217
- attribute for that parameter. If a function or one of its parameters is
7218
- declared with the `carries_dependency` attribute in its first
7219
- declaration in one translation unit and the same function or one of its
7220
- parameters is declared without the `carries_dependency` attribute in its
7221
- first declaration in another translation unit, the program is
7222
- ill-formed, no diagnostic required.
7223
-
7224
- [*Note 1*: The `carries_dependency` attribute does not change the
7225
- meaning of the program, but might result in generation of more efficient
7226
- code. — *end note*]
7227
-
7228
- [*Example 1*:
7229
-
7230
- ``` cpp
7231
- /* Translation unit A. */
7232
-
7233
- struct foo { int* a; int* b; };
7234
- std::atomic<struct foo *> foo_head[10];
7235
- int foo_array[10][10];
7236
-
7237
- [[carries_dependency]] struct foo* f(int i) {
7238
- return foo_head[i].load(memory_order::consume);
7239
- }
7240
-
7241
- int g(int* x, int* y [[carries_dependency]]) {
7242
- return kill_dependency(foo_array[*x][*y]);
7243
- }
7244
-
7245
- /* Translation unit B. */
7246
-
7247
- [[carries_dependency]] struct foo* f(int i);
7248
- int g(int* x, int* y [[carries_dependency]]);
7249
-
7250
- int c = 3;
7251
-
7252
- void h(int i) {
7253
- struct foo* p;
7254
-
7255
- p = f(i);
7256
- do_something_with(g(&c, p->a));
7257
- do_something_with(g(p->a, &c));
7258
- }
7259
- ```
7260
-
7261
- The `carries_dependency` attribute on function `f` means that the return
7262
- value carries a dependency out of `f`, so that the implementation need
7263
- not constrain ordering upon return from `f`. Implementations of `f` and
7264
- its caller may choose to preserve dependencies instead of emitting
7265
- hardware memory ordering instructions (a.k.a. fences). Function `g`’s
7266
- second parameter has a `carries_dependency` attribute, but its first
7267
- parameter does not. Therefore, function `h`’s first call to `g` carries
7268
- a dependency into `g`, but its second call does not. The implementation
7269
- might need to insert a fence prior to the second call to `g`.
7270
-
7271
- — *end example*]
7272
-
7273
- ### Deprecated attribute <a id="dcl.attr.deprecated">[[dcl.attr.deprecated]]</a>
7274
-
7275
- The *attribute-token* `deprecated` can be used to mark names and
7276
- entities whose use is still allowed, but is discouraged for some reason.
7277
-
7278
- [*Note 1*: In particular, `deprecated` is appropriate for names and
7279
- entities that are deemed obsolescent or unsafe. — *end note*]
7280
-
7281
- An *attribute-argument-clause* may be present and, if present, it shall
7282
- have the form:
7283
-
7284
- ``` bnf
7285
- '(' string-literal ')'
7286
- ```
7287
-
7288
- [*Note 2*: The *string-literal* in the *attribute-argument-clause* can
7289
- be used to explain the rationale for deprecation and/or to suggest a
7290
- replacing entity. — *end note*]
7291
-
7292
- The attribute may be applied to the declaration of a class, a
7293
- *typedef-name*, a variable, a non-static data member, a function, a
7294
- namespace, an enumeration, an enumerator, a concept, or a template
7295
- specialization.
7296
-
7297
- An entity declared without the `deprecated` attribute can later be
7298
- redeclared with the attribute and vice-versa.
7299
-
7300
- [*Note 3*: Thus, an entity initially declared without the attribute can
7301
- be marked as deprecated by a subsequent redeclaration. However, after an
7302
- entity is marked as deprecated, later redeclarations do not un-deprecate
7303
- the entity. — *end note*]
7304
-
7305
- Redeclarations using different forms of the attribute (with or without
7306
- the *attribute-argument-clause* or with different
7307
- *attribute-argument-clause*s) are allowed.
7308
-
7309
- *Recommended practice:* Implementations should use the `deprecated`
7310
- attribute to produce a diagnostic message in case the program refers to
7311
- a name or entity other than to declare it, after a declaration that
7312
- specifies the attribute. The diagnostic message should include the text
7313
- provided within the *attribute-argument-clause* of any `deprecated`
7314
- attribute applied to the name or entity.
7315
-
7316
- ### Fallthrough attribute <a id="dcl.attr.fallthrough">[[dcl.attr.fallthrough]]</a>
7317
-
7318
- The *attribute-token* `fallthrough` may be applied to a null statement
7319
- [[stmt.expr]]; such a statement is a fallthrough statement. No
7320
- *attribute-argument-clause* shall be present. A fallthrough statement
7321
- may only appear within an enclosing `switch` statement [[stmt.switch]].
7322
- The next statement that would be executed after a fallthrough statement
7323
- shall be a labeled statement whose label is a case label or default
7324
- label for the same `switch` statement and, if the fallthrough statement
7325
- is contained in an iteration statement, the next statement shall be part
7326
- of the same execution of the substatement of the innermost enclosing
7327
- iteration statement. The program is ill-formed if there is no such
7328
- statement.
7329
-
7330
- *Recommended practice:* The use of a fallthrough statement should
7331
- suppress a warning that an implementation might otherwise issue for a
7332
- case or default label that is reachable from another case or default
7333
- label along some path of execution. Implementations should issue a
7334
- warning if a fallthrough statement is not dynamically reachable.
7335
-
7336
- [*Example 1*:
7337
-
7338
- ``` cpp
7339
- void f(int n) {
7340
- void g(), h(), i();
7341
- switch (n) {
7342
- case 1:
7343
- case 2:
7344
- g();
7345
- [[fallthrough]];
7346
- case 3: // warning on fallthrough discouraged
7347
- do {
7348
- [[fallthrough]]; // error: next statement is not part of the same substatement execution
7349
- } while (false);
7350
- case 6:
7351
- do {
7352
- [[fallthrough]]; // error: next statement is not part of the same substatement execution
7353
- } while (n--);
7354
- case 7:
7355
- while (false) {
7356
- [[fallthrough]]; // error: next statement is not part of the same substatement execution
7357
- }
7358
- case 5:
7359
- h();
7360
- case 4: // implementation may warn on fallthrough
7361
- i();
7362
- [[fallthrough]]; // error
7363
- }
7364
- }
7365
- ```
7366
-
7367
- — *end example*]
7368
-
7369
- ### Likelihood attributes <a id="dcl.attr.likelihood">[[dcl.attr.likelihood]]</a>
7370
-
7371
- The *attribute-token*s `likely` and `unlikely` may be applied to labels
7372
- or statements. No *attribute-argument-clause* shall be present. The
7373
- *attribute-token* `likely` shall not appear in an
7374
- *attribute-specifier-seq* that contains the *attribute-token*
7375
- `unlikely`.
7376
-
7377
- *Recommended practice:* The use of the `likely` attribute is intended to
7378
- allow implementations to optimize for the case where paths of execution
7379
- including it are arbitrarily more likely than any alternative path of
7380
- execution that does not include such an attribute on a statement or
7381
- label. The use of the `unlikely` attribute is intended to allow
7382
- implementations to optimize for the case where paths of execution
7383
- including it are arbitrarily more unlikely than any alternative path of
7384
- execution that does not include such an attribute on a statement or
7385
- label. A path of execution includes a label if and only if it contains a
7386
- jump to that label.
7387
-
7388
- [*Note 1*: Excessive usage of either of these attributes is liable to
7389
- result in performance degradation. — *end note*]
7390
-
7391
- [*Example 1*:
7392
-
7393
- ``` cpp
7394
- void g(int);
7395
- int f(int n) {
7396
- if (n > 5) [[unlikely]] { // n > 5 is considered to be arbitrarily unlikely
7397
- g(0);
7398
- return n * 2 + 1;
7399
- }
7400
-
7401
- switch (n) {
7402
- case 1:
7403
- g(1);
7404
- [[fallthrough]];
7405
-
7406
- [[likely]] case 2: // n == 2 is considered to be arbitrarily more
7407
- g(2); // likely than any other value of n
7408
- break;
7409
- }
7410
- return 3;
7411
- }
7412
- ```
7413
-
7414
- — *end example*]
7415
-
7416
- ### Maybe unused attribute <a id="dcl.attr.unused">[[dcl.attr.unused]]</a>
7417
-
7418
- The *attribute-token* `maybe_unused` indicates that a name or entity is
7419
- possibly intentionally unused. No *attribute-argument-clause* shall be
7420
- present.
7421
-
7422
- The attribute may be applied to the declaration of a class, a
7423
- *typedef-name*, a variable (including a structured binding declaration),
7424
- a non-static data member, a function, an enumeration, or an enumerator.
7425
-
7426
- A name or entity declared without the `maybe_unused` attribute can later
7427
- be redeclared with the attribute and vice versa. An entity is considered
7428
- marked after the first declaration that marks it.
7429
-
7430
- *Recommended practice:* For an entity marked `maybe_unused`,
7431
- implementations should not emit a warning that the entity or its
7432
- structured bindings (if any) are used or unused. For a structured
7433
- binding declaration not marked `maybe_unused`, implementations should
7434
- not emit such a warning unless all of its structured bindings are
7435
- unused.
7436
-
7437
- [*Example 1*:
7438
-
7439
- ``` cpp
7440
- [[maybe_unused]] void f([[maybe_unused]] bool thing1,
7441
- [[maybe_unused]] bool thing2) {
7442
- [[maybe_unused]] bool b = thing1 && thing2;
7443
- assert(b);
7444
- }
7445
- ```
7446
-
7447
- Implementations should not warn that `b` is unused, whether or not
7448
- `NDEBUG` is defined.
7449
-
7450
- — *end example*]
7451
-
7452
- ### Nodiscard attribute <a id="dcl.attr.nodiscard">[[dcl.attr.nodiscard]]</a>
7453
-
7454
- The *attribute-token* `nodiscard` may be applied to a function or a
7455
- lambda call operator or to the declaration of a class or enumeration. An
7456
- *attribute-argument-clause* may be present and, if present, shall have
7457
- the form:
7458
-
7459
- ``` bnf
7460
- '(' string-literal ')'
7461
- ```
7462
-
7463
- A name or entity declared without the `nodiscard` attribute can later be
7464
- redeclared with the attribute and vice-versa.
7465
-
7466
- [*Note 1*: Thus, an entity initially declared without the attribute can
7467
- be marked as `nodiscard` by a subsequent redeclaration. However, after
7468
- an entity is marked as `nodiscard`, later redeclarations do not remove
7469
- the `nodiscard` from the entity. — *end note*]
7470
-
7471
- Redeclarations using different forms of the attribute (with or without
7472
- the *attribute-argument-clause* or with different
7473
- *attribute-argument-clause*s) are allowed.
7474
-
7475
- A *nodiscard type* is a (possibly cv-qualified) class or enumeration
7476
- type marked `nodiscard` in a reachable declaration. A *nodiscard call*
7477
- is either
7478
-
7479
- - a function call expression [[expr.call]] that calls a function
7480
- declared `nodiscard` in a reachable declaration or whose return type
7481
- is a nodiscard type, or
7482
- - an explicit type conversion
7483
- [[expr.type.conv]], [[expr.static.cast]], [[expr.cast]] that
7484
- constructs an object through a constructor declared `nodiscard` in a
7485
- reachable declaration, or that initializes an object of a nodiscard
7486
- type.
7487
-
7488
- *Recommended practice:* Appearance of a nodiscard call as a
7489
- potentially-evaluated discarded-value expression [[expr.prop]] is
7490
- discouraged unless explicitly cast to `void`. Implementations should
7491
- issue a warning in such cases.
7492
-
7493
- [*Note 2*: This is typically because discarding the return value of a
7494
- nodiscard call has surprising consequences. — *end note*]
7495
-
7496
- The *string-literal* in a `nodiscard` *attribute-argument-clause* should
7497
- be used in the message of the warning as the rationale for why the
7498
- result should not be discarded.
7499
-
7500
- [*Example 1*:
7501
-
7502
- ``` cpp
7503
- struct [[nodiscard]] my_scopeguard { ... };
7504
- struct my_unique {
7505
- my_unique() = default; // does not acquire resource
7506
- [[nodiscard]] my_unique(int fd) { ... } // acquires resource
7507
- ~my_unique() noexcept { ... } // releases resource, if any
7508
- ...
7509
- };
7510
- struct [[nodiscard]] error_info { ... };
7511
- error_info enable_missile_safety_mode();
7512
- void launch_missiles();
7513
- void test_missiles() {
7514
- my_scopeguard(); // warning encouraged
7515
- (void)my_scopeguard(), // warning not encouraged, cast to void
7516
- launch_missiles(); // comma operator, statement continues
7517
- my_unique(42); // warning encouraged
7518
- my_unique(); // warning not encouraged
7519
- enable_missile_safety_mode(); // warning encouraged
7520
- launch_missiles();
7521
- }
7522
- error_info &foo();
7523
- void f() { foo(); } // warning not encouraged: not a nodiscard call, because neither
7524
- // the (reference) return type nor the function is declared nodiscard
7525
- ```
7526
-
7527
- — *end example*]
7528
-
7529
- ### Noreturn attribute <a id="dcl.attr.noreturn">[[dcl.attr.noreturn]]</a>
7530
-
7531
- The *attribute-token* `noreturn` specifies that a function does not
7532
- return. No *attribute-argument-clause* shall be present. The attribute
7533
- may be applied to a function or a lambda call operator. The first
7534
- declaration of a function shall specify the `noreturn` attribute if any
7535
- declaration of that function specifies the `noreturn` attribute. If a
7536
- function is declared with the `noreturn` attribute in one translation
7537
- unit and the same function is declared without the `noreturn` attribute
7538
- in another translation unit, the program is ill-formed, no diagnostic
7539
- required.
7540
-
7541
- If a function `f` is called where `f` was previously declared with the
7542
- `noreturn` attribute and `f` eventually returns, the behavior is
7543
- undefined.
7544
-
7545
- [*Note 1*: The function may terminate by throwing an
7546
- exception. — *end note*]
7547
-
7548
- *Recommended practice:* Implementations should issue a warning if a
7549
- function marked `[[noreturn]]` might return.
7550
-
7551
- [*Example 1*:
7552
-
7553
- ``` cpp
7554
- [[ noreturn ]] void f() {
7555
- throw "error"; // OK
7556
- }
7557
-
7558
- [[ noreturn ]] void q(int i) { // behavior is undefined if called with an argument <= 0
7559
- if (i > 0)
7560
- throw "positive";
7561
- }
7562
- ```
7563
-
7564
- — *end example*]
7565
-
7566
- ### No unique address attribute <a id="dcl.attr.nouniqueaddr">[[dcl.attr.nouniqueaddr]]</a>
7567
-
7568
- The *attribute-token* `no_unique_address` specifies that a non-static
7569
- data member is a potentially-overlapping subobject [[intro.object]]. No
7570
- *attribute-argument-clause* shall be present. The attribute may
7571
- appertain to a non-static data member other than a bit-field.
7572
-
7573
- [*Note 1*: The non-static data member can share the address of another
7574
- non-static data member or that of a base class, and any padding that
7575
- would normally be inserted at the end of the object can be reused as
7576
- storage for other members. — *end note*]
7577
-
7578
- [*Example 1*:
7579
-
7580
- ``` cpp
7581
- template<typename Key, typename Value,
7582
- typename Hash, typename Pred, typename Allocator>
7583
- class hash_map {
7584
- [[no_unique_address]] Hash hasher;
7585
- [[no_unique_address]] Pred pred;
7586
- [[no_unique_address]] Allocator alloc;
7587
- Bucket *buckets;
7588
- // ...
7589
- public:
7590
- // ...
7591
- };
7592
- ```
7593
-
7594
- Here, `hasher`, `pred`, and `alloc` could have the same address as
7595
- `buckets` if their respective types are all empty.
7596
-
7597
- — *end example*]
7598
-
7599
- <!-- Link reference definitions -->
7600
- [basic.align]: basic.md#basic.align
7601
- [basic.compound]: basic.md#basic.compound
7602
- [basic.def]: basic.md#basic.def
7603
- [basic.def.odr]: basic.md#basic.def.odr
7604
- [basic.fundamental]: basic.md#basic.fundamental
7605
- [basic.life]: basic.md#basic.life
7606
- [basic.link]: basic.md#basic.link
7607
- [basic.lookup]: basic.md#basic.lookup
7608
- [basic.lookup.argdep]: basic.md#basic.lookup.argdep
7609
- [basic.lookup.elab]: basic.md#basic.lookup.elab
7610
- [basic.lookup.general]: basic.md#basic.lookup.general
7611
- [basic.lookup.qual]: basic.md#basic.lookup.qual
7612
- [basic.lookup.udir]: basic.md#basic.lookup.udir
7613
- [basic.lookup.unqual]: basic.md#basic.lookup.unqual
7614
- [basic.namespace]: #basic.namespace
7615
- [basic.namespace.general]: #basic.namespace.general
7616
- [basic.scope.namespace]: basic.md#basic.scope.namespace
7617
- [basic.scope.scope]: basic.md#basic.scope.scope
7618
- [basic.start]: basic.md#basic.start
7619
- [basic.start.dynamic]: basic.md#basic.start.dynamic
7620
- [basic.start.static]: basic.md#basic.start.static
7621
- [basic.stc]: basic.md#basic.stc
7622
- [basic.stc.auto]: basic.md#basic.stc.auto
7623
- [basic.stc.dynamic]: basic.md#basic.stc.dynamic
7624
- [basic.stc.dynamic.allocation]: basic.md#basic.stc.dynamic.allocation
7625
- [basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
7626
- [basic.stc.static]: basic.md#basic.stc.static
7627
- [basic.stc.thread]: basic.md#basic.stc.thread
7628
- [basic.type.qualifier]: basic.md#basic.type.qualifier
7629
- [class]: class.md#class
7630
- [class.access]: class.md#class.access
7631
- [class.access.base]: class.md#class.access.base
7632
- [class.base.init]: class.md#class.base.init
7633
- [class.bit]: class.md#class.bit
7634
- [class.conv.ctor]: class.md#class.conv.ctor
7635
- [class.conv.fct]: class.md#class.conv.fct
7636
- [class.copy.assign]: class.md#class.copy.assign
7637
- [class.copy.ctor]: class.md#class.copy.ctor
7638
- [class.copy.elision]: class.md#class.copy.elision
7639
- [class.ctor]: class.md#class.ctor
7640
- [class.default.ctor]: class.md#class.default.ctor
7641
- [class.dtor]: class.md#class.dtor
7642
- [class.expl.init]: class.md#class.expl.init
7643
- [class.friend]: class.md#class.friend
7644
- [class.inhctor.init]: class.md#class.inhctor.init
7645
- [class.init]: class.md#class.init
7646
- [class.mem]: class.md#class.mem
7647
- [class.mem.general]: class.md#class.mem.general
7648
- [class.member.lookup]: basic.md#class.member.lookup
7649
- [class.mfct]: class.md#class.mfct
7650
- [class.mi]: class.md#class.mi
7651
- [class.name]: class.md#class.name
7652
- [class.pre]: class.md#class.pre
7653
- [class.qual]: basic.md#class.qual
7654
- [class.static]: class.md#class.static
7655
- [class.static.data]: class.md#class.static.data
7656
- [class.temporary]: basic.md#class.temporary
7657
- [class.union]: class.md#class.union
7658
- [class.union.anon]: class.md#class.union.anon
7659
- [class.virtual]: class.md#class.virtual
7660
- [conv]: expr.md#conv
7661
- [conv.array]: expr.md#conv.array
7662
- [conv.func]: expr.md#conv.func
7663
- [conv.general]: expr.md#conv.general
7664
- [conv.integral]: expr.md#conv.integral
7665
- [conv.lval]: expr.md#conv.lval
7666
- [conv.prom]: expr.md#conv.prom
7667
- [conv.ptr]: expr.md#conv.ptr
7668
- [conv.qual]: expr.md#conv.qual
7669
- [conv.rval]: expr.md#conv.rval
7670
- [coroutine.handle]: support.md#coroutine.handle
7671
- [coroutine.handle.resumption]: support.md#coroutine.handle.resumption
7672
- [dcl.align]: #dcl.align
7673
- [dcl.ambig.res]: #dcl.ambig.res
7674
- [dcl.array]: #dcl.array
7675
- [dcl.asm]: #dcl.asm
7676
- [dcl.attr]: #dcl.attr
7677
- [dcl.attr.assume]: #dcl.attr.assume
7678
- [dcl.attr.depend]: #dcl.attr.depend
7679
- [dcl.attr.deprecated]: #dcl.attr.deprecated
7680
- [dcl.attr.fallthrough]: #dcl.attr.fallthrough
7681
- [dcl.attr.grammar]: #dcl.attr.grammar
7682
- [dcl.attr.likelihood]: #dcl.attr.likelihood
7683
- [dcl.attr.nodiscard]: #dcl.attr.nodiscard
7684
- [dcl.attr.noreturn]: #dcl.attr.noreturn
7685
- [dcl.attr.nouniqueaddr]: #dcl.attr.nouniqueaddr
7686
- [dcl.attr.unused]: #dcl.attr.unused
7687
- [dcl.constexpr]: #dcl.constexpr
7688
- [dcl.constinit]: #dcl.constinit
7689
- [dcl.dcl]: #dcl.dcl
7690
- [dcl.decl]: #dcl.decl
7691
- [dcl.decl.general]: #dcl.decl.general
7692
- [dcl.enum]: #dcl.enum
7693
- [dcl.fct]: #dcl.fct
7694
- [dcl.fct.def]: #dcl.fct.def
7695
- [dcl.fct.def.coroutine]: #dcl.fct.def.coroutine
7696
- [dcl.fct.def.default]: #dcl.fct.def.default
7697
- [dcl.fct.def.delete]: #dcl.fct.def.delete
7698
- [dcl.fct.def.general]: #dcl.fct.def.general
7699
- [dcl.fct.default]: #dcl.fct.default
7700
- [dcl.fct.spec]: #dcl.fct.spec
7701
- [dcl.friend]: #dcl.friend
7702
- [dcl.init]: #dcl.init
7703
- [dcl.init.aggr]: #dcl.init.aggr
7704
- [dcl.init.general]: #dcl.init.general
7705
- [dcl.init.list]: #dcl.init.list
7706
- [dcl.init.ref]: #dcl.init.ref
7707
- [dcl.init.string]: #dcl.init.string
7708
- [dcl.inline]: #dcl.inline
7709
- [dcl.link]: #dcl.link
7710
- [dcl.meaning]: #dcl.meaning
7711
- [dcl.meaning.general]: #dcl.meaning.general
7712
- [dcl.mptr]: #dcl.mptr
7713
- [dcl.name]: #dcl.name
7714
- [dcl.pre]: #dcl.pre
7715
- [dcl.ptr]: #dcl.ptr
7716
- [dcl.ref]: #dcl.ref
7717
- [dcl.spec]: #dcl.spec
7718
- [dcl.spec.auto]: #dcl.spec.auto
7719
- [dcl.spec.auto.general]: #dcl.spec.auto.general
7720
- [dcl.spec.general]: #dcl.spec.general
7721
- [dcl.stc]: #dcl.stc
7722
- [dcl.struct.bind]: #dcl.struct.bind
7723
- [dcl.type]: #dcl.type
7724
- [dcl.type.auto.deduct]: #dcl.type.auto.deduct
7725
- [dcl.type.class.deduct]: #dcl.type.class.deduct
7726
- [dcl.type.cv]: #dcl.type.cv
7727
- [dcl.type.decltype]: #dcl.type.decltype
7728
- [dcl.type.elab]: #dcl.type.elab
7729
- [dcl.type.general]: #dcl.type.general
7730
- [dcl.type.simple]: #dcl.type.simple
7731
- [dcl.typedef]: #dcl.typedef
7732
- [depr.volatile.type]: future.md#depr.volatile.type
7733
- [enum]: #enum
7734
- [enum.udecl]: #enum.udecl
7735
- [except.ctor]: except.md#except.ctor
7736
- [except.handle]: except.md#except.handle
7737
- [except.pre]: except.md#except.pre
7738
- [except.spec]: except.md#except.spec
7739
- [except.throw]: except.md#except.throw
7740
- [expr.alignof]: expr.md#expr.alignof
7741
- [expr.ass]: expr.md#expr.ass
7742
- [expr.await]: expr.md#expr.await
7743
- [expr.call]: expr.md#expr.call
7744
- [expr.cast]: expr.md#expr.cast
7745
- [expr.comma]: expr.md#expr.comma
7746
- [expr.const]: expr.md#expr.const
7747
- [expr.const.cast]: expr.md#expr.const.cast
7748
- [expr.mptr.oper]: expr.md#expr.mptr.oper
7749
- [expr.new]: expr.md#expr.new
7750
- [expr.post.incr]: expr.md#expr.post.incr
7751
- [expr.pre.incr]: expr.md#expr.pre.incr
7752
- [expr.prim.id.unqual]: expr.md#expr.prim.id.unqual
7753
- [expr.prim.lambda]: expr.md#expr.prim.lambda
7754
- [expr.prim.this]: expr.md#expr.prim.this
7755
- [expr.prop]: expr.md#expr.prop
7756
- [expr.ref]: expr.md#expr.ref
7757
- [expr.static.cast]: expr.md#expr.static.cast
7758
- [expr.sub]: expr.md#expr.sub
7759
- [expr.type.conv]: expr.md#expr.type.conv
7760
- [expr.unary]: expr.md#expr.unary
7761
- [expr.unary.op]: expr.md#expr.unary.op
7762
- [expr.yield]: expr.md#expr.yield
7763
- [initializer.list.syn]: support.md#initializer.list.syn
7764
- [intro.abstract]: intro.md#intro.abstract
7765
- [intro.compliance]: intro.md#intro.compliance
7766
- [intro.execution]: basic.md#intro.execution
7767
- [intro.multithread]: basic.md#intro.multithread
7768
- [intro.object]: basic.md#intro.object
7769
- [lex.digraph]: lex.md#lex.digraph
7770
- [lex.key]: lex.md#lex.key
7771
- [lex.name]: lex.md#lex.name
7772
- [lex.string]: lex.md#lex.string
7773
- [module.interface]: module.md#module.interface
7774
- [module.reach]: module.md#module.reach
7775
- [module.unit]: module.md#module.unit
7776
- [namespace.alias]: #namespace.alias
7777
- [namespace.def]: #namespace.def
7778
- [namespace.def.general]: #namespace.def.general
7779
- [namespace.qual]: basic.md#namespace.qual
7780
- [namespace.udecl]: #namespace.udecl
7781
- [namespace.udir]: #namespace.udir
7782
- [namespace.unnamed]: #namespace.unnamed
7783
- [over]: over.md#over
7784
- [over.binary]: over.md#over.binary
7785
- [over.literal]: over.md#over.literal
7786
- [over.match]: over.md#over.match
7787
- [over.match.best]: over.md#over.match.best
7788
- [over.match.class.deduct]: over.md#over.match.class.deduct
7789
- [over.match.conv]: over.md#over.match.conv
7790
- [over.match.copy]: over.md#over.match.copy
7791
- [over.match.ctor]: over.md#over.match.ctor
7792
- [over.match.funcs]: over.md#over.match.funcs
7793
- [over.match.list]: over.md#over.match.list
7794
- [over.match.ref]: over.md#over.match.ref
7795
- [over.match.viable]: over.md#over.match.viable
7796
- [over.oper]: over.md#over.oper
7797
- [over.sub]: over.md#over.sub
7798
- [special]: class.md#special
7799
- [std.modules]: library.md#std.modules
7800
- [stmt.ambig]: stmt.md#stmt.ambig
7801
- [stmt.dcl]: stmt.md#stmt.dcl
7802
- [stmt.expr]: stmt.md#stmt.expr
7803
- [stmt.if]: stmt.md#stmt.if
7804
- [stmt.iter]: stmt.md#stmt.iter
7805
- [stmt.return]: stmt.md#stmt.return
7806
- [stmt.return.coroutine]: stmt.md#stmt.return.coroutine
7807
- [stmt.select]: stmt.md#stmt.select
7808
- [stmt.stmt]: stmt.md#stmt.stmt
7809
- [stmt.switch]: stmt.md#stmt.switch
7810
- [support.runtime]: support.md#support.runtime
7811
- [temp.arg.type]: temp.md#temp.arg.type
7812
- [temp.decls]: temp.md#temp.decls
7813
- [temp.deduct]: temp.md#temp.deduct
7814
- [temp.deduct.call]: temp.md#temp.deduct.call
7815
- [temp.deduct.decl]: temp.md#temp.deduct.decl
7816
- [temp.deduct.guide]: temp.md#temp.deduct.guide
7817
- [temp.dep.type]: temp.md#temp.dep.type
7818
- [temp.expl.spec]: temp.md#temp.expl.spec
7819
- [temp.explicit]: temp.md#temp.explicit
7820
- [temp.fct]: temp.md#temp.fct
7821
- [temp.inst]: temp.md#temp.inst
7822
- [temp.local]: temp.md#temp.local
7823
- [temp.names]: temp.md#temp.names
7824
- [temp.param]: temp.md#temp.param
7825
- [temp.pre]: temp.md#temp.pre
7826
- [temp.res]: temp.md#temp.res
7827
- [temp.spec.partial]: temp.md#temp.spec.partial
7828
- [temp.variadic]: temp.md#temp.variadic
7829
- [term.odr.use]: basic.md#term.odr.use
7830
- [term.padding.bits]: basic.md#term.padding.bits
7831
- [term.scalar.type]: basic.md#term.scalar.type
7832
- [term.unevaluated.operand]: expr.md#term.unevaluated.operand
7833
-
7834
- [^1]: There is no special provision for a *decl-specifier-seq* that
7835
- lacks a *type-specifier* or that has a *type-specifier* that only
7836
- specifies *cv-qualifier*s. The “implicit int” rule of C is no longer
7837
- supported.
7838
-
7839
- [^2]: As indicated by syntax, cv-qualifiers are a significant component
7840
- in function return types.
7841
-
7842
- [^3]: One can explicitly disambiguate the parse either by introducing a
7843
- comma (so the ellipsis will be parsed as part of the
7844
- *parameter-declaration-clause*) or by introducing a name for the
7845
- parameter (so the ellipsis will be parsed as part of the
7846
- *declarator-id*).
7847
-
7848
- [^4]: This means that default arguments cannot appear, for example, in
7849
- declarations of pointers to functions, references to functions, or
7850
- `typedef` declarations.
7851
-
7852
- [^5]: As specified in  [[conv.ptr]], converting an integer literal whose
7853
- value is `0` to a pointer type results in a null pointer value.
7854
-
7855
- [^6]: The syntax provides for empty *braced-init-list*s, but nonetheless
7856
- C++ does not have zero length arrays.
7857
-
7858
- [^7]: This requires a conversion function [[class.conv.fct]] returning a
7859
- reference type.
7860
-
7861
- [^8]: Implementations are permitted to provide additional predefined
7862
- variables with names that are reserved to the implementation
7863
- [[lex.name]]. If a predefined variable is not odr-used
7864
- [[term.odr.use]], its string value need not be present in the
7865
- program image.
7866
-
7867
- [^9]: This set of values is used to define promotion and conversion
7868
- semantics for the enumeration type. It does not preclude an
7869
- expression of enumeration type from having a value that falls
7870
- outside this range.
7871
-
7872
- [^10]: During name lookup in a class hierarchy, some ambiguities can be
7873
- resolved by considering whether one member hides the other along
7874
- some paths [[class.member.lookup]]. There is no such
7875
- disambiguation when considering the set of names found as a result
7876
- of following \*using-directive\*s.
7877
-
7878
- [^11]: A *using-declaration* with more than one *using-declarator* is
7879
- equivalent to a corresponding sequence of *using-declaration*s with
7880
- one *using-declarator* each.