forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeclarators.tex
More file actions
3884 lines (3432 loc) · 112 KB
/
declarators.tex
File metadata and controls
3884 lines (3432 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!TEX root = std.tex
\rSec0[dcl.decl]{Declarators}%
\indextext{declarator|(}
%gram: \rSec1[gram.decl]{Declarators}
%gram:
\indextext{initialization!class~object|seealso{constructor}}%
\indextext{\idxcode{*}|see{declarator, pointer}}
\indextext{\idxcode{\&}|see{declarator, reference}}%
\indextext{\idxcode{::*}|see{declarator, pointer to member}}%
\indextext{\idxcode{[]}|see{declarator, array}}%
\indextext{\idxcode{()}|see{declarator, function}}%
\pnum
A declarator declares a single variable, function, or type, within a declaration.
The
\grammarterm{init-declarator-list}
appearing in a declaration
is a comma-separated sequence of declarators,
each of which can have an initializer.
\begin{bnf}
\nontermdef{init-declarator-list}\br
init-declarator\br
init-declarator-list \terminal{,} init-declarator
\end{bnf}
\begin{bnf}
\nontermdef{init-declarator}\br
declarator initializer\opt
\end{bnf}
\pnum
The three components of a
\grammarterm{simple-declaration}
are the
attributes~(\ref{dcl.attr}), the
specifiers
(\grammarterm{decl-specifier-seq};
\ref{dcl.spec}) and the declarators
(\grammarterm{init-declarator-list}).
The specifiers indicate the type, storage class or other properties of
the entities being declared.
The declarators specify the names of these entities
and (optionally) modify the type of the specifiers with operators such as
\tcode{*}
(pointer
to)
and
\tcode{()}
(function returning).
Initial values can also be specified in a declarator;
initializers are discussed in~\ref{dcl.init} and~\ref{class.init}.
\pnum
Each
\grammarterm{init-declarator}
in a declaration is analyzed separately as if it was in a declaration by
itself.\footnote{A declaration with several declarators is usually equivalent
to the corresponding sequence of declarations each with a single declarator.
That is
\tcode{T D1, D2, ... Dn;}
\noindent is usually equivalent to
\tcode{T D1; T D2; ... T Dn;}
\noindent where
\tcode{T}
is a
\grammarterm{decl-specifier-seq}
and each
\tcode{Di}
is an
\grammarterm{init-declarator}.
An exception occurs when a name introduced by one of the
\grammarterm{declarator}{s}
hides a type name used by the
\grammarterm{decl-specifiers},
so that when the same
\grammarterm{decl-specifiers}
are used in a subsequent declaration, they do not have the same meaning,
as in
\tcode{struct S { ... };}\\
\indent\tcode{S S, T; \textrm{// declare two instances of \tcode{struct S}}}
\noindent which is not equivalent to
\tcode{struct S { ... };}\\
\indent\tcode{S S;}\\
\indent\tcode{S T; \textrm{// error}}
\noindent Another exception occurs when \tcode{T} is \tcode{auto}~(\ref{dcl.spec.auto}),
for example:
\tcode{auto i = 1, j = 2.0; \textrm{// error: deduced types for \tcode{i} and \tcode{j} do not match}}\\
\noindent as opposed to\\
\indent\tcode{auto i = 1; \textrm{// OK: \tcode{i} deduced to have type \tcode{int}}}\\
\indent\tcode{auto j = 2.0; \textrm{// OK: \tcode{j} deduced to have type \tcode{double}}}
}
\pnum
Declarators have the syntax
\begin{bnf}
\nontermdef{declarator}\br
ptr-declarator\br
noptr-declarator parameters-and-qualifiers trailing-return-type
\end{bnf}
\begin{bnf}
\nontermdef{ptr-declarator}\br
noptr-declarator\br
ptr-operator ptr-declarator
\end{bnf}
\begin{bnf}
\nontermdef{noptr-declarator}\br
declarator-id attribute-specifier-seq\opt\br
noptr-declarator parameters-and-qualifiers\br
noptr-declarator \terminal{[} constant-expression\opt \terminal{]} attribute-specifier-seq\opt\br
\terminal{(} ptr-declarator \terminal{)}
\end{bnf}
\begin{bnf}
\nontermdef{parameters-and-qualifiers}\br
\terminal{(} parameter-declaration-clause \terminal{)} cv-qualifier-seq\opt\br
\hspace*{\bnfindentinc}ref-qualifier\opt exception-specification\opt attribute-specifier-seq\opt
\end{bnf}
\begin{bnf}
\nontermdef{trailing-return-type}\br
\terminal{->} trailing-type-specifier-seq abstract-declarator\opt
\end{bnf}
\begin{bnf}
\nontermdef{ptr-operator}\br
\terminal{*} attribute-specifier-seq\opt cv-qualifier-seq\opt\br
\terminal{\&} attribute-specifier-seq\opt\br
\terminal{\&\&} attribute-specifier-seq\opt\br
nested-name-specifier \terminal{*} attribute-specifier-seq\opt cv-qualifier-seq\opt
\end{bnf}
\begin{bnf}
\nontermdef{cv-qualifier-seq}\br
cv-qualifier cv-qualifier-seq\opt
\end{bnf}
\begin{bnf}
\nontermdef{cv-qualifier}\br
\terminal{const}\br
\terminal{volatile}
\end{bnf}
\begin{bnf}
\nontermdef{ref-qualifier}\br
\terminal{\&}\br
\terminal{\&\&}
\end{bnf}
\begin{bnf}
\nontermdef{declarator-id}\br
\terminal{...}\opt id-expression
\end{bnf}
\pnum
The optional \grammarterm{attribute-specifier-seq} in a
\grammarterm{trailing-return-type} appertains to the indicated return type. The
\grammarterm{type-id} in a \grammarterm{trailing-return-type} includes the longest
possible sequence of \grammarterm{abstract-declarator}{s}. \enternote This resolves the
ambiguous binding of array and function declarators. \enterexample
\begin{codeblock}
auto f()->int(*)[4]; // function returning a pointer to array[4] of \tcode{int}
// not function returning array[4] of pointer to \tcode{int}
\end{codeblock}
\exitexample \exitnote
\rSec1[dcl.name]{Type names}
\pnum
\indextext{type~name}%
To specify type conversions explicitly,
\indextext{operator!cast}%
and as an argument of
\tcode{sizeof},
\tcode{alignof},
\tcode{new},
or
\tcode{typeid},
the name of a type shall be specified.
This can be done with a
\grammarterm{type-id},
which is syntactically a declaration for a variable or function
of that type that omits the name of the entity.
\begin{bnf}
\nontermdef{type-id}\br
type-specifier-seq abstract-declarator\opt
\end{bnf}
\begin{bnf}
\nontermdef{abstract-declarator}\br
ptr-abstract-declarator\br
noptr-abstract-declarator\opt parameters-and-qualifiers trailing-return-type\br
abstract-pack-declarator
\end{bnf}
\begin{bnf}
\nontermdef{ptr-abstract-declarator}\br
noptr-abstract-declarator\br
ptr-operator ptr-abstract-declarator\opt
\end{bnf}
\begin{bnf}
\nontermdef{noptr-abstract-declarator}\br
noptr-abstract-declarator\opt parameters-and-qualifiers\br
noptr-abstract-declarator\opt \terminal{[} constant-expression\opt{} \terminal{]} attribute-specifier-seq\opt\br
\terminal{(} ptr-abstract-declarator \terminal{)}
\end{bnf}
\begin{bnf}
\nontermdef{abstract-pack-declarator}\br
noptr-abstract-pack-declarator\br
ptr-operator abstract-pack-declarator
\end{bnf}
\begin{bnf}
\nontermdef{noptr-abstract-pack-declarator}\br
noptr-abstract-pack-declarator parameters-and-qualifiers\br
noptr-abstract-pack-declarator \terminal{[} constant-expression\opt\ \terminal{]} attribute-specifier-seq\opt\br
\terminal{...}
\end{bnf}
It is possible to identify uniquely the location in the
\grammarterm{abstract-declarator}
where the identifier would appear if the construction were a declarator
in a declaration.
The named type is then the same as the type of the
hypothetical identifier.
\enterexample
\indextext{example!type~name}%
\indextext{example!declarator}%
\begin{codeblock}
int // \tcode{int i}
int * // \tcode{int *pi}
int *[3] // \tcode{int *p[3]}
int (*)[3] // \tcode{int (*p3i)[3]}
int *() // \tcode{int *f()}
int (*)(double) // \tcode{int (*pf)(double)}
\end{codeblock}
name respectively the types
``\tcode{int},''
``pointer to
\tcode{int},''
``array of 3 pointers to
\tcode{int},''
``pointer to array of 3
\tcode{int},''
``function of (no parameters) returning pointer to
\tcode{int},''
and ``pointer to a function of
(\tcode{double})
returning
\tcode{int}.''
\exitexample
\pnum
A type can also be named (often more easily) by using a
\grammarterm{typedef}
(\ref{dcl.typedef}).
\rSec1[dcl.ambig.res]{Ambiguity resolution}%
\indextext{ambiguity!declaration~versus cast}%
\indextext{declaration!parentheses~in}
\pnum
The ambiguity arising from the similarity between a function-style cast and
a declaration mentioned in~\ref{stmt.ambig} can also occur in the context of a declaration.
In that context, the choice is between a function declaration with
a redundant set of parentheses around a parameter name and an object declaration
with a function-style cast as the initializer.
Just as for the ambiguities mentioned in~\ref{stmt.ambig},
the resolution is to consider any construct that could possibly
be a declaration a declaration.
\enternote
A declaration can be explicitly disambiguated by a nonfunction-style
cast, by an
\tcode{=}
to indicate initialization or
by removing the redundant parentheses around the parameter name.
\exitnote
\enterexample
\begin{codeblock}
struct S {
S(int);
};
void foo(double a) {
S w(int(a)); // function declaration
S x(int()); // function declaration
S y((int)a); // object declaration
S z = int(a); // object declaration
}
\end{codeblock}
\exitexample
\pnum
The ambiguity arising from the similarity between a function-style
cast and a
\grammarterm{type-id}
can occur in different contexts.
The ambiguity appears as a choice between a function-style cast
expression and a declaration of a type.
The resolution is that any construct that could possibly be a
\grammarterm{type-id}
in its syntactic context shall be considered a
\grammarterm{type-id}.
\pnum
\enterexample
\begin{codeblock}
#include <cstddef>
char* p;
void* operator new(std::size_t, int);
void foo() {
const int x = 63;
new (int(*p)) int; // new-placement
new (int(*[x])); // parenthesized type-id
}
\end{codeblock}
\pnum
For another example,
\begin{codeblock}
template <class T>
struct S {
T* p;
};
S<int()> x; // type-id
S<int(1)> y; // expression (ill-formed)
\end{codeblock}
\pnum
For another example,
\begin{codeblock}
void foo() {
sizeof(int(1)); // expression
sizeof(int()); // type-id (ill-formed)
}
\end{codeblock}
\pnum
For another example,
\begin{codeblock}
void foo() {
(int(1)); // expression
(int())1; // type-id (ill-formed)
}
\end{codeblock}
\exitexample
\pnum
Another ambiguity arises in a
\grammarterm{parameter-declaration-clause}
of a function declaration, or in a
\grammarterm{type-id}
that is the operand of a
\tcode{sizeof}
or
\tcode{typeid}
operator, when a
\grammarterm{type-name}
is nested in parentheses.
In this case, the choice is between the declaration of a parameter of type
pointer to function and the declaration of a parameter with redundant
parentheses around the
\grammarterm{declarator-id}.
The resolution is to consider the
\grammarterm{type-name}
as a
\grammarterm{simple-type-specifier}
rather than a
\grammarterm{declarator-id}.
\enterexample
\begin{codeblock}
class C { };
void f(int(C)) { } // \tcode{void f(int(*fp)(C c)) \{ \}}
// not: \tcode{void f(int C)};
int g(C);
void foo() {
f(1); // error: cannot convert \tcode{1} to function pointer
f(g); // OK
}
\end{codeblock}
For another example,
\begin{codeblock}
class C { };
void h(int *(C[10])); // \tcode{void h(int *(*_fp)(C _parm[10]));}
// not: \tcode{void h(int *C[10]);}
\end{codeblock}
\exitexample
\rSec1[dcl.meaning]{Meaning of declarators}%
\indextext{declarator!meaning~of|(}
\pnum
A list of declarators appears after an optional (Clause~\ref{dcl.dcl})
\grammarterm{decl-specifier-seq}
(\ref{dcl.spec}).
\indextext{declaration!type}%
Each declarator contains exactly one
\grammarterm{declarator-id};
it names the identifier that is declared.
An
\grammarterm{unqualified-id}
occurring in
a
\grammarterm{declarator-id}
shall be a simple
\grammarterm{identifier}
except for the declaration of some special functions~(\ref{class.ctor},
\ref{class.conv}, \ref{class.dtor}, \ref{over.oper}) and
for the declaration of template specializations
or partial specializations~(\ref{temp.spec}).
When the
\grammarterm{declarator-id}
is qualified, the declaration shall refer to a previously declared member
of the class or namespace to which the qualifier refers (or,
in the case of a namespace,
of an element of the inline namespace
set of that namespace~(\ref{namespace.def})) or to a specialization thereof; the member
shall not merely have been introduced by a
\grammarterm{using-declaration}
in the scope of the class or namespace nominated by the
\grammarterm{nested-name-specifier}
of the
\grammarterm{declarator-id}.
The \grammarterm{nested-name-specifier} of a qualified \grammarterm{declarator-id} shall not
begin with a \grammarterm{decltype-specifier}.
\enternote
If the qualifier is the global
\tcode{::}
scope resolution operator, the
\grammarterm{declarator-id}
refers to a name declared in the global namespace scope.
\exitnote
The optional \grammarterm{attribute-specifier-seq} following a \grammarterm{declarator-id} appertains to the entity that is declared.
\pnum
A
\tcode{static},
\tcode{thread_local},
\tcode{extern},
\tcode{register},
\tcode{mutable},
\tcode{friend},
\tcode{inline},
\tcode{virtual},
or
\tcode{typedef}
specifier applies directly to each
\grammarterm{declarator-id}
in an
\grammarterm{init-declarator-list};
the type specified for each
\grammarterm{declarator-id}
depends on both the
\grammarterm{decl-specifier-seq}
and its
\grammarterm{declarator}.
\pnum
Thus, a declaration of a particular identifier has the form
\begin{codeblock}
T D
\end{codeblock}
where
\tcode{T}
is of the form \grammarterm{attribute-specifier-seq\opt}
\grammarterm{decl-specifier-seq}
and
\tcode{D}
is a declarator.
Following is a recursive procedure for determining
the type specified for the contained
\grammarterm{declarator-id}
by such a declaration.
\pnum
First, the
\grammarterm{decl-specifier-seq}
determines a type.
In a declaration
\begin{codeblock}
T D
\end{codeblock}
the
\grammarterm{decl-specifier-seq}
\tcode{T}
determines the type
\tcode{T}.
\enterexample
in the declaration
\begin{codeblock}
int unsigned i;
\end{codeblock}
the type specifiers
\tcode{int}
\tcode{unsigned}
determine the type
``\tcode{unsigned int}''
(\ref{dcl.type.simple}).
\exitexample
\pnum
In a declaration
\grammarterm{attribute-specifier-seq\opt}
\tcode{T}
\tcode{D}
where
\tcode{D}
is an unadorned identifier the type of this identifier is
``\tcode{T}''.
\pnum
In a declaration
\tcode{T}
\tcode{D}
where
\tcode{D}
has the form
\begin{ncsimplebnf}
( D1 )
\end{ncsimplebnf}
the type of the contained
\grammarterm{declarator-id}
is the same as that of the contained
\grammarterm{declarator-id}
in the declaration
\begin{codeblock}
T D1
\end{codeblock}
\indextext{declaration!parentheses~in}%
Parentheses do not alter the type of the embedded
\grammarterm{declarator-id},
but they can alter the binding of complex declarators.
\rSec2[dcl.ptr]{Pointers}%
\indextext{declarator!pointer}%
\pnum
In a declaration
\tcode{T}
\tcode{D}
where
\tcode{D}
has the form
\begin{ncsimplebnf}
\terminal{*} attribute-specifier-seq\opt cv-qualifier-seq\opt \terminal{D1}
\end{ncsimplebnf}
and the type of the identifier in the declaration
\tcode{T}
\tcode{D1}
is ``\nonterminal{derived-declarator-type-list}
\tcode{T},''
then the type of the identifier of
\tcode{D}
is ``\nonterminal{derived-declarator-type-list cv-qualifier-seq} pointer to
\tcode{T}.''
\indextext{declaration!pointer}%
\indextext{declaration!constant~pointer}%
The
\grammarterm{cv-qualifier}{s}
apply to the pointer and not to the object pointed to.
Similarly, the optional \grammarterm{attribute-specifier-seq}~(\ref{dcl.attr.grammar}) appertains to the pointer and not to the object pointed to.
\pnum
\enterexample
the declarations
\indextext{example!\idxcode{const}}%
\indextext{example!constant pointer}%
\begin{codeblock}
const int ci = 10, *pc = &ci, *const cpc = pc, **ppc;
int i, *p, *const cp = &i;
\end{codeblock}
declare
\tcode{ci},
a constant integer;
\tcode{pc},
a pointer to a constant integer;
\tcode{cpc},
a constant pointer to a constant integer;
\tcode{ppc},
a pointer to a pointer to a constant integer;
\tcode{i},
an integer;
\tcode{p},
a pointer to integer; and
\tcode{cp},
a constant pointer to integer.
The value of
\tcode{ci},
\tcode{cpc},
and
\tcode{cp}
cannot be changed after initialization.
The value of
\tcode{pc}
can be changed, and so can the object pointed to by
\tcode{cp}.
Examples of
some correct operations are
\begin{codeblock}
i = ci;
*cp = ci;
pc++;
pc = cpc;
pc = p;
ppc = &pc;
\end{codeblock}
Examples of ill-formed operations are
\begin{codeblock}
ci = 1; // error
ci++; // error
*pc = 2; // error
cp = &ci; // error
cpc++; // error
p = pc; // error
ppc = &p; // error
\end{codeblock}
Each is unacceptable because it would either change the value of an object declared
\tcode{const}
or allow it to be changed through a cv-unqualified pointer later, for example:
\begin{codeblock}
*ppc = &ci; // OK, but would make \tcode{p} point to \tcode{ci} ...
// ... because of previous error
*p = 5; // clobber \tcode{ci}
\end{codeblock}
\exitexample
\pnum
See also~\ref{expr.ass} and~\ref{dcl.init}.
\pnum
\enternote
Forming a pointer to reference type is ill-formed; see~\ref{dcl.ref}.
Forming a pointer to function type is ill-formed if the function type has
\grammarterm{cv-qualifier}{s} or a \grammarterm{ref-qualifier};
see~\ref{dcl.fct}.
Since the address of a bit-field (\ref{class.bit}) cannot be taken,
a pointer can never point to a bit-field.
\exitnote
\rSec2[dcl.ref]{References}%
\indextext{declarator!reference}
\pnum
In a declaration
\tcode{T}
\tcode{D}
where
\tcode{D}
has either of the forms
\begin{ncsimplebnf}
\terminal{\&} attribute-specifier-seq\opt \terminal{D1}\br
\terminal{\&\&} attribute-specifier-seq\opt \terminal{D1}
\end{ncsimplebnf}
and the type of the identifier in the declaration
\tcode{T}
\tcode{D1}
is ``\nonterminal{derived-declarator-type-list}
\tcode{T},''
then the type of the identifier of
\tcode{D}
is ``\nonterminal{derived-declarator-type-list} reference to
\tcode{T}.''
The optional \grammarterm{attribute-specifier-seq} appertains to the reference type.
Cv-qualified references are ill-formed except when the cv-qualifiers
are introduced through the use of a
\grammarterm{typedef-name}~(\ref{dcl.typedef}, \ref{temp.param}) or
\grammarterm{decltype-specifier}~(\ref{dcl.type.simple}),
in which case the cv-qualifiers are ignored.
\enterexample
\begin{codeblock}
typedef int& A;
const A aref = 3; // ill-formed; lvalue reference to non-\tcode{const} initialized with rvalue
\end{codeblock}
The type of
\tcode{aref}
is ``lvalue reference to \tcode{int}'',
not ``lvalue reference to \tcode{const int}''.
\exitexample
\indextext{\idxcode{void\&}}%
\enternote
A reference can be thought of as a name of an object.
\exitnote
A declarator that specifies the type
``reference to \cv \tcode{void}''
is ill-formed.
\pnum
\indextext{lvalue~reference}%
\indextext{rvalue~reference}%
A reference type that is declared using \tcode{\&} is called an
\term{lvalue reference}, and a reference type that
is declared using \tcode{\&\&} is called an
\term{rvalue reference}. Lvalue references and
rvalue references are distinct types. Except where explicitly noted, they are
semantically equivalent and commonly referred to as references.
\pnum
\indextext{declaration!reference}%
\indextext{parameter!reference}%
\enterexample
\begin{codeblock}
void f(double& a) { a += 3.14; }
// ...
double d = 0;
f(d);
\end{codeblock}
declares
\tcode{a}
to be a reference parameter of
\tcode{f}
so the call
\tcode{f(d)}
will add
\tcode{3.14}
to
\tcode{d}.
\begin{codeblock}
int v[20];
// ...
int& g(int i) { return v[i]; }
// ...
g(3) = 7;
\end{codeblock}
declares the function
\tcode{g()}
to return a reference to an integer so
\tcode{g(3)=7}
will assign
\tcode{7}
to the fourth element of the array
\tcode{v}.
For another example,
\begin{codeblock}
struct link {
link* next;
};
link* first;
void h(link*& p) { // \tcode{p} is a reference to pointer
p->next = first;
first = p;
p = 0;
}
void k() {
link* q = new link;
h(q);
}
\end{codeblock}
declares
\tcode{p}
to be a reference to a pointer to
\tcode{link}
so
\tcode{h(q)}
will leave
\tcode{q}
with the value zero.
See also~\ref{dcl.init.ref}.
\exitexample
\pnum
It is unspecified whether or not
a reference requires storage (\ref{basic.stc}).
\pnum
\indextext{restriction!reference}%
There shall be no references to references,
no arrays of references, and no pointers to references.
\indextext{initialization!reference}%
The declaration of a reference shall contain an
\grammarterm{initializer}
(\ref{dcl.init.ref})
except when the declaration contains an explicit
\tcode{extern}
specifier (\ref{dcl.stc}),
is a class member (\ref{class.mem}) declaration within a class definition,
or is the declaration of a parameter or a return type (\ref{dcl.fct}); see~\ref{basic.def}.
A reference shall be initialized to refer to a valid object or function.
\enternote
\indextext{reference!null}%
in particular, a null reference cannot exist in a well-defined program,
because the only way to create such a reference would be to bind it to
the ``object'' obtained by indirection through a null pointer,
which causes undefined behavior.
As described in~\ref{class.bit}, a reference cannot be bound directly
to a bit-field.
\exitnote
\pnum
\indextext{reference collapsing}%
If a \grammarterm{typedef-name}~(\ref{dcl.typedef}, \ref{temp.param})
or a \grammarterm{decltype-specifier}~(\ref{dcl.type.simple}) denotes a type \tcode{TR} that
is a reference to a type \tcode{T}, an attempt to create the type ``lvalue reference to \cv\
\tcode{TR}'' creates the type ``lvalue reference to \tcode{T}'', while an attempt to create
the type ``rvalue reference to \cv\ \tcode{TR}'' creates the type \tcode{TR}. \enterexample
\begin{codeblock}
int i;
typedef int& LRI;
typedef int&& RRI;
LRI& r1 = i; // \tcode{r1} has the type \tcode{int\&}
const LRI& r2 = i; // \tcode{r2} has the type \tcode{int\&}
const LRI&& r3 = i; // \tcode{r3} has the type \tcode{int\&}
RRI& r4 = i; // \tcode{r4} has the type \tcode{int\&}
RRI&& r5 = 5; // \tcode{r5} has the type \tcode{int\&\&}
decltype(r2)& r6 = i; // \tcode{r6} has the type \tcode{int\&}
decltype(r2)&& r7 = i; // \tcode{r7} has the type \tcode{int\&}
\end{codeblock}
\exitexample
\pnum
\enternote Forming a reference to function type is ill-formed if the function
type has \grammarterm{cv-qualifier}{s} or a \grammarterm{ref-qualifier};
see~\ref{dcl.fct}.
\exitnote
\rSec2[dcl.mptr]{Pointers to members}%
\indextext{declarator!pointer to member}
\pnum
In a declaration
\tcode{T}
\tcode{D}
where
\tcode{D}
has the form
\begin{ncsimplebnf}
nested-name-specifier \terminal{*} attribute-specifier-seq\opt cv-qualifier-seq\opt \tcode{D1}
\end{ncsimplebnf}
and the
\grammarterm{nested-name-specifier}
denotes a class,
and the type of the identifier in the declaration
\tcode{T}
\tcode{D1}
is ``\nonterminal{derived-declarator-type-list}
\tcode{T}'',
then the type of the identifier of
\tcode{D}
is ``\nonterminal{derived-declarator-type-list cv-qualifier-seq} pointer to member of class
\nonterminal{nested-name-specifier} of type
\tcode{T}''.
The optional \grammarterm{attribute-specifier-seq}~(\ref{dcl.attr.grammar}) appertains to the
pointer-to-member.
\pnum
\enterexample%
\indextext{example!pointer~to~member}
\begin{codeblock}
struct X {
void f(int);
int a;
};
struct Y;
int X::* pmi = &X::a;
void (X::* pmf)(int) = &X::f;
double X::* pmd;
char Y::* pmc;
\end{codeblock}
declares
\tcode{pmi},
\tcode{pmf},
\tcode{pmd}
and
\tcode{pmc}
to be a pointer to a member of
\tcode{X}
of type
\tcode{int},
a pointer to a member of
\tcode{X}
of type
\tcode{void(int)},
a pointer to a member of
\tcode{X}
of type
\tcode{double}
and a pointer to a member of
\tcode{Y}
of type
\tcode{char}
respectively.
The declaration of
\tcode{pmd}
is well-formed even though
\tcode{X}
has no members of type
\tcode{double}.
Similarly, the declaration of
\tcode{pmc}
is well-formed even though
\tcode{Y}
is an incomplete type.
\tcode{pmi}
and
\tcode{pmf}
can be used like this:
\begin{codeblock}
X obj;
// ...
obj.*pmi = 7; // assign \tcode{7} to an integer
// member of \tcode{obj}
(obj.*pmf)(7); // call a function member of \tcode{obj}
// with the argument \tcode{7}
\end{codeblock}
\exitexample
\pnum
A pointer to member shall not point to a static member
of a class (\ref{class.static}),
a member with reference type,
or
``\cv
\tcode{void}.''
\enternote
See also~\ref{expr.unary} and~\ref{expr.mptr.oper}.
The type ``pointer to member'' is distinct from the type ``pointer'',
that is, a pointer to member is declared only by the pointer to member
declarator syntax, and never by the pointer declarator syntax.
There is no ``reference-to-member'' type in \Cpp.
\exitnote
\rSec2[dcl.array]{Arrays}%
\indextext{declarator!array}
\pnum
In a declaration
\tcode{T}
\tcode{D}