p3.cpp revision 8ad6c8696a23f410398fc126929b107404c59a95
1// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 2// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++98 -Wno-c++11-extensions -Wc++11-compat 3void f() { 4 auto a = a; // expected-error{{variable 'a' declared with 'auto' type cannot appear in its own initializer}} 5 auto *b = b; // expected-error{{variable 'b' declared with 'auto' type cannot appear in its own initializer}} 6 const auto c = c; // expected-error{{variable 'c' declared with 'auto' type cannot appear in its own initializer}} 7 if (auto d = d) {} // expected-error {{variable 'd' declared with 'auto' type cannot appear in its own initializer}} 8 auto e = ({ auto f = e; 0; }); // expected-error {{variable 'e' declared with 'auto' type cannot appear in its own initializer}} 9} 10 11void g() { 12 auto a; // expected-error{{declaration of variable 'a' with type 'auto' requires an initializer}} 13 14 auto *b; // expected-error{{declaration of variable 'b' with type 'auto *' requires an initializer}} 15 16 if (auto b) {} // expected-error {{must have an initializer}} 17 for (;auto b;) {} // expected-error {{must have an initializer}} 18 while (auto b) {} // expected-error {{must have an initializer}} 19 if (auto b = true) { (void)b; } 20} 21 22auto n(1,2,3); // expected-error{{initializer for variable 'n' with type 'auto' contains multiple expressions}} 23 24namespace N 25{ 26 auto a = "const char [16]", *p = &a; 27} 28 29void h() { 30 auto b = 42ULL; 31 32 for (auto c = 0; c < b; ++c) { 33 } 34} 35 36template<typename T, typename U> struct same; 37template<typename T> struct same<T, T> {}; 38 39void p3example() { 40 auto x = 5; 41 const auto *v = &x, u = 6; 42 static auto y = 0.0; 43 // In C++98: 'auto' storage class specifier is redundant and incompatible with C++0x 44 // In C++0x: 'auto' storage class specifier is not permitted in C++0x, and will not be supported in future releases 45 auto int r; // expected-warning {{'auto' storage class specifier}} 46 47 same<__typeof(x), int> xHasTypeInt; 48 same<__typeof(v), const int*> vHasTypeConstIntPtr; 49 same<__typeof(u), const int> uHasTypeConstInt; 50 same<__typeof(y), double> yHasTypeDouble; 51} 52 53#if __cplusplus >= 201103L 54namespace PR13293 { 55 // Ensure that dependent declarators have their deduction delayed. 56 int f(char); 57 double f(short); 58 template<typename T> struct S { 59 static constexpr auto (*p)(T) = &f; 60 }; 61 62 constexpr int (*f1)(char) = &f; 63 constexpr double (*f2)(short) = &f; 64 static_assert(S<char>::p == f1, ""); 65 static_assert(S<short>::p == f2, ""); 66 67 struct K { int n; }; 68 template<typename T> struct U { 69 static constexpr auto (T::*p) = &K::n; 70 }; 71 static_assert(U<K>::p == &K::n, ""); 72 73 template<typename T> 74 using X = auto(int) -> auto(*)(T) -> auto(*)(char) -> long; 75 X<double> x; 76 template<typename T> struct V { 77 //static constexpr auto (*p)(int) -> auto(*)(T) -> auto(*)(char) = &x; // ill-formed 78 static constexpr auto (*(*(*p)(int))(T))(char) = &x; // ok 79 }; 80 V<double> v; 81 82 int *g(double); 83 template<typename T> void h() { 84 new (auto(*)(T)) (&g); 85 } 86 template void h<double>(); 87} 88#endif 89