forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecial.tex
More file actions
3274 lines (2882 loc) · 100 KB
/
special.tex
File metadata and controls
3274 lines (2882 loc) · 100 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
\rSec0[special]{Special member functions}
%gram: \rSec1[gram.special]{Special member functions}
%gram:
\indextext{special~member~function|see{constructor, destructor, inline function,
user-defined conversion, virtual function}}%
\indextext{\idxcode{X(X\&)}|see{copy~constructor}}%
\indextext{\~@\tcode{\tilde}|see{destructor}}%
\indextext{assignment!copy|see{assignment operator, copy}}%
\indextext{assignment!move|see{assignment operator, move}}%
\indextext{implicitly-declared~default~constructor|see{constructor, default}}
\pnum
\indextext{constructor!default}%
\indextext{constructor!copy}%
\indextext{constructor!move}%
\indextext{assignment operator!copy}%
\indextext{assignment operator!move}%
The default constructor~(\ref{class.ctor}),
copy constructor and copy assignment operator~(\ref{class.copy}),
move constructor and move assignment operator~(\ref{class.copy}),
and destructor~(\ref{class.dtor}) are
\term{special member functions}.
\enternote The implementation will implicitly declare these member functions for some class
types when the program does not explicitly declare them.
The implementation will implicitly define them if they are odr-used~(\ref{basic.def.odr}).
See~\ref{class.ctor}, \ref{class.dtor} and~\ref{class.copy}. \exitnote
Programs shall not define implicitly-declared special member functions.
\pnum
Programs may explicitly refer to implicitly-declared special member functions.
\enterexample
a program may explicitly call, take the address of or form a pointer to member
to an implicitly-declared special member function.
\begin{codeblock}
struct A { }; // implicitly declared \tcode{A::operator=}
struct B : A {
B& operator=(const B &);
};
B& B::operator=(const B& s) {
this->A::operator=(s); // well formed
return *this;
}
\end{codeblock}
\exitexample
\pnum
\enternote
The special member functions affect the way objects of class type are created,
copied, moved, and destroyed, and how values can be converted to values of other types.
Often such special member functions are called implicitly.
\exitnote
\pnum
\indextext{access~control!member~function~and}%
Special member functions obey the usual access rules (Clause~\ref{class.access}).
\enterexample
declaring a constructor
\tcode{protected}
ensures that only derived classes and friends can create objects using it.
\exitexample
\rSec1[class.ctor]{Constructors}%
\indextext{constructor}
\pnum
Constructors do not have names.
A special declarator syntax is used to declare or define the constructor.
The syntax uses:
\begin{itemize}
\item an optional \grammarterm{decl-specifier-seq} in which each
\grammarterm{decl-specifier} is either a \grammarterm{function-specifier} or \tcode{constexpr},
\item the constructor's class name, and
\item a parameter list
\end{itemize}
in that order.
In such a declaration, optional parentheses around the constructor class name
are ignored.
\enterexample
\begin{codeblock}
struct S {
S(); // declares the constructor
};
S::S() { } // defines the constructor
\end{codeblock}
\exitexample
\pnum
A constructor is used to initialize objects of its class type.
Because constructors do not have names, they are never found during
name lookup; however an explicit type conversion using the functional
notation~(\ref{expr.type.conv}) will cause a constructor to be called to
initialize an object.
\enternote
For initialization of objects of class type see~\ref{class.init}.
\exitnote
\pnum
A
\grammarterm{typedef-name}
shall not be used as the
\grammarterm{class-name}
in the
\grammarterm{declarator-id}
for a constructor declaration.
\pnum
A constructor shall not be
\tcode{virtual}~(\ref{class.virtual}) or
\tcode{static}~(\ref{class.static}).
\indextext{\idxcode{const}!constructor~and}%
\indextext{\idxcode{volatile}!constructor~and}%
A constructor can be invoked for a
\tcode{const},
\tcode{volatile}
or
\tcode{const}
\tcode{volatile}
object.
\indextext{restriction!constructor}%
A constructor shall not be declared
\tcode{const},
\tcode{volatile},
or
\tcode{const}
\tcode{volatile}~(\ref{class.this}).
\tcode{const}
and
\tcode{volatile}
semantics~(\ref{dcl.type.cv}) are not applied on an object under construction.
They come into effect when the constructor for the
most derived object~(\ref{intro.object}) ends.
A constructor shall not be declared with a \grammarterm{ref-qualifier}.
\pnum
\indextext{constructor!inheritance~of}%
\indextext{constructor!default}%
\indextext{constructor!non-trivial}%
A
\term{default}
constructor for a class
\tcode{X}
is a constructor of class
\tcode{X}
that can be called without an argument.
\indextext{implicitly-declared~default~constructor}%
If there is no user-declared constructor for class
\tcode{X},
a constructor having no parameters is implicitly declared
as defaulted~(\ref{dcl.fct.def}).
An implicitly-declared default constructor is an
\tcode{inline}
\tcode{public}
member of its class.
A defaulted default constructor for class \tcode{X} is defined as deleted if:
\begin{itemize}
\item \tcode{X} is a union-like class that has a variant
member with a non-trivial default constructor,
\item any non-static data member with no \grammarterm{brace-or-equal-initializer} is of reference type,
\item any non-variant non-static data member of const-qualified type (or array
thereof) with no \grammarterm{brace-or-equal-initializer} does not have a user-provided default constructor,
\item \tcode{X} is a union and all of its variant members are of const-qualified
type (or array thereof),
\item \tcode{X} is a non-union class and all members of any anonymous union member are
of const-qualified type (or array thereof),
\item any direct or virtual base class, or non-static data member
with no \grammarterm{brace-or-equal-initializer}, has
class type \tcode{M} (or array thereof) and either \tcode{M}
has no default constructor or overload resolution
(\ref{over.match}) as applied to \tcode{M}'s default
constructor results in an ambiguity or in a function that is deleted or
inaccessible from the defaulted default constructor, or
\item any direct or virtual base class or non-static data member has a type
with a destructor that is deleted or inaccessible from the defaulted default
constructor.
\end{itemize}
A default constructor is
trivial
if it is not user-provided and if:
\begin{itemize}
\item
its class has no virtual functions~(\ref{class.virtual}) and no virtual base
classes~(\ref{class.mi}), and
\item no non-static data member of its class has a \grammarterm{brace-or-equal-initializer}, and
\item
all the direct base classes of its class have trivial default constructors, and
\item
for all the non-static data members of its class that are of class
type (or array thereof), each such class has a trivial default constructor.
\end{itemize}
Otherwise, the default constructor is
\grammarterm{non-trivial}.
\pnum
\indextext{constructor!implicitly defined}%
A default constructor
that is defaulted and not defined as deleted
is
\term{implicitly defined}
when it is odr-used~(\ref{basic.def.odr})
to create an object of its class type~(\ref{intro.object})
or when it is explicitly defaulted after its first declaration.
The implicitly-defined default constructor performs the set of
initializations of the class that would be performed by a user-written default
constructor for that class with no
\grammarterm{ctor-initializer}~(\ref{class.base.init}) and an empty
\grammarterm{compound-statement}.
If that user-written default constructor would be ill-formed,
the program is ill-formed.
If that user-written default constructor would satisfy the requirements
of a \tcode{constexpr} constructor~(\ref{dcl.constexpr}), the implicitly-defined
default constructor is \tcode{constexpr}.
Before the defaulted default constructor for a class is
implicitly defined,
all the non-user-provided default constructors for its base classes and
its non-static data members shall have been implicitly defined.
\enternote
An implicitly-declared default constructor has an
\grammarterm{exception-specification}~(\ref{except.spec}).
An explicitly-defaulted definition might have an
implicit \grammarterm{exception-specification,} see~\ref{dcl.fct.def}.
\exitnote
\pnum
\indextext{constructor!implicitly called}%
Default constructors are called implicitly to create class objects of static, thread,
or automatic storage duration~(\ref{basic.stc.static}, \ref{basic.stc.thread}, \ref{basic.stc.auto}) defined
without an initializer~(\ref{dcl.init}),
are called to create class objects of dynamic storage duration~(\ref{basic.stc.dynamic}) created by a
\grammarterm{new-expression}
in which the
\grammarterm{new-initializer}
is omitted~(\ref{expr.new}), or
are called when the explicit type conversion syntax~(\ref{expr.type.conv}) is
used.
A program is ill-formed if the default constructor for an object
is implicitly used and the constructor is not accessible (Clause~\ref{class.access}).
\pnum
\enternote
\indextext{order~of~execution!base~class constructor}%
\indextext{order~of~execution!member constructor}%
\ref{class.base.init} describes the order in which constructors for base
classes and non-static data members are called and
describes how arguments can be specified for the calls to these constructors.
\exitnote
\pnum
\indextext{constructor!copy}%
\indextext{constructor!move}%
A copy constructor~(\ref{class.copy}) is used to copy objects of class type.
A move constructor~(\ref{class.copy}) is used to move the contents of objects of class type.
\pnum
\indextext{restriction!constructor}%
\indextext{constructor!type~of}%
No return type (not even
\tcode{void})
shall be specified for a constructor.
A
\tcode{return}
statement in the body of a constructor shall not specify a return value.
\indextext{constructor!address~of}%
The address of a constructor shall not be taken.
\pnum
\indextext{object!unnamed}%
\indextext{constructor!explicit call}%
A functional notation type conversion~(\ref{expr.type.conv}) can be used
to create new objects of its type.
\enternote
The syntax looks like an explicit call of the constructor.
\exitnote
\enterexample
\indextext{example!constructor}%
\begin{codeblock}
complex zz = complex(1,2.3);
cprint( complex(7.8,1.2) );
\end{codeblock}
\exitexample
\pnum
An object created in this way is unnamed.
\enternote
\ref{class.temporary} describes the lifetime of temporary objects.
\exitnote
\enternote
Explicit constructor calls do not yield lvalues, see~\ref{basic.lval}.
\exitnote
\pnum
\enternote
\indextext{member function!constructor~and}%
some language constructs have special semantics when used during construction;
see~\ref{class.base.init} and~\ref{class.cdtor}.
\exitnote
\pnum
During the construction of a
\tcode{const}
object, if the value of the object or any of its subobjects is
accessed through a glvalue that is not obtained, directly or indirectly, from
the constructor's
\tcode{this}
pointer, the value of the object or subobject thus obtained is unspecified.
\enterexample
\begin{codeblock}
struct C;
void no_opt(C*);
struct C {
int c;
C() : c(0) { no_opt(this); }
};
const C cobj;
void no_opt(C* cptr) {
int i = cobj.c * 100; // value of \tcode{cobj.c} is unspecified
cptr->c = 1;
cout << cobj.c * 100 // value of \tcode{cobj.c} is unspecified
<< '\n';
}
\end{codeblock}
\exitexample
\rSec1[class.temporary]{Temporary objects}
\pnum
\indextext{object~temporary|see{temporary}}%
\indextext{temporary}%
\indextext{optimization~of~temporary|see{elimination~of~temporary}}%
\indextext{temporary!elimination~of}%
\indextext{temporary!implementation-defined~generation~of}%
Temporaries of class type are created in various contexts:
binding a reference to a prvalue~(\ref{dcl.init.ref}),
returning a prvalue~(\ref{stmt.return}),
a conversion that creates a prvalue~(\ref{conv.lval}, \ref{expr.static.cast},
\ref{expr.const.cast}, \ref{expr.cast}),
throwing an exception~(\ref{except.throw}),
entering a
\term{handler}~(\ref{except.handle}), and in some initializations~(\ref{dcl.init}).
\enternote
The lifetime of exception objects is described in~\ref{except.throw}.
\exitnote
Even when the creation of the temporary object is
unevaluated (Clause~\ref{expr}) or otherwise
avoided~(\ref{class.copy}),
all the semantic restrictions shall be respected as if the temporary object
had been created and later destroyed.
\enternote
even if
there is no call to the destructor or copy/move constructor,
all the semantic restrictions,
such as accessibility (Clause~\ref{class.access})
and whether the function is deleted~(\ref{dcl.fct.def.delete}), shall be satisfied.
However, in the special case of a function call used as the operand of a
\grammarterm{decltype-specifier}~(\ref{expr.call}), no temporary is introduced,
so the foregoing does not apply to the prvalue of any such function call.
\exitnote
\pnum
\enterexample Consider the following code:
\begin{codeblock}
class X {
public:
X(int);
X(const X&);
X& operator=(const X&);
~X();
};
class Y {
public:
Y(int);
Y(Y&&);
~Y();
};
X f(X);
Y g(Y);
void h() {
X a(1);
X b = f(X(2));
Y c = g(Y(3));
a = f(a);
}
\end{codeblock}
\indextext{class~object~copy|see{copy~constructor}}%
\indextext{constructor!copy}%
An implementation might use a temporary in which to construct
\tcode{X(2)}
before passing it to
\tcode{f()}
using
\tcode{X}'s
copy constructor; alternatively,
\tcode{X(2)}
might be constructed in the space used to hold the argument.
Likewise, an implementation might use a temporary in which to construct
\tcode{Y(3)} before passing it to \tcode{g()} using \tcode{Y}'s move constructor;
alternatively, \tcode{Y(3)} might be constructed in the space used to hold the argument.
Also, a temporary might be used to hold the result of
\tcode{f(X(2))}
before copying it to
\tcode{b}
using
\tcode{X}'s
copy constructor; alternatively,
\tcode{f()}'s
result might be constructed in
\tcode{b}.
Likewise, a temporary might be used to hold the result of \tcode{g(Y(3))} before
moving it to \tcode{c} using \tcode{Y}'s move constructor; alternatively, \tcode{g()}'s
result might be constructed in \tcode{c}.
On the other hand, the expression
\tcode{a=f(a)}
requires a temporary for
the result of \tcode{f(a)}, which is then assigned to \tcode{a}.
\exitexample
\pnum
\indextext{temporary!constructor~for}%
\indextext{temporary!destructor~for}%
\indextext{temporary!destruction~of}%
When an implementation introduces a temporary object of a class that has a
non-trivial constructor~(\ref{class.ctor}, \ref{class.copy}), it shall ensure that
a constructor is called for the temporary object.
Similarly, the destructor shall be called for a temporary with a non-trivial
destructor~(\ref{class.dtor}).
Temporary objects are destroyed as the last step
in evaluating
the full-expression~(\ref{intro.execution})
that (lexically) contains the point where
they were created.
This is true even if that evaluation ends in throwing an exception.
The
\indextext{value computation}%
value computations and
\indextext{side effects}%
side effects of destroying a temporary object
are associated only with the full-expression, not with any specific
subexpression.
\pnum
\indextext{initializer!temporary~and declarator}%
\indextext{temporary!order~of destruction~of}%
There are two contexts in which temporaries are destroyed at a different
point than the end of the full-expression.
The first context is when a default constructor is called to initialize an
element of an array. If the constructor has one or more default arguments,
the destruction of every temporary created in
a default argument is sequenced before the construction of the next array element, if any.
\pnum
The second context is when a reference is bound to a temporary.
The temporary to which the reference is bound or the temporary
that is the complete object of a subobject to which the reference is bound
persists for the lifetime of the reference except:
\begin{itemize}
\item A temporary bound to a reference member in a constructor's ctor-initializer~(\ref{class.base.init}) persists until the constructor exits.
\item A temporary bound to a reference parameter in a function call~(\ref{expr.call})
persists until the completion of the full-expression containing the call.
\item The lifetime of a temporary bound to the returned value in a function return statement~(\ref{stmt.return}) is not extended; the temporary is destroyed at the end of the full-expression in the return statement.
\item A temporary bound to a reference in a \grammarterm{new-initializer}~(\ref{expr.new}) persists until the completion of the full-expression containing the \grammarterm{new-initializer}. \enterexample
\begin{codeblock}
struct S { int mi; const std::pair<int,int>& mp; };
S a { 1, {2,3} };
S* p = new S{ 1, {2,3} }; // Creates dangling reference
\end{codeblock}
\exitexample \enternote This may introduce a dangling reference, and implementations are encouraged to issue a warning in such a case. \exitnote
\end{itemize}
The destruction of a temporary whose lifetime is not extended by being
bound to a reference is sequenced before the destruction of every
temporary which is constructed earlier in the same full-expression.
If the lifetime of two or more temporaries to which references are bound ends
at the same point,
these temporaries are destroyed at that point in the reverse order of the
completion of their construction.
In addition, the destruction of temporaries bound to references shall
take into account the ordering of destruction of objects with static, thread, or
automatic storage duration~(\ref{basic.stc.static}, \ref{basic.stc.thread}, \ref{basic.stc.auto});
that is, if
\tcode{obj1}
is an object with the same storage duration as the temporary and
created before the temporary is created
the temporary shall be destroyed before
\tcode{obj1}
is destroyed;
if
\tcode{obj2}
is an object with the same storage duration as the temporary and
created after the temporary is created
the temporary shall be destroyed after
\tcode{obj2}
is destroyed.
\enterexample
\begin{codeblock}
struct S {
S();
S(int);
friend S operator+(const S&, const S&);
~S();
};
S obj1;
const S& cr = S(16)+S(23);
S obj2;
\end{codeblock}
the expression
\tcode{S(16) + S(23)}
creates three temporaries:
a first temporary
\tcode{T1}
to hold the result of the expression
\tcode{S(16)},
a second temporary
\tcode{T2}
to hold the result of the expression
\tcode{S(23)},
and a third temporary
\tcode{T3}
to hold the result of the addition of these two expressions.
The temporary
\tcode{T3}
is then bound to the reference
\tcode{cr}.
It is unspecified whether
\tcode{T1}
or
\tcode{T2}
is created first.
On an implementation where
\tcode{T1}
is created before
\tcode{T2},
it is guaranteed that
\tcode{T2}
is destroyed before
\tcode{T1}.
The temporaries
\tcode{T1}
and
\tcode{T2}
are bound to the reference parameters of
\tcode{operator+};
these temporaries are destroyed at the end of the full-expression
containing the call to
\tcode{operator+}.
The temporary
\tcode{T3}
bound to the reference
\tcode{cr}
is destroyed at the end of
\tcode{cr}'s
lifetime, that is, at the end of the program.
In addition, the order in which
\tcode{T3}
is destroyed takes into account the destruction order of other objects with
static storage duration.
That is, because
\tcode{obj1}
is constructed before
\tcode{T3},
and
\tcode{T3}
is constructed before
\tcode{obj2},
it is guaranteed that
\tcode{obj2}
is destroyed before
\tcode{T3},
and that
\tcode{T3}
is destroyed before
\tcode{obj1}.
\exitexample
\rSec1[class.conv]{Conversions}
\pnum
\indextext{conversion!class}%
\indextext{conversion!user-defined}%
\indextext{constructor, conversion by|see{conversion, user-defined}}%
\indextext{conversion~function|see{conversion, user-defined}}%
\indextext{conversion!implicit}%
Type conversions of class objects can be specified by constructors and
by conversion functions.
These conversions are called
\term{user-defined conversions}
and are used for implicit type conversions (Clause~\ref{conv}),
for initialization~(\ref{dcl.init}),
and for explicit type conversions~(\ref{expr.cast}, \ref{expr.static.cast}).
\pnum
User-defined conversions are applied only where they are unambiguous~(\ref{class.member.lookup}, \ref{class.conv.fct}).
Conversions obey the access control rules (Clause~\ref{class.access}).
Access control is applied after ambiguity resolution~(\ref{basic.lookup}).
\pnum
\enternote
See~\ref{over.match} for a discussion of the use of conversions in function calls
as well as examples below.
\exitnote
\pnum
\indextext{conversion!implicit user-defined}%
At most one user-defined conversion (constructor or conversion function)
is implicitly applied to a single value.
\enterexample
\begin{codeblock}
struct X {
operator int();
};
struct Y {
operator X();
};
Y a;
int b = a; // error
// \tcode{a.operator X().operator int()} not tried
int c = X(a); // OK: \tcode{a.operator X().operator int()}
\end{codeblock}
\exitexample
\pnum
User-defined conversions are used implicitly only if they are unambiguous.
\indextext{name~hiding!user-defined conversion~and}%
A conversion function in a derived class does not hide a conversion function
in a base class unless the two functions convert to the same type.
Function overload resolution~(\ref{over.match.best}) selects the best
conversion function to perform the conversion.
\enterexample
\begin{codeblock}
struct X {
operator int();
};
struct Y : X {
operator char();
};
void f(Y& a) {
if (a) { // ill-formed:
// \tcode{X::operator int()} or \tcode{Y::operator char()}
}
}
\end{codeblock}
\exitexample
\rSec2[class.conv.ctor]{Conversion by constructor}%
\indextext{conversion!user-defined}
\pnum
A constructor declared without the
\grammarterm{function-specifier}
\tcode{explicit}
specifies a conversion from
the types of its parameters
to the type of its class.
Such a constructor is called a
\indexdefn{constructor!converting}%
\term{converting constructor}.
\enterexample
\indextext{Jessie}%
\begin{codeblock}
struct X {
X(int);
X(const char*, int =0);
};
void f(X arg) {
X a = 1; // \tcode{a = X(1)}
X b = "Jessie"; // \tcode{b = X("Jessie",0)}
a = 2; // \tcode{a = X(2)}
f(3); // \tcode{f(X(3))}
}
\end{codeblock}
\exitexample
\pnum
An explicit constructor constructs objects just like non-explicit
constructors, but does so only where the direct-initialization syntax~(\ref{dcl.init}) or where casts~(\ref{expr.static.cast}, \ref{expr.cast}) are explicitly
used.
A default constructor may be an explicit constructor; such a constructor
will be used to perform default-initialization
or value-initialization~(\ref{dcl.init}).
\enterexample
\begin{codeblock}
struct Z {
explicit Z();
explicit Z(int);
};
Z a; // OK: default-initialization performed
Z a1 = 1; // error: no implicit conversion
Z a3 = Z(1); // OK: direct initialization syntax used
Z a2(1); // OK: direct initialization syntax used
Z* p = new Z(1); // OK: direct initialization syntax used
Z a4 = (Z)1; // OK: explicit cast used
Z a5 = static_cast<Z>(1); // OK: explicit cast used
\end{codeblock}
\exitexample
\pnum
A
non-explicit
copy/move constructor~(\ref{class.copy}) is a converting constructor.
An implicitly-declared copy/move constructor is not an explicit constructor;
it may be called for implicit type conversions.
\rSec2[class.conv.fct]{Conversion functions}%
\indextext{function!conversion}%
\indextext{fundamental~type~conversion|see{conversion, user-defined}}%
\indextext{conversion!user-defined}%
\indextext{conversion operator|see{conversion, user defined}}
\pnum
A member function of a class \tcode{X} having no parameters with a name of the form
\begin{bnf}
\nontermdef{conversion-function-id}\br
\terminal{operator} conversion-type-id
\end{bnf}
\begin{bnf}
\nontermdef{conversion-type-id}\br
type-specifier-seq conversion-declarator\opt
\end{bnf}
\begin{bnf}
\nontermdef{conversion-declarator}\br
ptr-operator conversion-declarator\opt
\end{bnf}
specifies a conversion from
\tcode{X}
to the type specified by the
\grammarterm{conversion-type-id}.
Such functions are called conversion functions.
No return type can be specified.
\indextext{conversion!type~of}%
If a conversion function is a member function, the type of the conversion function~(\ref{dcl.fct}) is
``function taking no parameter returning
\grammarterm{conversion-type-id}''.
A conversion function is never used to convert a (possibly cv-qualified) object
to the (possibly cv-qualified) same object type (or a reference to it),
to a (possibly cv-qualified) base class of that type (or a reference to it),
or to (possibly cv-qualified) void.\footnote{These conversions are considered
as standard conversions for the purposes of overload resolution~(\ref{over.best.ics}, \ref{over.ics.ref}) and therefore initialization~(\ref{dcl.init}) and explicit casts~(\ref{expr.static.cast}). A conversion to \tcode{void} does not invoke any conversion function~(\ref{expr.static.cast}).
Even though never directly called to perform a conversion,
such conversion functions can be declared and can potentially
be reached through a call to a virtual conversion function in a base class.}
\enterexample
\begin{codeblock}
struct X {
operator int();
};
void f(X a) {
int i = int(a);
i = (int)a;
i = a;
}
\end{codeblock}
In all three cases the value assigned will be converted by
\tcode{X::operator int()}.
\exitexample
\pnum
A conversion function may be explicit~(\ref{dcl.fct.spec}), in which case it is only considered as a user-defined conversion for direct-initialization~(\ref{dcl.init}). Otherwise, user-defined conversions are not restricted to use in assignments and initializations.
\enterexample
\begin{codeblock}
class Y { };
struct Z {
explicit operator Y() const;
};
void h(Z z) {
Y y1(z); // OK: direct-initialization
Y y2 = z; // ill-formed: copy-initialization
Y y3 = (Y)z; // OK: cast notation
}
void g(X a, X b) {
int i = (a) ? 1+a : 0;
int j = (a&&b) ? a+b : i;
if (a) {
}
}
\end{codeblock}
\exitexample
\pnum
The
\grammarterm{conversion-type-id}
shall not represent a function type nor an array type.
The
\grammarterm{conversion-type-id}
in a
\grammarterm{conversion-function-id}
is the longest possible sequence of
\grammarterm{conversion-declarator}{s}.
\enternote
This prevents ambiguities between the declarator operator * and its expression
counterparts.
\enterexample
\begin{codeblock}
&ac.operator int*i; // syntax error:
// parsed as: \tcode{\&(ac.operator int *)i}
// not as: \tcode{\&(ac.operator int)*i}
\end{codeblock}
The \tcode{*} is the pointer declarator and not the multiplication operator.
\exitexample
\exitnote
\pnum
\indextext{conversion!inheritance~of user-defined}%
Conversion functions are inherited.
\pnum
\indextext{conversion!virtual user-defined}%
Conversion functions can be virtual.
\pnum
\indextext{conversion!static user-defined}%
Conversion functions cannot be declared
\tcode{static}.
\rSec1[class.dtor]{Destructors}%
\indextext{destructor}
\pnum
A special declarator syntax using an optional
\grammarterm{function-specifier}~(\ref{dcl.fct.spec}) followed by
\tcode{\~{}}
followed by the destructor's class name
followed by an empty parameter list
is used to declare the destructor in a class definition.
In such a declaration, the
\tcode{\~{}}
followed by the destructor's class name can be enclosed in optional parentheses;
such parentheses are ignored.
A
\grammarterm{typedef-name}
shall not be used as the
\grammarterm{class-name}
following the
\tcode{$\sim$}
in the declarator for a destructor declaration.
\pnum
A destructor is used to destroy objects of its class type.
\indextext{restriction!destructor}%
A destructor takes no parameters, and no return type can be
specified for it (not even
\tcode{void}).
The address of a destructor shall not be taken.
A destructor shall not be
\tcode{static}.
\indextext{\idxcode{const}!destructor~and}%
\indextext{\idxcode{volatile}!destructor~and}%
A destructor can be invoked for a
\tcode{const},
\tcode{volatile}
or
\tcode{const}
\tcode{volatile}
object.
A destructor shall not be declared
\tcode{const},
\tcode{volatile}
or
\tcode{const}
\tcode{volatile}~(\ref{class.this}).
\tcode{const}
and
\tcode{volatile}
semantics~(\ref{dcl.type.cv}) are not applied on an object under destruction.
They stop being in effect when the destructor for the
most derived object~(\ref{intro.object}) starts.
A destructor shall not be declared with a \grammarterm{ref-qualifier}.
\pnum
A declaration of a destructor that does not have an \grammarterm{exception-specification}
is implicitly considered to have the same \grammarterm{exception-specification} as an
implicit declaration~(\ref{except.spec}).
\pnum
\indextext{generated~destructor|see{destructor, default}}%
\indextext{destructor!default}%
\indextext{destructor!non-trivial}%
If a class has no user-declared
destructor, a destructor is implicitly
declared as defaulted~(\ref{dcl.fct.def}).
An implicitly-declared destructor is an
\tcode{inline}
\tcode{public}
member of its class.
\pnum
A defaulted destructor for a class
\tcode{X} is defined as deleted if:
\begin{itemize}
\item \tcode{X} is a union-like class that has a variant
member with a non-trivial destructor,
\item any of the non-static data members has class type
\tcode{M} (or array thereof) and
\tcode{M} has a deleted destructor or a destructor
that is inaccessible from the defaulted destructor,
\item any direct or virtual base class has a deleted
destructor or a destructor that is inaccessible from the
defaulted destructor,
\item or, for a virtual destructor, lookup of the non-array deallocation
function results in an ambiguity or in a function that is deleted or
inaccessible from the defaulted destructor.
\end{itemize}
A destructor is trivial if it is not user-provided and if:
\begin{itemize}
\item the destructor is not \tcode{virtual},
\item all of the direct base classes of its class have trivial destructors, and
\item for all of the non-static data members of its class that are of class
type (or array thereof), each such class has a trivial destructor.
\end{itemize}
Otherwise, the destructor is
\grammarterm{non-trivial}.
\pnum
\indextext{destructor!implicitly defined}%
A destructor
that is defaulted and not defined as deleted
is
\term{implicitly defined}
when it is odr-used~(\ref{basic.def.odr}) to destroy an object of its class type~(\ref{basic.stc})
or when it is explicitly defaulted after its first declaration.
\pnum
Before the
defaulted destructor for a class is implicitly defined, all the non-user-provided
destructors for its base classes and its non-static data members shall have been
implicitly defined.
\pnum
\indextext{order~of~execution!destructor}%
\indextext{order~of~execution!base~class destructor}%
\indextext{order~of~execution!member destructor}%
After executing the body of the destructor and destroying
any automatic objects allocated within the body, a
destructor for class
\tcode{X}
calls the destructors for