-
Notifications
You must be signed in to change notification settings - Fork 160
/
const_cast.cpp
75 lines (61 loc) · 1.93 KB
/
const_cast.cpp
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
/*
# const_cast
Change (add/remove) constantness and volatility of objects (property called cv-qualification).
Does not compile to any extra CPU instructions:
only changes type information for the compiler.
TODO when should it be used?
- http://stackoverflow.com/questions/357600/is-const-cast-safe/357640#357640
- avoiding repetition between const and non const versions of arguments is an use case:
- http://stackoverflow.com/questions/123758/how-do-i-remove-code-duplication-between-similar-const-and-non-const-member-func
- http://stackoverflow.com/questions/5324256/is-const-castconst-type-ever-useful/5326238#comment69750469_5326238
*/
#include "common.hpp"
int main() {
// Remove const.
{
// UB if data is const.
{
const int i = 0;
const int *ip = &i;
// ERROR.
//*ip = 1;
// UB
// http://stackoverflow.com/questions/19208560/can-we-use-const-cast-to-modify-a-constant-variable
//*(const_cast<int*>(ip)) = 1;
}
// Fine otherwise.
{
int i = 0;
const int *ip = &i;
// ERROR.
//*ip = 1;
// OK.
*(const_cast<int*>(ip)) = 1;
assert(i == 1);
}
}
// Only works for references or pointers.
{
int i;
// ERROR
//const_cast<const int>(i);
const_cast<const int&>(i);
const_cast<const int*>(&i);
}
// ERROR: Returns rvalues. Therefore cannot initialize non-const references.
{
int i = 0;
int *ip;
//const int*& ipa = const_cast<const int*>(ip);
//ipa is a non const reference.
//int *const& would be a const reference.
}
// Only has effect for a single statement.
{
int i = 0;
const int *ip = &i;
const_cast<int*>(ip);
// ERROR.
//*ip = 1;
}
}