forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathub.tex
More file actions
2004 lines (1662 loc) · 46.2 KB
/
Copy pathub.tex
File metadata and controls
2004 lines (1662 loc) · 46.2 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
\infannex{ub}{Core undefined behavior}
\rSec1[ub.general]{General}
\pnum
This Annex documents
undefined behavior explicitly called out in
\ref{intro} through \ref{\lastcorechapter}
using the following phrases:
\begin{itemize}
\item is undefined
\item the behavior is undefined
\item the behavior of the program is undefined
\item has undefined behavior
\item have undefined behavior
\item result has undefined behavior
\item results in undefined behavior
\end{itemize}
Undefined behavior that is implicit is not covered by this annex.
Each entry contains
a title,
a cross-reference,
a summary of the circumstances,
and code examples.
The code examples are not intended
to exhaustively cover all possible ways of invoking that case.
\rSec1[ub.basic]{\ref*{basic}: Basics}
\ubdescription{intro.object.implicit.create}
\pnum
For each operation that is specified as implicitly creating objects,
that operation implicitly creates and starts the lifetime
of zero or more objects of implicit-lifetime types\iref{basic.types}
in its specified region of storage
if doing so would result in the program having defined behavior.
If no such set of objects would give the program defined behavior,
the behavior of the program is undefined.
\pnum
\begin{example}
\begin{codeblock}
void f()
{
void *p = malloc(sizeof(int) + sizeof(float));
*reinterpret_cast<int*>(p) = 0;
*reinterpret_cast<float*>(p) = 0.0f; // undefined behavior, cannot create
// both \tcode{int} and \tcode{float} in same place
}
\end{codeblock}
\end{example}
\ubdescription{intro.object.implicit.pointer}
\pnum
After implicitly creating objects within a specified region of storage,
some operations are described as producing a pointer to a
suitable created object\iref{basic.types}.
These operations select one of the implicitly-created objects
whose address is the address of the start of the region of storage,
and produce a pointer value that points to that object,
if that value would result in the program having defined behavior.
If no such pointer value would give the program defined behavior,
the behavior of the program is undefined.
\pnum
\begin{example}
\begin{codeblock}
#include <cstdlib>
struct X {
int a, b;
~X() = delete; // deleted destructor makes \tcode{X} a non-implicit-lifetime class
};
X* make_x() {
X* p = (X*)std::malloc(sizeof(struct X)); // Cannot implicitly create an object because \tcode{X}
// is not an implicit-lifetime class.
p->a = 1; // undefined behavior, no set of objects give us defined behavior
return p;
}
\end{codeblock}
\end{example}
\begin{example}
\begin{codeblock}
#include <cstdlib>
struct X {
int a;
};
struct Y {
int x;
};
Y* make_y() {
X* p1 = (X*)std::malloc(sizeof(struct X)); // object \#1
p1->a = 1;
Y* p2 = (Y*)p1;
p2->x = 2; // undefined behavior, \tcode{p2} points to an in-lifetime object of type \tcode{X},
// which is not similar to \tcode{Y}\iref{expr.ref}
return p2;
}
\end{codeblock}
\end{example}
\ubdescription{basic.align.object.alignment}
\pnum
All instances of a type must be created in storage
that meets the alignment requirement of that type.
\pnum
\begin{example}
\begin{codeblock}
struct alignas(4) S {};
void make_misaligned()
{
alignas(S) char s[sizeof(S) + 1];
new (&s+1) S(); // undefined behavior, \tcode{\&s+1} will yield a pointer to
// a \tcode{char} which is $1$ byte away from an address with
// an alignment of $4$ and so cannot have an alignment of $4$
}
\end{codeblock}
\end{example}
\ubdescription{lifetime.outside.pointer.delete}
\pnum
For a pointer pointing to an object outside of its lifetime,
behavior is undefined if the object will be or was of a class type
with a non-trivial destructor
and the pointer is used as the operand of a \grammarterm{delete-expression}.
\pnum
\begin{example}
\begin{codeblock}
struct S {
float f = 0;
~S() {}
};
void f() {
S* p = new S;
p->~S();
delete p; // undefined behavior, operand of \tcode{delete}, lifetime has ended and \tcode{S}
// has a non-trivial destructor
}
\end{codeblock}
\end{example}
\ubdescription{lifetime.outside.pointer.member}
\pnum
For a pointer pointing to an object outside of its lifetime, behavior is
undefined if the pointer is used to access a non-static data member or call a
non-static member function of the object.
\pnum
\begin{example}
\begin{codeblock}
struct S {
float f = 0;
};
float f() {
S s;
S* p = &s;
s.~S();
return p->f; // undefined behavior, accessing non-static data member after end of lifetime
}
\end{codeblock}
\end{example}
\ubdescription{lifetime.outside.pointer.virtual}
\pnum
For a pointer pointing to an object outside of its lifetime, behavior is
undefined if the pointer is implicitly converted\iref{conv.ptr} to a pointer
to a virtual base class (or base class of a virtual base class).
\pnum
\begin{example}
\begin{codeblock}
struct B {};
struct D : virtual B {};
void f() {
D d;
D* p = &d;
d.~D();
B* b = p; // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{lifetime.outside.pointer.dynamic.cast}
\pnum
For a pointer pointing to an object outside of its lifetime, behavior is
undefined if the pointer is used as the operand of a
\tcode{dynamic_cast}\iref{expr.dynamic.cast}.
\pnum
\begin{example}
\begin{codeblock}
struct B { virtual ~B() = default; };
struct D : B {};
void f()
{
D d;
B* bp = &d;
d.~D();
D* dp = dynamic_cast<D*>(bp); // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{lifetime.outside.glvalue.access}
\pnum
Behavior is undefined if a glvalue referring to an object
outside of its lifetime is used to access the object.
\pnum
\begin{example}
\begin{codeblock}
void f() {
int x = int{10};
using T = int;
x.~T();
int y = x; // undefined behavior, glvalue used to access the
// object after the lifetime has ended
}
\end{codeblock}
\end{example}
\ubdescription{lifetime.outside.glvalue.member}
\pnum
Behavior is undefined if a glvalue referring to an object
outside of its lifetime is used to call
a non-static member function of the object.
\pnum
\begin{example}
\begin{codeblock}
struct A {
void f() {}
};
void f() {
A a;
a.~A();
a.f(); // undefined behavior, glvalue used to access a non-static member
// function after the lifetime has ended
}
\end{codeblock}
\end{example}
\ubdescription{lifetime.outside.glvalue.virtual}
\pnum
Behavior is undefined if a glvalue referring to an object
outside of its lifetime is bound to a reference to a virtual base class.
\pnum
\begin{example}
\begin{codeblock}
struct B {};
struct D : virtual B {
};
void f() {
D d;
d.~D();
B& b = d; // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{lifetime.outside.glvalue.dynamic.cast}
\pnum
Behavior is undefined if a glvalue referring to an object outside of its
lifetime is used as the operand of a
\keyword{dynamic_cast} or as the operand of \keyword{typeid}.
\pnum
\begin{example}
\begin{codeblock}
struct B { virtual ~B(); };
struct D : virtual B {};
void f() {
D d;
B& br = d;
d.~D();
D& dr = dynamic_cast<D&>(br); // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{original.type.implicit.destructor}
\pnum
The behavior is undefined if
a non-trivial implicit destructor call
occurs when the type of the object inhabiting the associated storage
is not the original type associated with that storage.
\pnum
\begin{example}
\begin{codeblock}
class T {};
struct B {
~B();
};
void h() {
B b;
new (&b) T;
} // undefined behavior at block exit
\end{codeblock}
\end{example}
\ubdescription{creating.within.const.complete.obj}
\pnum
Creating a new object within the storage that a const complete object
with static, thread, or automatic storage duration occupies,
or within the storage that such a const object used to occupy
before its lifetime ended, results in undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
struct B {
B();
~B();
};
const B b;
void h() {
b.~B();
new (const_cast<B*>(&b)) const B; // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{basic.indet.value}
\pnum
When the result of an evaluation is an indeterminate value
(but not just an erroneous value) the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
void g() {
int x [[ indeterminate ]];
int y = x; // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{basic.stc.alloc.dealloc.constraint}
\pnum
If the behavior of an allocation or deallocation function
does not satisfy the semantic constraints specified
in~\ref{basic.stc.dynamic.allocation}
and~\ref{basic.stc.dynamic.deallocation},
the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
#include <new>
void* operator new(std::size_t sz) {
if (sz == 0)
return nullptr; // undefined behavior, should return non-null pointer
return std::malloc(1); // undefined behavior, if successful should return allocation with
// length in bytes is at least as large as the requested size
}
void operator delete(void* ptr) noexcept {
throw 0; // undefined behavior, terminates by throwing an exception
}
\end{codeblock}
\end{example}
\ubdescription{basic.stc.alloc.zero.dereference}
\pnum
The pointer returned when invoking an allocation function with a size of zero
cannot be dereferenced.
\pnum
\begin{example}
\begin{codeblock}
void test()
{
char* c = static_cast<char*>(operator new(0z));
c[0] = 'X'; // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{basic.compound.invalid.pointer}
\pnum
Indirection or the invocation of a deallocation function
with a pointer value referencing storage that has been freed
has undefined behavior.
(Most other uses of such a pointer have implementation-defined behavior.)
\pnum
\begin{example}
\begin{codeblock}
void f()
{
int *x = new int{5};
delete x;
int y = *x; // undefined behavior
delete x; // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{intro.execution.unsequenced.modification}
\pnum
If a side effect on a memory location\iref{intro.memory}
is unsequenced relative to either another side effect
on the same memory location
or a value computation using the value of any object
in the same memory location,
and they are not potentially concurrent\iref{intro.multithread},
the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
void g(int i) {
i = 7, i++, i++; // \tcode{i} becomes 9
i = i++ + 1; // the value of \tcode{i} is incremented
i = i++ + i; // undefined behavior
i = i + 1; // the value of \tcode{i} is incremented
}
\end{codeblock}
\end{example}
\ubdescription{intro.races.data}
\pnum
The execution of a program contains a data race
if it contains two potentially concurrent conflicting actions,
at least one of which is not atomic,
and neither happens before the other,
except for the special case for signal handlers
described in~\ref{intro.races}.
Any such data race results in undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
int count = 0;
auto f = [&] { count++; };
std::thread t1{f}, t2{f}, t3{f};
// undefined behavior, \tcode{t1}, \tcode{t2} and \tcode{t3} have a data race on access of variable \tcode{count}
\end{codeblock}
\end{example}
\ubdescription{intro.progress.stops}
\pnum
The behavior is undefined if a thread of execution
that has not terminated stops making execution steps.
\pnum
\begin{example}
\begin{codeblock}
bool stop() { return false; }
void busy_wait_thread() {
while (!stop()); // undefined behavior, thread makes no progress but the loop
} // is not trivial because \tcode{stop()} is not a constant expression
int main() {
std::thread t(busy_wait_thread);
t.join();
}
\end{codeblock}
\end{example}
\ubdescription{basic.start.main.exit.during.destruction}
\pnum
If \tcode{std::exit} is called to
end a program during the destruction of an object with static or thread storage duration, the program has
undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
#include <cstdlib>
struct Exiter {
~Exiter() { std::exit(0); }
};
Exiter ex;
int main() {}
// undefined behavior when destructor of static variable \tcode{ex} is called it will call \tcode{std::exit}
\end{codeblock}
\end{example}
\ubdescription{basic.start.term.use.after.destruction}
\pnum
If a function contains a block-scope object of static
or thread storage duration that has been destroyed
and the function is called
during the destruction of an object with static or thread storage duration,
the program has undefined behavior if the flow of control
passes through the definition of the previously destroyed block-scope object.
Likewise, the behavior is undefined if the block-scope object
is used indirectly (i.e., through a pointer) after its destruction.
\pnum
\begin{example}
\begin{codeblock}
struct A {};
void f() {
static A a;
}
struct B {
B() { f(); }
};
struct C {
~C() { f(); }
};
C c;
B b; // call to \tcode{f()} in constructor begins lifetime of \tcode{a}
int main() {}
// undefined behavior, static objects are destructed in reverse order, in this case \tcode{a} then \tcode{b} and
// finally \tcode{c}. When the destructor of \tcode{c} is called, it calls \tcode{f()} which passes through the definition of
// previously destroyed block-scope object
\end{codeblock}
\end{example}
\rSec1[ub.expr]{\ref*{expr}: Expressions}
\ubdescription{expr.expr.eval}
\pnum
If during the evaluation of an expression,
the result is not mathematically defined
or not in the range of representable values for its type,
the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
#include <limits>
int main() {
// Assuming $32$-bit \tcode{int}, the range of values is: $-2,147,483,648$ to $2,147,483,647$.
int x1 = std::numeric_limits<int>::max() + 1;
// undefined behavior, $2,147,483,647 + 1$ is not representable as an \tcode{int}
int x2 = std::numeric_limits<int>::min() / -1;
// undefined behavior, $-2,147,483,648 / -1$ is not representable as an \tcode{int}
}
\end{codeblock}
\end{example}
\ubdescription{expr.basic.lvalue.strict.aliasing.violation}
\pnum
If a program attempts to access\iref{defns.access} the stored value of an object
whose dynamic type is $T$ through a glvalue
whose type is not similar\iref{conv.qual} to $T$
(or its corresponding signed or unsigned types)
the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
int foo(float* f, int* i) {
*i = 1;
*f = 0.f; // undefined behavior, glvalue is not similar to \tcode{int}
return *i;
}
int main() {
int x = 0;
x = foo(reinterpret_cast<float*>(&x), &x);
}
\end{codeblock}
\end{example}
\ubdescription{expr.basic.lvalue.union.initialization}
\pnum
If a program invokes a defaulted copy/move constructor or
defaulted copy/move assignment operator of a union
with an argument that is not an object of a similar type
within its lifetime, the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
union U { int x; };
void f()
{
char u[sizeof(U)];
U o = reinterpret_cast<U&>(u); // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{expr.type.reference.lifetime}
\pnum
Evaluating a reference when an equivalent use of a pointer denoting the same object
would be invalid has undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
void g()
{
int* ip = new int(5);
int& i = *ip;
delete ip;
i; // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{conv.lval.valid.representation}
\pnum
Performing an
lvalue-to-rvalue conversion
on an object whose
value representation
is not valid for its type
has undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
bool f() {
bool b = true;
char c = 42;
memcpy(&b, &c, 1);
return b; // undefined behavior if \tcode{42} is not a valid value representation for \keyword{bool}
}
\end{codeblock}
\end{example}
\ubdescription{conv.double.out.of.range}
\pnum
Converting a floating-point value to a type
that cannot represent the value has undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
#include <limits>
int main() {
// Assuming 32-bit int, 32-bit float and 64-bit double.
double d2 = std::numeric_limits<double>::max();
float f = d2; // undefined behavior on systems where the range of representable values
// of \tcode{float} is \tcode{[-max,+max]}; on systems where the range of representable
// values is \tcode{[-inf,+inf]} this would not be undefined behavior
int i = d2; // undefined behavior, the max value of \tcode{double} is not representable as \tcode{int}
}
\end{codeblock}
\end{example}
\ubdescription{conv.fpint.float.not.represented}
\pnum
When converting a floating-point value to an integer type, if
the value is not representable in the destination type, the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
#include <limits>
int main() {
// Assuming $32$-bit \tcode{int}, the range of values is: $-2,147,483,648$ to
// $2,147,483,647$. Assuming $32$-bit \tcode{float} and $64$-bit \tcode{double}.
double d = (double)std::numeric_limits<int>::max() + 1;
int x1 = d; // undefined behavior, $2,147,483,647 + 1$ is not representable as \tcode{int}
}
\end{codeblock}
\end{example}
\ubdescription{conv.fpint.int.not.represented}
\pnum
When converting a value of integer or unscoped enumeration type
to a floating-point type,
if the value is not representable in the destination type
the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
int main() {
unsigned long long x2 = -1;
float f = x2; // undefined behavior on systems where \tcode{f} does not include
// a representation for infinity and the maximum value for \tcode{float}
// is smaller than the maximum value for \tcode{unsigned long long}
}
\end{codeblock}
\end{example}
\ubdescription{conv.ptr.virtual.base}
\pnum
Converting
a pointer to a derived class \tcode{D}
to
a pointer to a virtual base class \tcode{B}
that does not point to
a valid object
within its lifetime
has undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
struct B {};
struct D : virtual B {};
void f()
{
D ds[1];
B* b = &ds[1]; // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{conv.member.missing.member}
\pnum
The conversion of
a pointer to a member of a base class
to a pointer to member of a derived class
that could not contain that member
has undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
struct B {};
struct D1 : B { int d1; };
struct D2 : B {};
void f()
{
int (D1::*pd1) = &D1::d1;
int (B::*pb) = static_cast<int (B::*)>(pd1);
int (D2::*pd2) = pb; // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{expr.call.different.type}
\pnum
Calling a function through an expression whose
function type is not call-compatible with
the function type of the called
function's definition results in undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
using f_float = int (*)(float);
using f_int = int (*)(int);
int f1(float) { return 10; }
int f2(int) { return 20; }
int main() {
return reinterpret_cast<f_int>(f1)(20); // undefined behavior, the function type of the expression
// is different from the called function's definition
}
\end{codeblock}
\end{example}
\ubdescription{expr.ref.member.not.similar}
\pnum
In a class member access \tcode{E1.E2},
where \tcode{E2} is a non-static member,
the behavior is undefined
if \tcode{E1} refers to an object
whose type is not similar
to the type of the expression \tcode{E1}.
\pnum
\begin{example}
\begin{codeblock}
struct A { int i; };
struct B { int j; };
A a;
int x = reinterpret_cast<B&>(a).j; // undefined behavior
\end{codeblock}
\end{example}
\ubdescription{expr.dynamic.cast.pointer.lifetime}
\pnum
Evaluating a \keyword{dynamic_cast} on a non-null pointer
that points to an object (of polymorphic type) of the wrong type
or to an object not within its lifetime
has undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
struct B { virtual ~B(); };
void f() {
B bs[1];
B* dp = dynamic_cast<B*>(bs+1); // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{expr.dynamic.cast.glvalue.lifetime}
\pnum
Evaluating a \keyword{dynamic_cast} on a reference
that denotes an object (of polymorphic type)
of a type that is not similar to the referenced type
or an object not within its lifetime
has undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
struct B { virtual ~B(); };
void f() {
B bs[1];
B& dr = dynamic_cast<B&>(bs[1]); // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{expr.static.cast.base.class}
\pnum
A glvalue of type \tcode{B}
can be cast to the type ``reference to \tcode{D}''
if \tcode{B} is a base class of \tcode{D},
otherwise the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
struct B {};
struct D1 : B {};
struct D2 : B {};
void f() {
D1 d;
B &b = d;
static_cast<D2 &>(b); // undefined behavior, base class object of type \tcode{D1} not \tcode{D2}
}
\end{codeblock}
\end{example}
\ubdescription{expr.static.cast.enum.outside.range}
\pnum
If the enumeration type does not have a fixed underlying type,
the value is unchanged if the original value
is within the range of the enumeration values\iref{dcl.enum},
and otherwise, the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
enum A { e1 = 1, e2 };
void f() {
enum A a = static_cast<A>(4); // undefined behavior, $4$ is not within the range of enumeration values
}
\end{codeblock}
\end{example}
\ubdescription{expr.static.cast.fp.outside.range}
\pnum
An explicit conversion of a floating-point value
that is outside the range of the target type
has undefined behavior.
\pnum
\begin{example}
If \tcode{float} does not adhere to \IsoFloatUndated{}
and cannot represent positive infinity,
a sufficiently large \tcode{double} value will be
outside the (finite) range of \tcode{float}.
\begin{codeblock}
void f() {
double d = FLT_MAX;
d *= 16;
float f = static_cast<float>(d); // undefined behavior
}
\end{codeblock}
\end{example}
\ubdescription{expr.static.cast.downcast.wrong.derived.type}
\pnum
Casting from a pointer to a base class to a pointer to a derived class
when there is no enclosing object of that derived class at the
specified location has undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
struct B {};
struct D1 : B {};
struct D2 : B {};
void f() {
B *bp = new D1;
static_cast<D2 *>(bp); // undefined behavior, base class object of type \tcode{D1} not \tcode{D2}
}
\end{codeblock}
\end{example}
\ubdescription{expr.static.cast.does.not.contain.original.member}
\pnum
A
pointer to member of derived class \tcode{D}
can be cast to
a pointer to member of base class \tcode{B}
(with certain restrictions on \cv{} qualifiers)
as long as \tcode{B} contains the original member,
or is a base or derived class of the class
containing the original member;
otherwise the behavior is undefined.
\pnum
\begin{example}
\begin{codeblock}
struct A {
char c;
};
struct B {
int i;
};
struct D : A, B {};
int main() {
char D::*p1 = &D::c;
char B::*p2 = static_cast<char B::*>(p1); // undefined behavior, \tcode{B} does not contain the
// original member \tcode{c}
}
\end{codeblock}
\end{example}
\ubdescription{expr.unary.dereference}
\pnum
Dereferencing a pointer that does not point to an object or function
has undefined behavior.
\pnum
\begin{example}
\begin{codeblock}
int f()
{
int *p = nullptr;
return *p; // undefined behavior
}