| 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 |
class A { |
| 19 |
public: |
| 20 |
int a; |
| 21 |
int& b; |
| 22 |
static int c; |
| 23 |
|
| 24 |
A(int& b) : b(b) { } |
| 25 |
|
| 26 |
void foo() { |
| 27 |
CHECK_DECLTYPE(decltype(a), int); |
| 28 |
CHECK_DECLTYPE(decltype(this->a), int); |
| 29 |
CHECK_DECLTYPE(decltype((*this).a), int); |
| 30 |
CHECK_DECLTYPE(decltype(b), int&); |
| 31 |
CHECK_DECLTYPE(decltype(c), int); |
| 32 |
} |
| 33 |
void bar() const { |
| 34 |
CHECK_DECLTYPE(decltype(a), int); |
| 35 |
CHECK_DECLTYPE(decltype(b), int&); |
| 36 |
CHECK_DECLTYPE(decltype(c), int); |
| 37 |
} |
| 38 |
}; |
| 39 |
|
| 40 |
int b; |
| 41 |
A aa(b); |
| 42 |
const A& caa = aa; |
| 43 |
CHECK_DECLTYPE(decltype(aa.a), int); |
| 44 |
CHECK_DECLTYPE(decltype(aa.b), int&); |
| 45 |
CHECK_DECLTYPE(decltype(caa.a), int); |
| 46 |
|
| 47 |
class B { |
| 48 |
public: |
| 49 |
int a; |
| 50 |
enum B_enum { b }; |
| 51 |
decltype(a) c; |
| 52 |
decltype(a) foo() { return 0; } |
| 53 |
decltype(b) enums_are_in_scope() { return b; } // ok |
| 54 |
}; |
| 55 |
|
| 56 |
CHECK_DECLTYPE(decltype(aa.*&A::a), int&); |
| 57 |
decltype(aa.*&A::b) zz; // { dg-error "18:cannot create pointer to reference member" "cannot" } |
| 58 |
|
| 59 |
CHECK_DECLTYPE(decltype(caa.*&A::a), const int&); |
| 60 |
|
| 61 |
class X { |
| 62 |
void foo() { |
| 63 |
CHECK_DECLTYPE(decltype(this), X*); |
| 64 |
CHECK_DECLTYPE(decltype(*this), X&); |
| 65 |
} |
| 66 |
void bar() const { |
| 67 |
CHECK_DECLTYPE(decltype(this), const X*); |
| 68 |
CHECK_DECLTYPE(decltype(*this), const X&); |
| 69 |
} |
| 70 |
}; |
| 71 |
|