| 1 |
// { dg-options "-Wuseless-cast" } |
| 2 |
|
| 3 |
template<typename T> |
| 4 |
void tmpl_f1(T& t) |
| 5 |
{ |
| 6 |
(int)(t); |
| 7 |
static_cast<int>(t); |
| 8 |
reinterpret_cast<int>(t); |
| 9 |
|
| 10 |
(int*)(&t); |
| 11 |
const_cast<int*>(&t); |
| 12 |
static_cast<int*>(&t); |
| 13 |
reinterpret_cast<int*>(&t); |
| 14 |
|
| 15 |
(int&)(t); |
| 16 |
const_cast<int&>(t); |
| 17 |
static_cast<int&>(t); |
| 18 |
reinterpret_cast<int&>(t); |
| 19 |
} |
| 20 |
|
| 21 |
template<typename T> |
| 22 |
void tmpl_f2(T t) |
| 23 |
{ |
| 24 |
(int&)(t); |
| 25 |
const_cast<int&>(t); |
| 26 |
static_cast<int&>(t); |
| 27 |
reinterpret_cast<int&>(t); |
| 28 |
} |
| 29 |
|
| 30 |
struct A { }; |
| 31 |
|
| 32 |
template<typename T> |
| 33 |
void tmpl_f3(T& t) |
| 34 |
{ |
| 35 |
(A)(t); |
| 36 |
static_cast<A>(t); |
| 37 |
|
| 38 |
(A*)(&t); |
| 39 |
const_cast<A*>(&t); |
| 40 |
static_cast<A*>(&t); |
| 41 |
reinterpret_cast<A*>(&t); |
| 42 |
dynamic_cast<A*>(&t); |
| 43 |
|
| 44 |
(A&)(t); |
| 45 |
const_cast<A&>(t); |
| 46 |
static_cast<A&>(t); |
| 47 |
reinterpret_cast<A&>(t); |
| 48 |
dynamic_cast<A&>(t); |
| 49 |
} |
| 50 |
|
| 51 |
template<typename T> |
| 52 |
void tmpl_f4(T t) |
| 53 |
{ |
| 54 |
(A&)(t); |
| 55 |
const_cast<A&>(t); |
| 56 |
static_cast<A&>(t); |
| 57 |
reinterpret_cast<A&>(t); |
| 58 |
dynamic_cast<A&>(t); |
| 59 |
} |
| 60 |
|
| 61 |
A prvalue(); |
| 62 |
|
| 63 |
void f() |
| 64 |
{ |
| 65 |
int n; |
| 66 |
|
| 67 |
(int)(n); |
| 68 |
static_cast<int>(n); |
| 69 |
reinterpret_cast<int>(n); |
| 70 |
|
| 71 |
(int*)(&n); // { dg-warning "3:useless cast" } |
| 72 |
const_cast<int*>(&n); // { dg-warning "3:useless cast" } |
| 73 |
static_cast<int*>(&n); // { dg-warning "3:useless cast" } |
| 74 |
reinterpret_cast<int*>(&n); // { dg-warning "3:useless cast" } |
| 75 |
|
| 76 |
int& m = n; |
| 77 |
|
| 78 |
(int&)(m); // { dg-warning "3:useless cast" } |
| 79 |
const_cast<int&>(m); // { dg-warning "3:useless cast" } |
| 80 |
static_cast<int&>(m); // { dg-warning "3:useless cast" } |
| 81 |
reinterpret_cast<int&>(m); // { dg-warning "3:useless cast" } |
| 82 |
|
| 83 |
tmpl_f1(m); |
| 84 |
|
| 85 |
(int&)(n); // { dg-warning "3:useless cast" } |
| 86 |
const_cast<int&>(n); // { dg-warning "3:useless cast" } |
| 87 |
static_cast<int&>(n); // { dg-warning "3:useless cast" } |
| 88 |
reinterpret_cast<int&>(n); // { dg-warning "3:useless cast" } |
| 89 |
|
| 90 |
tmpl_f2(n); |
| 91 |
|
| 92 |
#ifdef __GXX_EXPERIMENTAL_CXX0X__ |
| 93 |
(int&&)(42); |
| 94 |
static_cast<int&&>(42); |
| 95 |
|
| 96 |
(A&&)(prvalue()); |
| 97 |
const_cast<A&&>(prvalue()); |
| 98 |
static_cast<A&&>(prvalue()); |
| 99 |
#endif |
| 100 |
|
| 101 |
A a; |
| 102 |
|
| 103 |
(A)(a); |
| 104 |
static_cast<A>(a); |
| 105 |
|
| 106 |
(A*)(&a); // { dg-warning "3:useless cast" } |
| 107 |
const_cast<A*>(&a); // { dg-warning "3:useless cast" } |
| 108 |
static_cast<A*>(&a); // { dg-warning "3:useless cast" } |
| 109 |
reinterpret_cast<A*>(&a); // { dg-warning "3:useless cast" } |
| 110 |
dynamic_cast<A*>(&a); // { dg-warning "3:useless cast" } |
| 111 |
|
| 112 |
A& b = a; |
| 113 |
|
| 114 |
(A&)(b); // { dg-warning "3:useless cast" } |
| 115 |
const_cast<A&>(b); // { dg-warning "3:useless cast" } |
| 116 |
static_cast<A&>(b); // { dg-warning "3:useless cast" } |
| 117 |
static_cast<A&>(b); // { dg-warning "3:useless cast" } |
| 118 |
dynamic_cast<A&>(b); // { dg-warning "3:useless cast" } |
| 119 |
|
| 120 |
tmpl_f3(b); |
| 121 |
|
| 122 |
(A&)(a); // { dg-warning "3:useless cast" } |
| 123 |
const_cast<A&>(a); // { dg-warning "3:useless cast" } |
| 124 |
static_cast<A&>(a); // { dg-warning "3:useless cast" } |
| 125 |
reinterpret_cast<A&>(a); // { dg-warning "3:useless cast" } |
| 126 |
dynamic_cast<A&>(a); // { dg-warning "3:useless cast" } |
| 127 |
|
| 128 |
tmpl_f4(a); |
| 129 |
} |