| 1 |
// { dg-do compile { target c++11 } } |
| 2 |
|
| 3 |
template<typename T, typename U> |
| 4 |
struct is_same |
| 5 |
{ |
| 6 |
static const bool value = false; |
| 7 |
}; |
| 8 |
|
| 9 |
template<typename T> |
| 10 |
struct is_same<T, T> |
| 11 |
{ |
| 12 |
static const bool value = true; |
| 13 |
}; |
| 14 |
|
| 15 |
#define CHECK_DECLTYPE(DECLTYPE,RESULT) \ |
| 16 |
static_assert(is_same< DECLTYPE , RESULT >::value, #DECLTYPE " should be " #RESULT) |
| 17 |
|
| 18 |
struct A { |
| 19 |
int x; |
| 20 |
int& y; |
| 21 |
int foo(char); |
| 22 |
int& bar() const; |
| 23 |
}; |
| 24 |
|
| 25 |
CHECK_DECLTYPE(decltype(&A::x), int A::*); |
| 26 |
decltype(&A::y) Ay; // { dg-error "14:cannot create pointer to reference member|invalid type" } |
| 27 |
CHECK_DECLTYPE(decltype(&A::foo), int (A::*) (char)); |
| 28 |
CHECK_DECLTYPE(decltype(&A::bar), int& (A::*) () const); |
| 29 |
|
| 30 |
CHECK_DECLTYPE(decltype("decltype"), const char(&)[9]); |
| 31 |
CHECK_DECLTYPE(decltype(1), int); |
| 32 |
|
| 33 |
int an_int = 5; |
| 34 |
int& i = an_int; |
| 35 |
const int j = an_int; |
| 36 |
|
| 37 |
CHECK_DECLTYPE(decltype(i)&, int&); |
| 38 |
CHECK_DECLTYPE(const decltype(j), const int); |
| 39 |
|
| 40 |
int foo(); |
| 41 |
CHECK_DECLTYPE(decltype(foo()), int); |
| 42 |
float& bar(int); |
| 43 |
CHECK_DECLTYPE(decltype (bar(1)), float&); |
| 44 |
const A bar(); |
| 45 |
CHECK_DECLTYPE(decltype (bar()), const A); |
| 46 |
const A& bar2(); |
| 47 |
CHECK_DECLTYPE(decltype (bar2()), const A&); |
| 48 |
|
| 49 |
void wibble() { |
| 50 |
CHECK_DECLTYPE(decltype(1+2), int); |
| 51 |
int* p; |
| 52 |
CHECK_DECLTYPE(decltype(*p), int&); |
| 53 |
int a[10]; |
| 54 |
CHECK_DECLTYPE(decltype(a[3]), int&); |
| 55 |
int i; int& j = i; |
| 56 |
CHECK_DECLTYPE(decltype (i = 5), int&); |
| 57 |
CHECK_DECLTYPE(decltype (j = 5), int&); |
| 58 |
|
| 59 |
CHECK_DECLTYPE(decltype (++i), int&); |
| 60 |
CHECK_DECLTYPE(decltype (i++), int); |
| 61 |
} |
| 62 |
|
| 63 |
struct B { |
| 64 |
B () : bit(), cbit() {} |
| 65 |
int bit : 2; |
| 66 |
const int cbit : 3; |
| 67 |
|
| 68 |
void foo() |
| 69 |
{ |
| 70 |
CHECK_DECLTYPE(decltype(bit), int); |
| 71 |
CHECK_DECLTYPE(decltype((bit)), int&); |
| 72 |
CHECK_DECLTYPE(decltype(cbit), const int); |
| 73 |
CHECK_DECLTYPE(decltype((cbit)), const int&); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
B b; |
| 78 |
const B& bc = b; |
| 79 |
CHECK_DECLTYPE(decltype(b.bit), int); |
| 80 |
CHECK_DECLTYPE(decltype(bc.bit), int); |
| 81 |
CHECK_DECLTYPE(decltype((b.bit)), int&); |
| 82 |
CHECK_DECLTYPE(decltype((bc.bit)), const int&); |