1 // PR c++/37962
2 // Negative test for auto
3 // { dg-do compile { target c++11 } }
4 
5 #include <typeinfo>
6 #include <stdarg.h>
7 #include <stddef.h>
8 
9 int i = *(auto *) 0;				// { dg-error "auto" }
10 struct A *p = (auto *) 0;			// { dg-error "auto" }
11 int *q = static_cast <auto *>(0);		// { dg-error "auto" }
12 const int *r = const_cast <auto *>(q);		// { dg-error "auto" }
13 const std::type_info &t1 = typeid (auto);	// { dg-error "auto" }
14 const std::type_info &t2 = typeid (auto *);	// { dg-error "auto" }
15 
16 struct A
17 {
18   operator auto ();				// { dg-error "auto" "" { target { ! c++14 } } }
19   operator auto *();				// { dg-error "auto" "" { target { ! c++14 } } }
20 };
21 
22 struct A2
23 {
24   operator auto () -> int;			// { dg-error "invalid use of|trailing return type" }
25   operator auto*() -> int;			// { dg-error "invalid use of|trailing return type" }
26 };
27 
28 template <typename> struct B
29 {
30   enum { e };
31 };
32 
33 template <typename T> struct C
34 {
CC35   C () : i () {}
36   int i;
37 };
38 
39 bool d = (auto (A::*)()) 0;			// { dg-error "auto" }
40 
41 void
foo()42 foo ()
43 {
44   __extension__ (auto) { 0 };			// { dg-error "auto" }
45   C<int> c;
46   dynamic_cast<auto> (c);			// { dg-error "auto" }
47   reinterpret_cast<auto> (c);			// { dg-error "auto" }
48   int i = auto (0);				// { dg-error "auto" }
49   auto p1 = new (auto);				// { dg-error "auto" }
50   auto p2 = new (auto) (42);			// { dg-error "invalid use of|deduce" }
51   offsetof (auto, fld);				// { dg-error "auto" }
52   offsetof (auto *, fld);			// { dg-error "auto" }
53   sizeof (auto);				// { dg-error "auto" }
54   sizeof (auto *);				// { dg-error "auto" }
55 }
56 
57 void
foo2(void)58 foo2 (void)
59 {
60   __alignof__ (auto);				// { dg-error "auto" }
61   __alignof__ (auto *);				// { dg-error "auto" }
62   __typeof__ (auto) v1;				// { dg-error "auto" }
63   __typeof__ (auto *) v2;			// { dg-error "auto" }
64   __is_class (auto);				// { dg-error "auto|expected" }
65   __is_pod (auto *);				// { dg-error "auto|expected" }
66   __is_base_of (int, auto);			// { dg-error "auto|expected" }
67   __is_base_of (auto, int);			// { dg-error "auto|expected" }
68   __is_base_of (auto, auto *);			// { dg-error "auto|expected" }
69 }
70 
71 B<auto> b;					// { dg-error "auto|invalid" }
72 C<auto> c;					// { dg-error "auto|invalid" }
73 C<auto *> c2;					// { dg-error "auto|invalid" }
74 
75 enum : auto { EE = 0 };				// { dg-error "must be an integral type" }
76 enum struct D : auto * { FF = 0 };		// { dg-error "must be an integral type|declar|expected" }
77 
78 void
bar()79 bar ()
80 {
81   try { } catch (auto i) { }			// { dg-error "parameter" }
82   try { } catch (auto) { }			// { dg-error "parameter" }
83   try { } catch (auto *i) { }			// { dg-error "parameter" }
84   try { } catch (auto *) { }			// { dg-error "parameter" }
85 }
86 
87 void
baz(int i,...)88 baz (int i, ...)
89 {
90   va_list ap;
91   va_start (ap, i);
92   va_arg (ap, auto);				// { dg-error "invalid use of" }
93   va_arg (ap, auto *);				// { dg-error "invalid use of|expected" }
94   va_arg (ap, auto &);				// { dg-error "invalid use of|expected" }
95   va_end (ap);
96 }
97 
98 template <typename T = auto> struct E {};	// { dg-error "invalid use of" }
99 template <class T = auto *> struct F {};	// { dg-error "invalid use of|expected" }
100 
101 auto fnlate () -> auto;				// { dg-error "invalid use of" "" { target { ! c++14 } } }
102 auto fnlate2 () -> auto *;			// { dg-error "invalid use of|expected" "" { target { ! c++14 } } }
103 
104 void
badthrow()105 badthrow () throw (auto)			// { dg-error "invalid use of" }
106 {						// { dg-error "dynamic exception specification" "" { target c++17 } .-1 }
107 }						// { dg-warning "deprecated" "" { target { ! c++17 } } .-2 }
108 
109 void
badthrow2()110 badthrow2 () throw (auto &)			// { dg-error "invalid use of|expected" }
111 {						// { dg-error "dynamic exception specification" "" { target c++17 } .-1 }
112 }						// { dg-warning "deprecated" "" { target { ! c++17 } } .-2 }
113 
114 template <auto V = 4> struct G {};		// { dg-error "auto" "" { target { ! c++17 } } }
115 
116 template <typename T> struct H { H (); ~H (); };
117 H<auto> h;					// { dg-error "invalid|initializer" }
118 
119 void qq (auto);		       // { dg-error "auto" "" { target { ! concepts } } }
120 void qr (auto*);	       // { dg-error "auto" "" { target { ! concepts } } }
121 
122 // PR c++/46145
123 typedef auto autot;		// { dg-error "auto" }
124