test, trial, experiments
修订版 | 72ab932b7505d9087087d8b9fbed62414705bbfb (tree) |
---|---|
时间 | 2017-03-22 12:52:59 |
作者 | Yamashta Daisuke <yamasdais@gmai...> |
Commiter | Yamashta Daisuke |
added apply<T...>
improved is_cons<T> check
@@ -69,6 +69,15 @@ struct any<cons<A, D>> { | ||
69 | 69 | using value_type = cons<A, D>; |
70 | 70 | }; |
71 | 71 | |
72 | +template <typename T> | |
73 | +std::false_type check_cons(...); | |
74 | + | |
75 | +template <typename T> | |
76 | +auto check_cons(T*) -> decltype( | |
77 | + std::is_same<typename T::car_type, typename T::cdr_type>::value, | |
78 | + std::true_type() | |
79 | +); | |
80 | + | |
72 | 81 | } // detail |
73 | 82 | |
74 | 83 | using nil = detail::nil_type; |
@@ -99,11 +108,16 @@ struct is_nil : public std::false_type { }; | ||
99 | 108 | template <> |
100 | 109 | struct is_nil<nil> : public std::true_type { }; |
101 | 110 | |
111 | +#if 0 | |
102 | 112 | template <typename T> |
103 | 113 | struct is_cons : public std::false_type { }; |
104 | 114 | |
105 | 115 | template <typename T0, typename T1> |
106 | 116 | struct is_cons<cons<T0, T1>> : std::true_type { }; |
117 | +#endif | |
118 | +template <typename T> | |
119 | +struct is_cons : public decltype(detail::check_cons<T>(nullptr)) { | |
120 | +}; | |
107 | 121 | |
108 | 122 | template <typename T> |
109 | 123 | struct is_atom : public std::true_type { }; |
@@ -169,6 +183,12 @@ struct list { | ||
169 | 183 | using apply = typename detail::list_elem<T...>::apply; |
170 | 184 | }; |
171 | 185 | |
186 | +template <template <class...> typename F, | |
187 | + typename... Rest> | |
188 | +struct apply { | |
189 | + using type = typename F<Rest...>::apply; | |
190 | +}; | |
191 | + | |
172 | 192 | template <typename Cond, typename IfTrue, typename IfFalse = nil> |
173 | 193 | struct if_t { |
174 | 194 | using apply = typename std::conditional< |
@@ -180,8 +200,10 @@ struct if_t { | ||
180 | 200 | |
181 | 201 | void test_list() { |
182 | 202 | using namespace fmp; |
183 | - using l_0 = list<>::apply; // (list) => nil | |
184 | - using l_int = list<int>::apply; // (list 'int) => (INT) | |
203 | + //using l_0 = list<>::apply; // (list) => nil | |
204 | + using l_0 = apply<list>::type; | |
205 | + //using l_int = list<int>::apply; // (list 'int) => (INT) | |
206 | + using l_int = apply<list, int>::type; | |
185 | 207 | using l_2 = list<int, char>::apply; |
186 | 208 | using l_3 = list<int, char, float>::apply; |
187 | 209 | using l_4 = list<int, char, float, double>::apply; |
@@ -241,6 +263,7 @@ void test0() { | ||
241 | 263 | |
242 | 264 | static_assert(eq<int1::value_type, int>::value, "int1 == int"); |
243 | 265 | static_assert(int1::value == 1, "val<int, 1> == 1"); |
266 | + static_assert(!is_cons<nil>::value, "is_cons<nil> != true"); | |
244 | 267 | static_assert(!fmp::is_cons<int>::value, "is_cons<int> != true"); |
245 | 268 | static_assert(fmp::is_cons<cint>::value, "is_consp<cint> == true"); |
246 | 269 | static_assert(fmp::is_nil<fmp::cons<fmp::nil>::car::value_type>::value, |