1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // UNSUPPORTED: c++98, c++03, c++11 11 12 // TODO(ericwf) 13 // constexpr support temporarily reverted due to bug: 14 // https://llvm.org/bugs/show_bug.cgi?id=23141 15 // XFAIL: * 16 17 // <experimental/tuple> 18 19 // template <class F, class T> constexpr decltype(auto) apply(F &&, T &&) 20 21 // Testing constexpr evaluation 22 23 #include <experimental/tuple> 24 #include <utility> 25 #include <cassert> 26 27 constexpr int f_int_0() { return 1; } 28 constexpr int f_int_1(int x) { return x; } 29 constexpr int f_int_2(int x, int y) { return (x + y); } 30 31 struct A_int_0 32 { 33 constexpr A_int_0() {} 34 constexpr int operator()() const { return 1; } 35 }; 36 37 struct A_int_1 38 { 39 constexpr A_int_1() {} 40 constexpr int operator()(int x) const { return x; } 41 }; 42 43 struct A_int_2 44 { 45 constexpr A_int_2() {} 46 constexpr int operator()(int x, int y) const { return (x + y); } 47 }; 48 49 namespace ex = std::experimental; 50 51 template <class Tuple> 52 void test_0() 53 { 54 // function 55 { 56 constexpr Tuple t{}; 57 static_assert(1 == ex::apply(f_int_0, t), ""); 58 } 59 // function pointer 60 { 61 constexpr Tuple t{}; 62 constexpr auto fp = &f_int_0; 63 static_assert(1 == ex::apply(fp, t), ""); 64 } 65 // functor 66 { 67 constexpr Tuple t{}; 68 constexpr A_int_0 a; 69 static_assert(1 == ex::apply(a, t), ""); 70 } 71 } 72 73 template <class Tuple> 74 void test_1() 75 { 76 // function 77 { 78 constexpr Tuple t{1}; 79 static_assert(1 == ex::apply(f_int_1, t), ""); 80 } 81 // function pointer 82 { 83 constexpr Tuple t{2}; 84 constexpr int (*fp)(int) = f_int_1; 85 static_assert(2 == ex::apply(fp, t), ""); 86 } 87 // functor 88 { 89 constexpr Tuple t{3}; 90 constexpr A_int_1 fn; 91 static_assert(3 == ex::apply(fn, t), ""); 92 } 93 } 94 95 template <class Tuple> 96 void test_2() 97 { 98 // function 99 { 100 constexpr Tuple t{1, 2}; 101 static_assert(3 == ex::apply(f_int_2, t), ""); 102 } 103 // function pointer 104 { 105 constexpr Tuple t{2, 3}; 106 constexpr auto fp = &f_int_2; 107 static_assert(5 == ex::apply(fp, t), ""); 108 } 109 // functor 110 { 111 constexpr Tuple t{3, 4}; 112 constexpr A_int_2 a; 113 static_assert(7 == ex::apply(a, t), ""); 114 } 115 } 116 117 int main() 118 { 119 test_0<std::tuple<>>(); 120 test_1<std::tuple<int>>(); 121 test_2<std::tuple<int, int>>(); 122 test_2<std::pair<int, int>>(); 123 } 124