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