Merged trunk at revision 161680 into branch.
[official-gcc.git] / gcc / testsuite / g++.dg / cpp0x / decltype4.C
blob32fbc2b2ec20fa22087e4b01d73f6b73a3e0755c
1 // { dg-do "compile" }
2 // { dg-options "-std=gnu++0x" }
4 template<typename T, typename U> 
5 struct is_same 
7   static const bool value = false;
8 };
10 template<typename T>
11 struct is_same<T, T>
13   static const bool value = true;
16 #define CHECK_DECLTYPE(DECLTYPE,RESULT) \
17   static_assert(is_same< DECLTYPE , RESULT >::value, #DECLTYPE " should be " #RESULT)
19 struct A {
20   int x; 
21   int& y; 
22   int foo(char); 
23   int& bar() const; 
24 }; 
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);
31 CHECK_DECLTYPE(decltype("decltype"), const char(&)[9]);
32 CHECK_DECLTYPE(decltype(1), int);
34 int an_int = 5;
35 int& i = an_int; 
36 const int j = an_int; 
38 CHECK_DECLTYPE(decltype(i)&, int&);
39 CHECK_DECLTYPE(const decltype(j), const int);
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&);
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&);
60   CHECK_DECLTYPE(decltype (++i), int&); 
61   CHECK_DECLTYPE(decltype (i++), int);
64 struct B {
65   B () : bit(), cbit() {} 
66   int bit : 2;
67   const int cbit : 3;
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   }
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&);