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