1 // RUN: %clang_cc1 -std=c++11 -verify %s
2 
3 typedef int (*fp)(int);
4 int surrogate(int);
5 
6 struct X {
7   X() = default;  // expected-note{{candidate constructor not viable: requires 0 arguments, but 1 was provided}}
8   X(const X&) = default;  // expected-note{{candidate constructor not viable: no known conversion from 'bool' to 'const X' for 1st argument}}
9   X(bool b) __attribute__((enable_if(b, "chosen when 'b' is true")));  // expected-note{{candidate disabled: chosen when 'b' is true}}
10 
11   void f(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));
12   void f(int n) __attribute__((enable_if(n == 1, "chosen when 'n' is one")));  // expected-note{{member declaration nearly matches}} expected-note{{candidate disabled: chosen when 'n' is one}}
13 
14   static void s(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));  // expected-note2{{candidate disabled: chosen when 'n' is zero}}
15 
16   void conflict(int n) __attribute__((enable_if(n+n == 10, "chosen when 'n' is five")));  // expected-note{{candidate function}}
17   void conflict(int n) __attribute__((enable_if(n*2 == 10, "chosen when 'n' is five")));  // expected-note{{candidate function}}
18 
19   operator long() __attribute__((enable_if(true, "chosen on your platform")));
20   operator int() __attribute__((enable_if(false, "chosen on other platform")));
21 
operator fpX22   operator fp() __attribute__((enable_if(false, "never enabled"))) { return surrogate; }  // expected-note{{conversion candidate of type 'int (*)(int)'}}  // FIXME: the message is not displayed
23 };
24 
f(int n)25 void X::f(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")))  // expected-note{{member declaration nearly matches}} expected-note{{candidate disabled: chosen when 'n' is zero}}
26 {
27 }
28 
f(int n)29 void X::f(int n) __attribute__((enable_if(n == 2, "chosen when 'n' is two")))  // expected-error{{out-of-line definition of 'f' does not match any declaration in 'X'}} expected-note{{candidate disabled: chosen when 'n' is two}}
30 {
31 }
32 
33 X x1(true);
34 X x2(false); // expected-error{{no matching constructor for initialization of 'X'}}
35 
old()36 __attribute__((deprecated)) constexpr int old() { return 0; }  // expected-note2{{'old' has been explicitly marked deprecated here}}
37 void deprec1(int i) __attribute__((enable_if(old() == 0, "chosen when old() is zero")));  // expected-warning{{'old' is deprecated}}
38 void deprec2(int i) __attribute__((enable_if(old() == 0, "chosen when old() is zero")));  // expected-warning{{'old' is deprecated}}
39 
40 void overloaded(int);
41 void overloaded(long);
42 
43 struct Nothing { };
44 template<typename T> void typedep(T t) __attribute__((enable_if(t, "")));  // expected-note{{candidate disabled:}}  expected-error{{value of type 'Nothing' is not contextually convertible to 'bool'}}
45 template<int N> void valuedep() __attribute__((enable_if(N == 1, "")));
46 
47 // FIXME: we skip potential constant expression evaluation on value dependent
48 // enable-if expressions
49 int not_constexpr();
50 template<int N> void valuedep() __attribute__((enable_if(N == not_constexpr(), "")));
51 
52 template <typename T> void instantiationdep() __attribute__((enable_if(sizeof(sizeof(T)) != 0, "")));
53 
test()54 void test() {
55   X x;
56   x.f(0);
57   x.f(1);
58   x.f(2);  // no error, suppressed by erroneous out-of-line definition
59   x.f(3);  // expected-error{{no matching member function for call to 'f'}}
60 
61   x.s(0);
62   x.s(1);  // expected-error{{no matching member function for call to 's'}}
63 
64   X::s(0);
65   X::s(1);  // expected-error{{no matching member function for call to 's'}}
66 
67   x.conflict(5);  // expected-error{{call to member function 'conflict' is ambiguous}}
68 
69   deprec2(0);
70 
71   overloaded(x);
72 
73   int i = x(1);  // expected-error{{no matching function for call to object of type 'X'}}
74 
75   Nothing n;
76   typedep(0);  // expected-error{{no matching function for call to 'typedep'}}
77   typedep(1);
78   typedep(n);  // expected-note{{in instantiation of function template specialization 'typedep<Nothing>' requested here}}
79 }
80 
81 template <typename T> class C {
f()82   void f() __attribute__((enable_if(T::expr == 0, ""))) {}
g()83   void g() { f(); }
84 };
85 
86 int fn3(bool b) __attribute__((enable_if(b, "")));
test3()87 template <class T> void test3() {
88   fn3(sizeof(T) == 1);
89 }
90 
91 // FIXME: issue an error (without instantiation) because ::h(T()) is not
92 // convertible to bool, because return types aren't overloadable.
93 void h(int);
outer()94 template <typename T> void outer() {
95   void local_function() __attribute__((enable_if(::h(T()), "")));
96   local_function();
97 };
98 
99 namespace PR20988 {
100   struct Integer {
101     Integer(int);
102   };
103 
104   int fn1(const Integer &) __attribute__((enable_if(true, "")));
test1()105   template <class T> void test1() {
106     int &expr = T::expr();
107     fn1(expr);
108   }
109 
110   int fn2(const Integer &) __attribute__((enable_if(false, "")));  // expected-note{{candidate disabled}}
test2()111   template <class T> void test2() {
112     int &expr = T::expr();
113     fn2(expr);  // expected-error{{no matching function for call to 'fn2'}}
114   }
115 
116   int fn3(bool b) __attribute__((enable_if(b, "")));
test3()117   template <class T> void test3() {
118     fn3(sizeof(T) == 1);
119   }
120 }
121