p6.cpp revision 32509f1e60451d86e9fbc473b6e853ba10b5fd1e
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
3
4template<typename T>
5struct only {
6  only(T);
7  template<typename U> only(U) = delete;
8};
9
10namespace N
11{
12  auto a = "const char [16]", *p = &a;
13
14  only<const char [16]> testA = a;
15  only<const char **> testP = p;
16}
17
18void h() {
19  auto b = 42ULL;
20  only<unsigned long long> testB = b;
21
22  for (auto c = 0; c < 100; ++c) {
23    only<int> testC = c;
24  }
25}
26
27void p3example() {
28  auto x = 5;
29  const auto *v = &x, u = 6;
30  static auto y = 0.0;
31
32  only<int> testX = x;
33  only<const int*> testV = v;
34  only<const int> testU = u;
35  only<double> testY = y;
36}
37
38void f() {
39  if (auto a = true) {
40    only<bool> testA = a;
41  }
42
43  switch (auto a = 0) {
44  case 0:
45    only<int> testA = a;
46  }
47
48  while (auto a = false) {
49    only<bool> testA = a;
50  }
51
52  for (; auto a = "test"; ) {
53    only<const char[5]> testA = a;
54  }
55
56  auto *fail1 = 0; // expected-error {{variable 'fail1' with type 'auto *' has incompatible initializer of type 'int'}}
57  int **p;
58  const auto **fail2(p); // expected-error {{variable 'fail2' with type 'auto const **' has incompatible initializer of type 'int **'}}
59}
60
61struct S {
62  void f();
63  char g(int);
64  float g(double);
65  int m;
66
67  void test() {
68    auto p1 = &S::f;
69    auto S::*p2 = &S::f;
70    auto (S::*p3)() = &S::f;
71    auto p4 = &S::g; // expected-error {{incompatible initializer of type '<overloaded function type>'}}
72    auto S::*p5 = &S::g; // expected-error {{incompatible initializer of type '<overloaded function type>'}}
73    auto (S::*p6)(int) = &S::g;
74    auto p7 = &S::m;
75    auto S::*p8 = &S::m;
76
77    only<void (S::*)()> test1 = p1;
78    only<void (S::*)()> test2 = p2;
79    only<void (S::*)()> test3 = p3;
80    only<char (S::*)(int)> test6 = p6;
81    only<int (S::*)> test7 = p7;
82    only<int (S::*)> test8 = p8;
83  }
84};
85
86namespace PR10939 {
87  struct X {
88    int method(int);
89    int method(float);
90  };
91
92  template<typename T> T g(T);
93
94  void f(X *x) {
95    // FIXME: we should really only get the first diagnostic here.
96    auto value = x->method; // expected-error {{reference to non-static member function must be called}} expected-error{{variable 'value' with type 'auto' has incompatible initializer of type '<bound member function type>'}}
97    if (value) { }
98
99    auto funcptr = &g<int>;
100    int (*funcptr2)(int) = funcptr;
101  }
102}
103
104// TODO: if the initializer is a braced-init-list, deduce auto as std::initializer_list<T>.
105