Debian Codegraph @

3rd_party/range-v3/meta/meta.hpp

raw
1 /// \file meta.hpp Tiny meta-programming library.
2 //
3 // Meta library
4 //
5 // Copyright Eric Niebler 2014-present
6 //
7 // Use, modification and distribution is subject to the
8 // Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
11 //
12 // Project home: https://github.com/ericniebler/meta
13 //
14
15 #ifndef META_HPP
16 #define META_HPP
17
18 #include <cstddef>
19 #include <initializer_list>
20 #include <meta/meta_fwd.hpp>
21 #include <type_traits>
22 #include <utility>
23
24 #ifdef __clang__
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
27 #pragma GCC diagnostic ignored "-Wpragmas"
28 #pragma GCC diagnostic ignored "-Wdocumentation-deprecated-sync"
29 #pragma GCC diagnostic ignored "-Wmissing-variable-declarations"
30 #endif
31
32 /// \defgroup meta Meta
33 ///
34 /// A tiny metaprogramming library
35
36 /// \defgroup trait Trait
37 /// Trait invocation/composition.
38 /// \ingroup meta
39
40 /// \defgroup invocation Invocation
41 /// Trait invocation
42 /// \ingroup trait
43
44 /// \defgroup composition Composition
45 /// Trait composition
46 /// \ingroup trait
47
48 /// \defgroup logical Logical
49 /// Logical operations
50 /// \ingroup meta
51
52 /// \defgroup algorithm Algorithms
53 /// Algorithms.
54 /// \ingroup meta
55
56 /// \defgroup query Query/Search
57 /// Query and search algorithms
58 /// \ingroup algorithm
59
60 /// \defgroup transformation Transformation
61 /// Transformation algorithms
62 /// \ingroup algorithm
63
64 /// \defgroup runtime Runtime
65 /// Runtime algorithms
66 /// \ingroup algorithm
67
68 /// \defgroup datatype Datatype
69 /// Datatypes.
70 /// \ingroup meta
71
72 /// \defgroup list list_like
73 /// \ingroup datatype
74
75 /// \defgroup integral Integer sequence
76 /// Equivalent to C++14's `std::integer_sequence`
77 /// \ingroup datatype
78
79 /// \defgroup extension Extension
80 /// Extend meta with your own datatypes.
81 /// \ingroup datatype
82
83 /// \defgroup math Math
84 /// Integral constant arithmetic.
85 /// \ingroup meta
86
87 /// \defgroup lazy_trait lazy
88 /// \ingroup trait
89
90 /// \defgroup lazy_invocation lazy
91 /// \ingroup invocation
92
93 /// \defgroup lazy_composition lazy
94 /// \ingroup composition
95
96 /// \defgroup lazy_logical lazy
97 /// \ingroup logical
98
99 /// \defgroup lazy_query lazy
100 /// \ingroup query
101
102 /// \defgroup lazy_transformation lazy
103 /// \ingroup transformation
104
105 /// \defgroup lazy_list lazy
106 /// \ingroup list
107
108 /// \defgroup lazy_datatype lazy
109 /// \ingroup datatype
110
111 /// \defgroup lazy_math lazy
112 /// \ingroup math
113
114 /// Tiny metaprogramming library
115 namespace meta
116 {
117 namespace detail
118 {
119 /// Returns a \p T nullptr
120 template <typename T>
121 constexpr T *_nullptr_v()
122 {
123 return nullptr;
124 }
125
126 #if META_CXX_VARIABLE_TEMPLATES
127 template <typename T>
128 META_INLINE_VAR constexpr T *nullptr_v = nullptr;
129 #endif
130 } // namespace detail
131
132 /// An empty type.
133 /// \ingroup datatype
134 struct nil_
135 {
136 };
137
138 /// Type alias for \p T::type.
139 /// \ingroup invocation
140 template <META_TYPE_CONSTRAINT(trait) T>
141 using _t = typename T::type;
142
143 #if META_CXX_VARIABLE_TEMPLATES || defined(META_DOXYGEN_INVOKED)
144 /// Variable alias for \c T::type::value
145 /// \note Requires C++14 or greater.
146 /// \ingroup invocation
147 template <META_TYPE_CONSTRAINT(integral) T>
148 constexpr typename T::type::value_type _v = T::type::value;
149 #endif
150
151 /// Lazy versions of meta actions
152 namespace lazy
153 {
154 /// \sa `meta::_t`
155 /// \ingroup lazy_invocation
156 template <typename T>
157 using _t = defer<_t, T>;
158 } // namespace lazy
159
160 /// An integral constant wrapper for \c std::size_t.
161 /// \ingroup integral
162 template <std::size_t N>
163 using size_t = std::integral_constant<std::size_t, N>;
164
165 /// An integral constant wrapper for \c bool.
166 /// \ingroup integral
167 template <bool B>
168 using bool_ = std::integral_constant<bool, B>;
169
170 /// An integral constant wrapper for \c int.
171 /// \ingroup integral
172 template <int I>
173 using int_ = std::integral_constant<int, I>;
174
175 /// An integral constant wrapper for \c char.
176 /// \ingroup integral
177 template <char Ch>
178 using char_ = std::integral_constant<char, Ch>;
179
180 ///////////////////////////////////////////////////////////////////////////////////////////
181 // Math operations
182 /// An integral constant wrapper around the result of incrementing the wrapped integer \c
183 /// T::type::value.
184 template <META_TYPE_CONSTRAINT(integral) T>
185 using inc = std::integral_constant<decltype(T::type::value + 1), T::type::value + 1>;
186
187 /// An integral constant wrapper around the result of decrementing the wrapped integer \c
188 /// T::type::value.
189 template <META_TYPE_CONSTRAINT(integral) T>
190 using dec = std::integral_constant<decltype(T::type::value - 1), T::type::value - 1>;
191
192 /// An integral constant wrapper around the result of adding the two wrapped integers
193 /// \c T::type::value and \c U::type::value.
194 /// \ingroup math
195 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
196 using plus = std::integral_constant<decltype(T::type::value + U::type::value),
197 T::type::value + U::type::value>;
198
199 /// An integral constant wrapper around the result of subtracting the two wrapped integers
200 /// \c T::type::value and \c U::type::value.
201 /// \ingroup math
202 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
203 using minus = std::integral_constant<decltype(T::type::value - U::type::value),
204 T::type::value - U::type::value>;
205
206 /// An integral constant wrapper around the result of multiplying the two wrapped integers
207 /// \c T::type::value and \c U::type::value.
208 /// \ingroup math
209 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
210 using multiplies = std::integral_constant<decltype(T::type::value * U::type::value),
211 T::type::value * U::type::value>;
212
213 /// An integral constant wrapper around the result of dividing the two wrapped integers \c
214 /// T::type::value and \c U::type::value.
215 /// \ingroup math
216 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
217 using divides = std::integral_constant<decltype(T::type::value / U::type::value),
218 T::type::value / U::type::value>;
219
220 /// An integral constant wrapper around the result of negating the wrapped integer
221 /// \c T::type::value.
222 /// \ingroup math
223 template <META_TYPE_CONSTRAINT(integral) T>
224 using negate = std::integral_constant<decltype(-T::type::value), -T::type::value>;
225
226 /// An integral constant wrapper around the remainder of dividing the two wrapped integers
227 /// \c T::type::value and \c U::type::value.
228 /// \ingroup math
229 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
230 using modulus = std::integral_constant<decltype(T::type::value % U::type::value),
231 T::type::value % U::type::value>;
232
233 /// A Boolean integral constant wrapper around the result of comparing \c T::type::value and
234 /// \c U::type::value for equality.
235 /// \ingroup math
236 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
237 using equal_to = bool_<T::type::value == U::type::value>;
238
239 /// A Boolean integral constant wrapper around the result of comparing \c T::type::value and
240 /// \c U::type::value for inequality.
241 /// \ingroup math
242 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
243 using not_equal_to = bool_<T::type::value != U::type::value>;
244
245 /// A Boolean integral constant wrapper around \c true if \c T::type::value is greater than
246 /// \c U::type::value; \c false, otherwise.
247 /// \ingroup math
248 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
249 using greater = bool_<(T::type::value > U::type::value)>;
250
251 /// A Boolean integral constant wrapper around \c true if \c T::type::value is less than \c
252 /// U::type::value; \c false, otherwise.
253 /// \ingroup math
254 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
255 using less = bool_<(T::type::value < U::type::value)>;
256
257 /// A Boolean integral constant wrapper around \c true if \c T::type::value is greater than
258 /// or equal to \c U::type::value; \c false, otherwise.
259 /// \ingroup math
260 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
261 using greater_equal = bool_<(T::type::value >= U::type::value)>;
262
263 /// A Boolean integral constant wrapper around \c true if \c T::type::value is less than or
264 /// equal to \c U::type::value; \c false, otherwise.
265 /// \ingroup math
266 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
267 using less_equal = bool_<(T::type::value <= U::type::value)>;
268
269 /// An integral constant wrapper around the result of bitwise-and'ing the two wrapped
270 /// integers \c T::type::value and \c U::type::value.
271 /// \ingroup math
272 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
273 using bit_and = std::integral_constant<decltype(T::type::value & U::type::value),
274 T::type::value & U::type::value>;
275
276 /// An integral constant wrapper around the result of bitwise-or'ing the two wrapped
277 /// integers \c T::type::value and \c U::type::value.
278 /// \ingroup math
279 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
280 using bit_or = std::integral_constant<decltype(T::type::value | U::type::value),
281 T::type::value | U::type::value>;
282
283 /// An integral constant wrapper around the result of bitwise-exclusive-or'ing the two
284 /// wrapped integers \c T::type::value and \c U::type::value.
285 /// \ingroup math
286 template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
287 using bit_xor = std::integral_constant<decltype(T::type::value ^ U::type::value),
288 T::type::value ^ U::type::value>;
289
290 /// An integral constant wrapper around the result of bitwise-complementing the wrapped
291 /// integer \c T::type::value.
292 /// \ingroup math
293 template <META_TYPE_CONSTRAINT(integral) T>
294 using bit_not = std::integral_constant<decltype(~T::type::value), ~T::type::value>;
295
296 namespace lazy
297 {
298 /// \sa 'meta::int'
299 /// \ingroup lazy_math
300 template <typename T>
301 using inc = defer<inc, T>;
302
303 /// \sa 'meta::dec'
304 /// \ingroup lazy_math
305 template <typename T>
306 using dec = defer<dec, T>;
307
308 /// \sa 'meta::plus'
309 /// \ingroup lazy_math
310 template <typename T, typename U>
311 using plus = defer<plus, T, U>;
312
313 /// \sa 'meta::minus'
314 /// \ingroup lazy_math
315 template <typename T, typename U>
316 using minus = defer<minus, T, U>;
317
318 /// \sa 'meta::multiplies'
319 /// \ingroup lazy_math
320 template <typename T, typename U>
321 using multiplies = defer<multiplies, T, U>;
322
323 /// \sa 'meta::divides'
324 /// \ingroup lazy_math
325 template <typename T, typename U>
326 using divides = defer<divides, T, U>;
327
328 /// \sa 'meta::negate'
329 /// \ingroup lazy_math
330 template <typename T>
331 using negate = defer<negate, T>;
332
333 /// \sa 'meta::modulus'
334 /// \ingroup lazy_math
335 template <typename T, typename U>
336 using modulus = defer<modulus, T, U>;
337
338 /// \sa 'meta::equal_to'
339 /// \ingroup lazy_math
340 template <typename T, typename U>
341 using equal_to = defer<equal_to, T, U>;
342
343 /// \sa 'meta::not_equal_t'
344 /// \ingroup lazy_math
345 template <typename T, typename U>
346 using not_equal_to = defer<not_equal_to, T, U>;
347
348 /// \sa 'meta::greater'
349 /// \ingroup lazy_math
350 template <typename T, typename U>
351 using greater = defer<greater, T, U>;
352
353 /// \sa 'meta::less'
354 /// \ingroup lazy_math
355 template <typename T, typename U>
356 using less = defer<less, T, U>;
357
358 /// \sa 'meta::greater_equal'
359 /// \ingroup lazy_math
360 template <typename T, typename U>
361 using greater_equal = defer<greater_equal, T, U>;
362
363 /// \sa 'meta::less_equal'
364 /// \ingroup lazy_math
365 template <typename T, typename U>
366 using less_equal = defer<less_equal, T, U>;
367
368 /// \sa 'meta::bit_and'
369 /// \ingroup lazy_math
370 template <typename T, typename U>
371 using bit_and = defer<bit_and, T, U>;
372
373 /// \sa 'meta::bit_or'
374 /// \ingroup lazy_math
375 template <typename T, typename U>
376 using bit_or = defer<bit_or, T, U>;
377
378 /// \sa 'meta::bit_xor'
379 /// \ingroup lazy_math
380 template <typename T, typename U>
381 using bit_xor = defer<bit_xor, T, U>;
382
383 /// \sa 'meta::bit_not'
384 /// \ingroup lazy_math
385 template <typename T>
386 using bit_not = defer<bit_not, T>;
387 } // namespace lazy
388
389 /// \cond
390 namespace detail
391 {
392 enum class indices_strategy_
393 {
394 done,
395 repeat,
396 recurse
397 };
398
399 constexpr indices_strategy_ strategy_(std::size_t cur, std::size_t end)
400 {
401 return cur >= end ? indices_strategy_::done
402 : cur * 2 <= end ? indices_strategy_::repeat
403 : indices_strategy_::recurse;
404 }
405
406 template <typename T>
407 constexpr std::size_t range_distance_(T begin, T end)
408 {
409 return begin <= end ? static_cast<std::size_t>(end - begin)
410 : throw "The start of the integer_sequence must not be "
411 "greater than the end";
412 }
413
414 template <std::size_t End, typename State, indices_strategy_ Status_>
415 struct make_indices_
416 {
417 using type = State;
418 };
419
420 template <typename T, T, typename>
421 struct coerce_indices_
422 {
423 };
424 } // namespace detail
425 /// \endcond
426
427 ///////////////////////////////////////////////////////////////////////////////////////////
428 // integer_sequence
429 #if !META_CXX_INTEGER_SEQUENCE
430 /// A container for a sequence of compile-time integer constants.
431 /// \ingroup integral
432 template <typename T, T... Is>
433 struct integer_sequence
434 {
435 using value_type = T;
436 /// \return `sizeof...(Is)`
437 static constexpr std::size_t size() noexcept { return sizeof...(Is); }
438 };
439 #endif
440
441 ///////////////////////////////////////////////////////////////////////////////////////////
442 // index_sequence
443 /// A container for a sequence of compile-time integer constants of type
444 /// \c std::size_t
445 /// \ingroup integral
446 template <std::size_t... Is>
447 using index_sequence = integer_sequence<std::size_t, Is...>;
448
449 #if META_HAS_MAKE_INTEGER_SEQ && !defined(META_DOXYGEN_INVOKED)
450 // Implement make_integer_sequence and make_index_sequence with the
451 // __make_integer_seq builtin on compilers that provide it. (Redirect
452 // through decltype to workaround suspected clang bug.)
453 /// \cond
454 namespace detail
455 {
456 template <typename T, T N>
457 __make_integer_seq<integer_sequence, T, N> make_integer_sequence_();
458 }
459 /// \endcond
460
461 template <typename T, T N>
462 using make_integer_sequence = decltype(detail::make_integer_sequence_<T, N>());
463
464 template <std::size_t N>
465 using make_index_sequence = make_integer_sequence<std::size_t, N>;
466 #else
467 /// Generate \c index_sequence containing integer constants [0,1,2,...,N-1].
468 /// \par Complexity
469 /// `O(log(N))`.
470 /// \ingroup integral
471 template <std::size_t N>
472 using make_index_sequence =
473 _t<detail::make_indices_<N, index_sequence<0>, detail::strategy_(1, N)>>;
474
475 /// Generate \c integer_sequence containing integer constants [0,1,2,...,N-1].
476 /// \par Complexity
477 /// `O(log(N))`.
478 /// \ingroup integral
479 template <typename T, T N>
480 using make_integer_sequence =
481 _t<detail::coerce_indices_<T, 0, make_index_sequence<static_cast<std::size_t>(N)>>>;
482 #endif
483
484 ///////////////////////////////////////////////////////////////////////////////////////////
485 // integer_range
486 /// Makes the integer sequence `[From, To)`.
487 /// \par Complexity
488 /// `O(log(To - From))`.
489 /// \ingroup integral
490 template <typename T, T From, T To>
491 using integer_range =
492 _t<detail::coerce_indices_<T, From,
493 make_index_sequence<detail::range_distance_(From, To)>>>;
494
495 /// \cond
496 namespace detail
497 {
498 template <typename, typename>
499 struct concat_indices_
500 {
501 };
502
503 template <std::size_t... Is, std::size_t... Js>
504 struct concat_indices_<index_sequence<Is...>, index_sequence<Js...>>
505 {
506 using type = index_sequence<Is..., (Js + sizeof...(Is))...>;
507 };
508
509 template <>
510 struct make_indices_<0u, index_sequence<0>, indices_strategy_::done>
511 {
512 using type = index_sequence<>;
513 };
514
515 template <std::size_t End, std::size_t... Values>
516 struct make_indices_<End, index_sequence<Values...>, indices_strategy_::repeat>
517 : make_indices_<End, index_sequence<Values..., (Values + sizeof...(Values))...>,
518 detail::strategy_(sizeof...(Values) * 2, End)>
519 {
520 };
521
522 template <std::size_t End, std::size_t... Values>
523 struct make_indices_<End, index_sequence<Values...>, indices_strategy_::recurse>
524 : concat_indices_<index_sequence<Values...>,
525 make_index_sequence<End - sizeof...(Values)>>
526 {
527 };
528
529 template <typename T, T Offset, std::size_t... Values>
530 struct coerce_indices_<T, Offset, index_sequence<Values...>>
531 {
532 using type =
533 integer_sequence<T, static_cast<T>(static_cast<T>(Values) + Offset)...>;
534 };
535 } // namespace detail
536 /// \endcond
537
538 /// Evaluate the invocable \p Fn with the arguments \p Args.
539 /// \ingroup invocation
540 template <META_TYPE_CONSTRAINT(invocable) Fn, typename... Args>
541 using invoke = typename Fn::template invoke<Args...>;
542
543 /// Lazy versions of meta actions
544 namespace lazy
545 {
546 /// \sa `meta::invoke`
547 /// \ingroup lazy_invocation
548 template <typename Fn, typename... Args>
549 using invoke = defer<invoke, Fn, Args...>;
550 } // namespace lazy
551
552 /// A trait that always returns its argument \p T. It is also an invocable
553 /// that always returns \p T.
554 /// \ingroup trait
555 /// \ingroup invocation
556 template <typename T>
557 struct id
558 {
559 #if defined(META_WORKAROUND_CWG_1558) && !defined(META_DOXYGEN_INVOKED)
560 // Redirect through decltype for compilers that have not
561 // yet implemented CWG 1558:
562 static id impl(void *);
563
564 template <typename... Ts>
565 using invoke = _t<decltype(id::impl(static_cast<list<Ts...> *>(nullptr)))>;
566 #else
567 template <typename...>
568 using invoke = T;
569 #endif
570
571 using type = T;
572 };
573
574 /// An alias for type \p T. Useful in non-deduced contexts.
575 /// \ingroup trait
576 template <typename T>
577 using id_t = _t<id<T>>;
578
579 namespace lazy
580 {
581 /// \sa `meta::id`
582 /// \ingroup lazy_trait
583 /// \ingroup lazy_invocation
584 template <typename T>
585 using id = defer<id, T>;
586 } // namespace lazy
587
588 /// An alias for `void`.
589 /// \ingroup trait
590 #if defined(META_WORKAROUND_CWG_1558) && !defined(META_DOXYGEN_INVOKED)
591 // Redirect through decltype for compilers that have not
592 // yet implemented CWG 1558:
593 template <typename... Ts>
594 using void_ = invoke<id<void>, Ts...>;
595 #else
596 template <typename...>
597 using void_ = void;
598 #endif
599
600 #if META_CXX_VARIABLE_TEMPLATES
601 #ifdef META_CONCEPT
602 /// `true` if `T::type` exists and names a type; `false` otherwise.
603 /// \ingroup trait
604 template <typename T>
605 META_INLINE_VAR constexpr bool is_trait_v = trait<T>;
606
607 /// `true` if `T::invoke` exists and names a class template; `false` otherwise.
608 /// \ingroup trait
609 template <typename T>
610 META_INLINE_VAR constexpr bool is_callable_v = invocable<T>;
611 #else // ^^^ Concepts / No concepts vvv
612 /// \cond
613 namespace detail
614 {
615 template <typename, typename = void>
616 META_INLINE_VAR constexpr bool is_trait_ = false;
617
618 template <typename T>
619 META_INLINE_VAR constexpr bool is_trait_<T, void_<typename T::type>> = true;
620
621 template <typename, typename = void>
622 META_INLINE_VAR constexpr bool is_callable_ = false;
623
624 template <typename T>
625 META_INLINE_VAR constexpr bool is_callable_<T, void_<quote<T::template invoke>>> = true;
626 } // namespace detail
627 /// \endcond
628
629 /// `true` if `T::type` exists and names a type; `false` otherwise.
630 /// \ingroup trait
631 template <typename T>
632 META_INLINE_VAR constexpr bool is_trait_v = detail::is_trait_<T>;
633
634 /// `true` if `T::invoke` exists and names a class template; `false` otherwise.
635 /// \ingroup trait
636 template <typename T>
637 META_INLINE_VAR constexpr bool is_callable_v = detail::is_callable_<T>;
638 #endif // Concepts vs. variable templates
639
640 /// An alias for `std::true_type` if `T::type` exists and names a type; otherwise, it's an
641 /// alias for `std::false_type`.
642 /// \ingroup trait
643 template <typename T>
644 using is_trait = bool_<is_trait_v<T>>;
645
646 /// An alias for `std::true_type` if `T::invoke` exists and names a class template;
647 /// otherwise, it's an alias for `std::false_type`.
648 /// \ingroup trait
649 template <typename T>
650 using is_callable = bool_<is_callable_v<T>>;
651 #else // ^^^ META_CXX_VARIABLE_TEMPLATES / !META_CXX_VARIABLE_TEMPLATES vvv
652 /// \cond
653 namespace detail
654 {
655 template <typename, typename = void>
656 struct is_trait_
657 {
658 using type = std::false_type;
659 };
660
661 template <typename T>
662 struct is_trait_<T, void_<typename T::type>>
663 {
664 using type = std::true_type;
665 };
666
667 template <typename, typename = void>
668 struct is_callable_
669 {
670 using type = std::false_type;
671 };
672
673 template <typename T>
674 struct is_callable_<T, void_<quote<T::template invoke>>>
675 {
676 using type = std::true_type;
677 };
678 } // namespace detail
679 /// \endcond
680
681 template <typename T>
682 using is_trait = _t<detail::is_trait_<T>>;
683
684 /// An alias for `std::true_type` if `T::invoke` exists and names a class
685 /// template or alias template; otherwise, it's an alias for
686 /// `std::false_type`.
687 /// \ingroup trait
688 template <typename T>
689 using is_callable = _t<detail::is_callable_<T>>;
690 #endif
691
692 /// \cond
693 namespace detail
694 {
695 #ifdef META_CONCEPT
696 template <template <typename...> class, typename...>
697 struct defer_
698 {
699 };
700
701 template <template <typename...> class C, typename... Ts>
702 requires valid<C, Ts...> struct defer_<C, Ts...>
703 {
704 using type = C<Ts...>;
705 };
706
707 template <typename T, template <T...> class, T...>
708 struct defer_i_
709 {
710 };
711
712 template <typename T, template <T...> class C, T... Is>
713 requires valid_i<T, C, Is...> struct defer_i_<T, C, Is...>
714 {
715 using type = C<Is...>;
716 };
717 #elif defined(META_WORKAROUND_MSVC_703656) // ^^^ Concepts / MSVC workaround vvv
718 template <typename, template <typename...> class, typename...>
719 struct _defer_
720 {
721 };
722
723 template <template <typename...> class C, typename... Ts>
724 struct _defer_<void_<C<Ts...>>, C, Ts...>
725 {
726 using type = C<Ts...>;
727 };
728
729 template <template <typename...> class C, typename... Ts>
730 using defer_ = _defer_<void, C, Ts...>;
731
732 template <typename, typename T, template <T...> class, T...>
733 struct _defer_i_
734 {
735 };
736
737 template <typename T, template <T...> class C, T... Is>
738 struct _defer_i_<void_<C<Is...>>, T, C, Is...>
739 {
740 using type = C<Is...>;
741 };
742
743 template <typename T, template <T...> class C, T... Is>
744 using defer_i_ = _defer_i_<void, T, C, Is...>;
745 #else // ^^^ workaround ^^^ / vvv no workaround vvv
746 template <template <typename...> class C, typename... Ts,
747 template <typename...> class D = C>
748 id<D<Ts...>> try_defer_(int);
749 template <template <typename...> class C, typename... Ts>
750 nil_ try_defer_(long);
751
752 template <template <typename...> class C, typename... Ts>
753 using defer_ = decltype(detail::try_defer_<C, Ts...>(0));
754
755 template <typename T, template <T...> class C, T... Is, template <T...> class D = C>
756 id<D<Is...>> try_defer_i_(int);
757 template <typename T, template <T...> class C, T... Is>
758 nil_ try_defer_i_(long);
759
760 template <typename T, template <T...> class C, T... Is>
761 using defer_i_ = decltype(detail::try_defer_i_<T, C, Is...>(0));
762 #endif // Concepts vs. MSVC vs. Other
763
764 template <typename T>
765 using _t_t = _t<_t<T>>;
766 } // namespace detail
767 /// \endcond
768
769 ///////////////////////////////////////////////////////////////////////////////////////////
770 // defer
771 /// A wrapper that defers the instantiation of a template \p C with type parameters \p Ts in
772 /// a \c lambda or \c let expression.
773 ///
774 /// In the code below, the lambda would ideally be written as
775 /// `lambda<_a,_b,push_back<_a,_b>>`, however this fails since `push_back` expects its first
776 /// argument to be a list, not a placeholder. Instead, we express it using \c defer as
777 /// follows:
778 ///
779 /// \code
780 /// template <typename L>
781 /// using reverse = reverse_fold<L, list<>, lambda<_a, _b, defer<push_back, _a, _b>>>;
782 /// \endcode
783 ///
784 /// \ingroup invocation
785 template <template <typename...> class C, typename... Ts>
786 struct defer : detail::defer_<C, Ts...>
787 {
788 };
789
790 ///////////////////////////////////////////////////////////////////////////////////////////
791 // defer_i
792 /// A wrapper that defers the instantiation of a template \p C with integral constant
793 /// parameters \p Is in a \c lambda or \c let expression.
794 /// \sa `defer`
795 /// \ingroup invocation
796 template <typename T, template <T...> class C, T... Is>
797 struct defer_i : detail::defer_i_<T, C, Is...>
798 {
799 };
800
801 ///////////////////////////////////////////////////////////////////////////////////////////
802 // defer_trait
803 /// A wrapper that defers the instantiation of a trait \p C with type parameters \p Ts in a
804 /// \c lambda or \c let expression.
805 /// \sa `defer`
806 /// \ingroup invocation
807 template <template <typename...> class C, typename... Ts>
808 using defer_trait = defer<detail::_t_t, detail::defer_<C, Ts...>>;
809
810 ///////////////////////////////////////////////////////////////////////////////////////////
811 // defer_trait_i
812 /// A wrapper that defers the instantiation of a trait \p C with integral constant
813 /// parameters \p Is in a \c lambda or \c let expression.
814 /// \sa `defer_i`
815 /// \ingroup invocation
816 template <typename T, template <T...> class C, T... Is>
817 using defer_trait_i = defer<detail::_t_t, detail::defer_i_<T, C, Is...>>;
818
819 /// An alias that computes the size of the type \p T.
820 /// \par Complexity
821 /// `O(1)`.
822 /// \ingroup trait
823 template <typename T>
824 using sizeof_ = meta::size_t<sizeof(T)>;
825
826 /// An alias that computes the alignment required for any instance of the type \p T.
827 /// \par Complexity
828 /// `O(1)`.
829 /// \ingroup trait
830 template <typename T>
831 using alignof_ = meta::size_t<alignof(T)>;
832
833 namespace lazy
834 {
835 /// \sa `meta::sizeof_`
836 /// \ingroup lazy_trait
837 template <typename T>
838 using sizeof_ = defer<sizeof_, T>;
839
840 /// \sa `meta::alignof_`
841 /// \ingroup lazy_trait
842 template <typename T>
843 using alignof_ = defer<alignof_, T>;
844 } // namespace lazy
845
846 #if META_CXX_VARIABLE_TEMPLATES
847 /// is
848 /// Test whether a type \p T is an instantiation of class
849 /// template \p C.
850 /// \ingroup trait
851 template <typename T, template <typename...> class C>
852 using is = bool_<is_v<T, C>>;
853 #else
854 /// is
855 /// \cond
856 namespace detail
857 {
858 template <typename, template <typename...> class>
859 struct is_ : std::false_type
860 {
861 };
862
863 template <typename... Ts, template <typename...> class C>
864 struct is_<C<Ts...>, C> : std::true_type
865 {
866 };
867 } // namespace detail
868 /// \endcond
869
870 /// Test whether a type \c T is an instantiation of class
871 /// template \c C.
872 /// \ingroup trait
873 template <typename T, template <typename...> class C>
874 using is = _t<detail::is_<T, C>>;
875 #endif
876
877 /// Compose the Invocables \p Fns in the parameter pack \p Ts.
878 /// \ingroup composition
879 template <META_TYPE_CONSTRAINT(invocable)... Fns>
880 struct compose_
881 {
882 };
883
884 template <META_TYPE_CONSTRAINT(invocable) Fn0>
885 struct compose_<Fn0>
886 {
887 template <typename... Ts>
888 using invoke = invoke<Fn0, Ts...>;
889 };
890
891 template <META_TYPE_CONSTRAINT(invocable) Fn0, META_TYPE_CONSTRAINT(invocable)... Fns>
892 struct compose_<Fn0, Fns...>
893 {
894 template <typename... Ts>
895 using invoke = invoke<Fn0, invoke<compose_<Fns...>, Ts...>>;
896 };
897
898 template <typename... Fns>
899 using compose = compose_<Fns...>;
900
901 namespace lazy
902 {
903 /// \sa 'meta::compose'
904 /// \ingroup lazy_composition
905 template <typename... Fns>
906 using compose = defer<compose, Fns...>;
907 } // namespace lazy
908
909 /// Turn a template \p C into an invocable.
910 /// \ingroup composition
911 template <template <typename...> class C>
912 struct quote
913 {
914 // Indirection through defer here needed to avoid Core issue 1430
915 // https://wg21.link/cwg1430
916 template <typename... Ts>
917 using invoke = _t<defer<C, Ts...>>;
918 };
919
920 /// Turn a template \p C taking literals of type \p T into a
921 /// invocable.
922 /// \ingroup composition
923 template <typename T, template <T...> class C>
924 struct quote_i
925 {
926 // Indirection through defer_i here needed to avoid Core issue 1430
927 // https://wg21.link/cwg1430
928 template <META_TYPE_CONSTRAINT(integral)... Ts>
929 using invoke = _t<defer_i<T, C, Ts::type::value...>>;
930 };
931
932 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 8 && \
933 !defined(META_DOXYGEN_INVOKED)
934 template <template <typename...> class C>
935 struct quote_trait
936 {
937 template <typename... Ts>
938 using invoke = _t<invoke<quote<C>, Ts...>>;
939 };
940
941 template <typename T, template <T...> class C>
942 struct quote_trait_i
943 {
944 template <typename... Ts>
945 using invoke = _t<invoke<quote_i<T, C>, Ts...>>;
946 };
947 #else
948 // clang-format off
949 /// Turn a trait template \p C into an invocable.
950 /// \code
951 /// static_assert(std::is_same_v<invoke<quote_trait<std::add_const>, int>, int const>, "");
952 /// \endcode
953 /// \ingroup composition
954 template <template <typename...> class C>
955 using quote_trait = compose<quote<_t>, quote<C>>;
956
957 /// Turn a trait template \p C taking literals of type \p T into an invocable.
958 /// \ingroup composition
959 template <typename T, template <T...> class C>
960 using quote_trait_i = compose<quote<_t>, quote_i<T, C>>;
961 // clang-format on
962 #endif
963
964 /// An invocable that partially applies the invocable
965 /// \p Fn by binding the arguments \p Ts to the \e front of \p Fn.
966 /// \ingroup composition
967 template <META_TYPE_CONSTRAINT(invocable) Fn, typename... Ts>
968 struct bind_front
969 {
970 template <typename... Us>
971 using invoke = invoke<Fn, Ts..., Us...>;
972 };
973
974 /// An invocable that partially applies the invocable \p Fn by binding the
975 /// arguments \p Us to the \e back of \p Fn.
976 /// \ingroup composition
977 template <META_TYPE_CONSTRAINT(invocable) Fn, typename... Us>
978 struct bind_back
979 {
980 template <typename... Ts>
981 using invoke = invoke<Fn, Ts..., Us...>;
982 };
983
984 namespace lazy
985 {
986 /// \sa 'meta::bind_front'
987 /// \ingroup lazy_composition
988 template <typename Fn, typename... Ts>
989 using bind_front = defer<bind_front, Fn, Ts...>;
990
991 /// \sa 'meta::bind_back'
992 /// \ingroup lazy_composition
993 template <typename Fn, typename... Ts>
994 using bind_back = defer<bind_back, Fn, Ts...>;
995 } // namespace lazy
996
997 /// Extend meta with your own datatypes.
998 namespace extension
999 {
1000 /// A trait that unpacks the types in the type list \p L into the invocable
1001 /// \p Fn.
1002 /// \ingroup extension
1003 template <META_TYPE_CONSTRAINT(invocable) Fn, typename L>
1004 struct apply
1005 {
1006 };
1007
1008 template <META_TYPE_CONSTRAINT(invocable) Fn, typename Ret, typename... Args>
1009 struct apply<Fn, Ret(Args...)> : lazy::invoke<Fn, Ret, Args...>
1010 {
1011 };
1012
1013 template <META_TYPE_CONSTRAINT(invocable) Fn, template <typename...> class T,
1014 typename... Ts>
1015 struct apply<Fn, T<Ts...>> : lazy::invoke<Fn, Ts...>
1016 {
1017 };
1018
1019 template <META_TYPE_CONSTRAINT(invocable) Fn, typename T, T... Is>
1020 struct apply<Fn, integer_sequence<T, Is...>>
1021 : lazy::invoke<Fn, std::integral_constant<T, Is>...>
1022 {
1023 };
1024 } // namespace extension
1025
1026 /// Applies the invocable \p Fn using the types in the type list \p L as
1027 /// arguments.
1028 /// \ingroup invocation
1029 template <META_TYPE_CONSTRAINT(invocable) Fn, typename L>
1030 using apply = _t<extension::apply<Fn, L>>;
1031
1032 namespace lazy
1033 {
1034 template <typename Fn, typename L>
1035 using apply = defer<apply, Fn, L>;
1036 }
1037
1038 /// An invocable that takes a bunch of arguments, bundles them into a type
1039 /// list, and then calls the invocable \p Fn with the type list \p Q.
1040 /// \ingroup composition
1041 template <META_TYPE_CONSTRAINT(invocable) Fn,
1042 META_TYPE_CONSTRAINT(invocable) Q = quote<list>>
1043 using curry = compose<Fn, Q>;
1044
1045 /// An invocable that takes a type list, unpacks the types, and then
1046 /// calls the invocable \p Fn with the types.
1047 /// \ingroup composition
1048 template <META_TYPE_CONSTRAINT(invocable) Fn>
1049 using uncurry = bind_front<quote<apply>, Fn>;
1050
1051 namespace lazy
1052 {
1053 /// \sa 'meta::curry'
1054 /// \ingroup lazy_composition
1055 template <typename Fn, typename Q = quote<list>>
1056 using curry = defer<curry, Fn, Q>;
1057
1058 /// \sa 'meta::uncurry'
1059 /// \ingroup lazy_composition
1060 template <typename Fn>
1061 using uncurry = defer<uncurry, Fn>;
1062 } // namespace lazy
1063
1064 /// An invocable that reverses the order of the first two arguments.
1065 /// \ingroup composition
1066 template <META_TYPE_CONSTRAINT(invocable) Fn>
1067 struct flip
1068 {
1069 private:
1070 template <typename... Ts>
1071 struct impl
1072 {
1073 };
1074 template <typename A, typename B, typename... Ts>
1075 struct impl<A, B, Ts...> : lazy::invoke<Fn, B, A, Ts...>
1076 {
1077 };
1078
1079 public:
1080 template <typename... Ts>
1081 using invoke = _t<impl<Ts...>>;
1082 };
1083
1084 namespace lazy
1085 {
1086 /// \sa 'meta::flip'
1087 /// \ingroup lazy_composition
1088 template <typename Fn>
1089 using flip = defer<flip, Fn>;
1090 } // namespace lazy
1091
1092 /// \cond
1093 namespace detail
1094 {
1095 template <typename...>
1096 struct on_
1097 {
1098 };
1099 template <typename Fn, typename... Gs>
1100 struct on_<Fn, Gs...>
1101 {
1102 template <typename... Ts>
1103 using invoke = invoke<Fn, invoke<compose<Gs...>, Ts>...>;
1104 };
1105 } // namespace detail
1106 /// \endcond
1107
1108 /// Use as `on<Fn, Gs...>`. Creates an invocable that applies invocable \c Fn to the
1109 /// result of applying invocable `compose<Gs...>` to all the arguments.
1110 /// \ingroup composition
1111 template <META_TYPE_CONSTRAINT(invocable)... Fns>
1112 using on_ = detail::on_<Fns...>;
1113
1114 template <typename... Fns>
1115 using on = on_<Fns...>;
1116
1117 namespace lazy
1118 {
1119 /// \sa 'meta::on'
1120 /// \ingroup lazy_composition
1121 template <typename Fn, typename G>
1122 using on = defer<on, Fn, G>;
1123 } // namespace lazy
1124
1125 ///////////////////////////////////////////////////////////////////////////////////////////
1126 // conditional_t
1127 /// \cond
1128 namespace detail
1129 {
1130 template <bool>
1131 struct _cond
1132 {
1133 template <typename Then, typename Else>
1134 using invoke = Then;
1135 };
1136 template <>
1137 struct _cond<false>
1138 {
1139 template <typename Then, typename Else>
1140 using invoke = Else;
1141 };
1142 } // namespace detail
1143 /// \endcond
1144
1145 /// Select one type or another depending on a compile-time Boolean.
1146 /// \ingroup logical
1147 template <bool If, typename Then, typename Else = void>
1148 using conditional_t = typename detail::_cond<If>::template invoke<Then, Else>;
1149
1150 ///////////////////////////////////////////////////////////////////////////////////////////
1151 // if_
1152 /// \cond
1153 namespace detail
1154 {
1155 #ifdef META_CONCEPT
1156 template <typename...>
1157 struct _if_
1158 {
1159 };
1160
1161 template <typename If>
1162 requires integral<If>
1163 struct _if_<If> : std::enable_if<_v<If>>
1164 {
1165 };
1166
1167 template <typename If, typename Then>
1168 requires integral<If>
1169 struct _if_<If, Then> : std::enable_if<_v<If>, Then>
1170 {
1171 };
1172
1173 template <typename If, typename Then, typename Else>
1174 requires integral<If>
1175 struct _if_<If, Then, Else> : std::conditional<_v<If>, Then, Else>
1176 {
1177 };
1178 #elif defined(__clang__)
1179 // Clang is faster with this implementation
1180 template <typename, typename = bool>
1181 struct _if_
1182 {
1183 };
1184
1185 template <typename If>
1186 struct _if_<list<If>, decltype(bool(If::type::value))> : std::enable_if<If::type::value>
1187 {
1188 };
1189
1190 template <typename If, typename Then>
1191 struct _if_<list<If, Then>, decltype(bool(If::type::value))>
1192 : std::enable_if<If::type::value, Then>
1193 {
1194 };
1195
1196 template <typename If, typename Then, typename Else>
1197 struct _if_<list<If, Then, Else>, decltype(bool(If::type::value))>
1198 : std::conditional<If::type::value, Then, Else>
1199 {
1200 };
1201 #else
1202 // GCC seems to prefer this implementation
1203 template <typename, typename = std::true_type>
1204 struct _if_
1205 {
1206 };
1207
1208 template <typename If>
1209 struct _if_<list<If>, bool_<If::type::value>>
1210 {
1211 using type = void;
1212 };
1213
1214 template <typename If, typename Then>
1215 struct _if_<list<If, Then>, bool_<If::type::value>>
1216 {
1217 using type = Then;
1218 };
1219
1220 template <typename If, typename Then, typename Else>
1221 struct _if_<list<If, Then, Else>, bool_<If::type::value>>
1222 {
1223 using type = Then;
1224 };
1225
1226 template <typename If, typename Then, typename Else>
1227 struct _if_<list<If, Then, Else>, bool_<!If::type::value>>
1228 {
1229 using type = Else;
1230 };
1231 #endif
1232 } // namespace detail
1233 /// \endcond
1234
1235 /// Select one type or another depending on a compile-time Boolean.
1236 /// \ingroup logical
1237 #ifdef META_CONCEPT
1238 template <typename... Args>
1239 using if_ = _t<detail::_if_<Args...>>;
1240
1241 /// Select one type or another depending on a compile-time Boolean.
1242 /// \ingroup logical
1243 template <bool If, typename... Args>
1244 using if_c = _t<detail::_if_<bool_<If>, Args...>>;
1245 #else
1246 template <typename... Args>
1247 using if_ = _t<detail::_if_<list<Args...>>>;
1248
1249 template <bool If, typename... Args>
1250 using if_c = _t<detail::_if_<list<bool_<If>, Args...>>>;
1251 #endif
1252
1253 namespace lazy
1254 {
1255 /// \sa 'meta::if_'
1256 /// \ingroup lazy_logical
1257 template <typename... Args>
1258 using if_ = defer<if_, Args...>;
1259
1260 /// \sa 'meta::if_c'
1261 /// \ingroup lazy_logical
1262 template <bool If, typename... Args>
1263 using if_c = if_<bool_<If>, Args...>;
1264 } // namespace lazy
1265
1266 /// \cond
1267 namespace detail
1268 {
1269 #ifdef META_CONCEPT
1270 template <typename...>
1271 struct _and_
1272 {
1273 };
1274
1275 template <>
1276 struct _and_<> : std::true_type
1277 {
1278 };
1279
1280 template <typename B, typename... Bs>
1281 requires integral<B> && (bool(B::type::value))
1282 struct _and_<B, Bs...> : _and_<Bs...>
1283 {
1284 };
1285
1286 template <typename B, typename... Bs>
1287 requires integral<B> && (!bool(B::type::value))
1288 struct _and_<B, Bs...> : std::false_type
1289 {
1290 };
1291
1292 template <typename...>
1293 struct _or_
1294 {
1295 };
1296
1297 template <>
1298 struct _or_<> : std::false_type
1299 {
1300 };
1301
1302 template <typename B, typename... Bs>
1303 requires integral<B> && (bool(B::type::value))
1304 struct _or_<B, Bs...> : std::true_type
1305 {
1306 };
1307
1308 template <typename B, typename... Bs>
1309 requires integral<B> && (!bool(B::type::value))
1310 struct _or_<B, Bs...> : _or_<Bs...>
1311 {
1312 };
1313 #else
1314 template <bool>
1315 struct _and_
1316 {
1317 template <typename...>
1318 using invoke = std::true_type;
1319 };
1320
1321 template <>
1322 struct _and_<false>
1323 {
1324 template <typename B, typename... Bs>
1325 using invoke = invoke<
1326 if_c<!B::type::value, id<std::false_type>, _and_<0 == sizeof...(Bs)>>,
1327 Bs...>;
1328 };
1329
1330 template <bool>
1331 struct _or_
1332 {
1333 template <typename = void>
1334 using invoke = std::false_type;
1335 };
1336
1337 template <>
1338 struct _or_<false>
1339 {
1340 template <typename B, typename... Bs>
1341 using invoke = invoke<
1342 if_c<B::type::value, id<std::true_type>, _or_<0 == sizeof...(Bs)>>,
1343 Bs...>;
1344 };
1345 #endif
1346 } // namespace detail
1347 /// \endcond
1348
1349 /// Logically negate the Boolean parameter
1350 /// \ingroup logical
1351 template <bool B>
1352 using not_c = bool_<!B>;
1353
1354 /// Logically negate the integral constant-wrapped Boolean parameter.
1355 /// \ingroup logical
1356 template <META_TYPE_CONSTRAINT(integral) B>
1357 using not_ = not_c<B::type::value>;
1358
1359 #if META_CXX_FOLD_EXPRESSIONS && !defined(META_WORKAROUND_GCC_UNKNOWN1)
1360 template <bool... Bs>
1361 META_INLINE_VAR constexpr bool and_v = (true && ... && Bs);
1362
1363 /// Logically AND together all the Boolean parameters
1364 /// \ingroup logical
1365 template <bool... Bs>
1366 #if defined(META_WORKAROUND_MSVC_756112) || defined(META_WORKAROUND_GCC_86356)
1367 using and_c = bool_<and_v<Bs...>>;
1368 #else
1369 using and_c = bool_<(true && ... && Bs)>;
1370 #endif
1371 #else
1372 #if defined(META_WORKAROUND_GCC_66405)
1373 template <bool... Bs>
1374 using and_c = meta::bool_<
1375 META_IS_SAME(integer_sequence<bool, true, Bs...>,
1376 integer_sequence<bool, Bs..., true>)>;
1377 #else
1378 template <bool... Bs>
1379 struct and_c
1380 : meta::bool_<
1381 META_IS_SAME(integer_sequence<bool, Bs...>,
1382 integer_sequence<bool, (Bs || true)...>)>
1383 {};
1384 #endif
1385 #if META_CXX_VARIABLE_TEMPLATES
1386 template <bool... Bs>
1387 META_INLINE_VAR constexpr bool and_v =
1388 META_IS_SAME(integer_sequence<bool, Bs...>,
1389 integer_sequence<bool, (Bs || true)...>);
1390 #endif
1391 #endif
1392
1393 /// Logically AND together all the integral constant-wrapped Boolean
1394 /// parameters, \e without short-circuiting.
1395 /// \ingroup logical
1396 template <META_TYPE_CONSTRAINT(integral)... Bs>
1397 using strict_and_ = and_c<Bs::type::value...>;
1398
1399 template <typename... Bs>
1400 using strict_and = strict_and_<Bs...>;
1401
1402 /// Logically AND together all the integral constant-wrapped Boolean
1403 /// parameters, \e with short-circuiting.
1404 /// \ingroup logical
1405 template <typename... Bs>
1406 #ifdef META_CONCEPT
1407 using and_ = _t<detail::_and_<Bs...>>;
1408 #else
1409 // Make a trip through defer<> to avoid CWG1430
1410 // https://wg21.link/cwg1430
1411 using and_ = _t<defer<detail::_and_<0 == sizeof...(Bs)>::template invoke, Bs...>>;
1412 #endif
1413
1414 /// Logically OR together all the Boolean parameters
1415 /// \ingroup logical
1416 #if META_CXX_FOLD_EXPRESSIONS && !defined(META_WORKAROUND_GCC_UNKNOWN1)
1417 template <bool... Bs>
1418 META_INLINE_VAR constexpr bool or_v = (false || ... || Bs);
1419
1420 template <bool... Bs>
1421 #if defined(META_WORKAROUND_MSVC_756112) || defined(META_WORKAROUND_GCC_86356)
1422 using or_c = bool_<or_v<Bs...>>;
1423 #else
1424 using or_c = bool_<(false || ... || Bs)>;
1425 #endif
1426 #else
1427 template <bool... Bs>
1428 struct or_c
1429 : meta::bool_<
1430 !META_IS_SAME(integer_sequence<bool, Bs...>,
1431 integer_sequence<bool, (Bs && false)...>)>
1432 {};
1433 #if META_CXX_VARIABLE_TEMPLATES
1434 template <bool... Bs>
1435 META_INLINE_VAR constexpr bool or_v =
1436 !META_IS_SAME(integer_sequence<bool, Bs...>,
1437 integer_sequence<bool, (Bs && false)...>);
1438 #endif
1439 #endif
1440
1441 /// Logically OR together all the integral constant-wrapped Boolean
1442 /// parameters, \e without short-circuiting.
1443 /// \ingroup logical
1444 template <META_TYPE_CONSTRAINT(integral)... Bs>
1445 using strict_or_ = or_c<Bs::type::value...>;
1446
1447 template <typename... Bs>
1448 using strict_or = strict_or_<Bs...>;
1449
1450 /// Logically OR together all the integral constant-wrapped Boolean
1451 /// parameters, \e with short-circuiting.
1452 /// \ingroup logical
1453 template <typename... Bs>
1454 #ifdef META_CONCEPT
1455 using or_ = _t<detail::_or_<Bs...>>;
1456 #else
1457 // Make a trip through defer<> to avoid CWG1430
1458 // https://wg21.link/cwg1430
1459 using or_ = _t<defer<detail::_or_<0 == sizeof...(Bs)>::template invoke, Bs...>>;
1460 #endif
1461
1462 namespace lazy
1463 {
1464 /// \sa 'meta::and_'
1465 /// \ingroup lazy_logical
1466 template <typename... Bs>
1467 using and_ = defer<and_, Bs...>;
1468
1469 /// \sa 'meta::or_'
1470 /// \ingroup lazy_logical
1471 template <typename... Bs>
1472 using or_ = defer<or_, Bs...>;
1473
1474 /// \sa 'meta::not_'
1475 /// \ingroup lazy_logical
1476 template <typename B>
1477 using not_ = defer<not_, B>;
1478
1479 /// \sa 'meta::strict_and'
1480 /// \ingroup lazy_logical
1481 template <typename... Bs>
1482 using strict_and = defer<strict_and, Bs...>;
1483
1484 /// \sa 'meta::strict_or'
1485 /// \ingroup lazy_logical
1486 template <typename... Bs>
1487 using strict_or = defer<strict_or, Bs...>;
1488 } // namespace lazy
1489
1490 ///////////////////////////////////////////////////////////////////////////////////////////
1491 // fold
1492 /// \cond
1493 namespace detail
1494 {
1495 template <typename, typename, typename>
1496 struct fold_
1497 {
1498 };
1499
1500 template <typename Fn, typename T0, typename T1, typename T2, typename T3, typename T4,
1501 typename T5, typename T6, typename T7, typename T8, typename T9>
1502 struct compose10_
1503 {
1504 template <typename X, typename Y>
1505 using F = invoke<Fn, X, Y>;
1506
1507 template <typename S>
1508 using invoke =
1509 F<F<F<F<F<F<F<F<F<F<_t<S>, T0>, T1>, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>;
1510 };
1511
1512 #ifdef META_CONCEPT
1513 template <typename Fn>
1514 struct compose_
1515 {
1516 template <typename X, typename Y>
1517 using F = invoke<Fn, X, Y>;
1518
1519 template <typename T0, typename T1, typename T2, typename T3, typename T4,
1520 typename T5, typename T6, typename T7, typename T8, typename T9,
1521 typename State>
1522 using invoke =
1523 F<F<F<F<F<F<F<F<F<F<State, T0>, T1>, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>;
1524 };
1525
1526 template <typename State, typename Fn>
1527 struct fold_<list<>, State, Fn>
1528 {
1529 using type = State;
1530 };
1531
1532 template <typename Head, typename... Tail, typename State, typename Fn>
1533 requires valid<invoke, Fn, State, Head>
1534 struct fold_<list<Head, Tail...>, State, Fn>
1535 : fold_<list<Tail...>, invoke<Fn, State, Head>, Fn>
1536 {
1537 };
1538
1539 template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
1540 typename T6, typename T7, typename T8, typename T9, typename... Tail,
1541 typename State, typename Fn>
1542 requires valid<invoke, compose_<Fn>, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, State>
1543 struct fold_<list<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, Tail...>, State, Fn>
1544 : fold_<list<Tail...>,
1545 invoke<compose_<Fn>, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, State>, Fn>
1546 {
1547 };
1548 #else // ^^^ Concepts / no Concepts vvv
1549 template <typename Fn, typename T0>
1550 struct compose1_
1551 {
1552 template <typename X>
1553 using invoke = invoke<Fn, _t<X>, T0>;
1554 };
1555
1556 template <typename State, typename Fn>
1557 struct fold_<list<>, State, Fn> : State
1558 {
1559 };
1560
1561 template <typename Head, typename... Tail, typename State, typename Fn>
1562 struct fold_<list<Head, Tail...>, State, Fn>
1563 : fold_<list<Tail...>, lazy::invoke<compose1_<Fn, Head>, State>, Fn>
1564 {
1565 };
1566
1567 template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
1568 typename T6, typename T7, typename T8, typename T9, typename... Tail,
1569 typename State, typename Fn>
1570 struct fold_<list<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, Tail...>, State, Fn>
1571 : fold_<list<Tail...>,
1572 lazy::invoke<compose10_<Fn, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>, State>, Fn>
1573 {
1574 };
1575 #endif // META_CONCEPT
1576 } // namespace detail
1577 /// \endcond
1578
1579 /// Return a new \c meta::list constructed by doing a left fold of the list \p L using
1580 /// binary invocable \p Fn and initial state \p State. That is, the \c State(N) for
1581 /// the list element \c A(N) is computed by `Fn(State(N-1), A(N)) -> State(N)`.
1582 /// \par Complexity
1583 /// `O(N)`.
1584 /// \ingroup transformation
1585 template <META_TYPE_CONSTRAINT(list_like) L, typename State, META_TYPE_CONSTRAINT(invocable) Fn>
1586 #ifdef META_CONCEPT
1587 using fold = _t<detail::fold_<L, State, Fn>>;
1588 #else
1589 using fold = _t<detail::fold_<L, id<State>, Fn>>;
1590 #endif
1591
1592 /// An alias for `meta::fold`.
1593 /// \par Complexity
1594 /// `O(N)`.
1595 /// \ingroup transformation
1596 template <META_TYPE_CONSTRAINT(list_like) L, typename State, META_TYPE_CONSTRAINT(invocable) Fn>
1597 using accumulate = fold<L, State, Fn>;
1598
1599 namespace lazy
1600 {
1601 /// \sa 'meta::foldl'
1602 /// \ingroup lazy_transformation
1603 template <typename L, typename State, typename Fn>
1604 using fold = defer<fold, L, State, Fn>;
1605
1606 /// \sa 'meta::accumulate'
1607 /// \ingroup lazy_transformation
1608 template <typename L, typename State, typename Fn>
1609 using accumulate = defer<accumulate, L, State, Fn>;
1610 } // namespace lazy
1611
1612 ///////////////////////////////////////////////////////////////////////////////////////////
1613 // reverse_fold
1614 /// \cond
1615 namespace detail
1616 {
1617 template <typename, typename, typename>
1618 struct reverse_fold_
1619 {
1620 };
1621
1622 template <typename State, typename Fn>
1623 struct reverse_fold_<list<>, State, Fn>
1624 {
1625 using type = State;
1626 };
1627
1628 #ifdef META_CONCEPT
1629 template <typename Head, typename... L, typename State, typename Fn>
1630 requires trait<reverse_fold_<list<L...>, State, Fn>> struct reverse_fold_<
1631 list<Head, L...>, State, Fn>
1632 : lazy::invoke<Fn, _t<reverse_fold_<list<L...>, State, Fn>>, Head>
1633 {
1634 };
1635 #else
1636 template <typename Head, typename... Tail, typename State, typename Fn>
1637 struct reverse_fold_<list<Head, Tail...>, State, Fn>
1638 : lazy::invoke<compose1_<Fn, Head>, reverse_fold_<list<Tail...>, State, Fn>>
1639 {
1640 };
1641 #endif
1642
1643 template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
1644 typename T6, typename T7, typename T8, typename T9, typename... Tail,
1645 typename State, typename Fn>
1646 struct reverse_fold_<list<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, Tail...>, State, Fn>
1647 : lazy::invoke<compose10_<Fn, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0>,
1648 reverse_fold_<list<Tail...>, State, Fn>>
1649 {
1650 };
1651 } // namespace detail
1652 /// \endcond
1653
1654 /// Return a new \c meta::list constructed by doing a right fold of the list \p L using
1655 /// binary invocable \p Fn and initial state \p State. That is, the \c State(N) for the list
1656 /// element \c A(N) is computed by `Fn(A(N), State(N+1)) -> State(N)`.
1657 /// \par Complexity
1658 /// `O(N)`.
1659 /// \ingroup transformation
1660 template <META_TYPE_CONSTRAINT(list_like) L, typename State, META_TYPE_CONSTRAINT(invocable) Fn>
1661 using reverse_fold = _t<detail::reverse_fold_<L, State, Fn>>;
1662
1663 namespace lazy
1664 {
1665 /// \sa 'meta::foldr'
1666 /// \ingroup lazy_transformation
1667 template <typename L, typename State, typename Fn>
1668 using reverse_fold = defer<reverse_fold, L, State, Fn>;
1669 } // namespace lazy
1670
1671 ///////////////////////////////////////////////////////////////////////////////////////////
1672 // npos
1673 /// A special value used to indicate no matches. It equals the maximum
1674 /// value representable by std::size_t.
1675 /// \ingroup list
1676 using npos = meta::size_t<std::size_t(-1)>;
1677
1678 ///////////////////////////////////////////////////////////////////////////////////////////
1679 // list
1680 /// A list of types.
1681 /// \ingroup list
1682 template <typename... Ts>
1683 struct list
1684 {
1685 using type = list;
1686 /// \return `sizeof...(Ts)`
1687 static constexpr std::size_t size() noexcept { return sizeof...(Ts); }
1688 };
1689
1690 ///////////////////////////////////////////////////////////////////////////////////////////
1691 // size
1692 /// An integral constant wrapper that is the size of the \c meta::list
1693 /// \p L.
1694 /// \ingroup list
1695 template <META_TYPE_CONSTRAINT(list_like) L>
1696 using size = meta::size_t<L::size()>;
1697
1698 namespace lazy
1699 {
1700 /// \sa 'meta::size'
1701 /// \ingroup lazy_list
1702 template <typename L>
1703 using size = defer<size, L>;
1704 } // namespace lazy
1705
1706 ///////////////////////////////////////////////////////////////////////////////////////////
1707 // concat
1708 /// \cond
1709 namespace detail
1710 {
1711 template <typename... Lists>
1712 struct concat_
1713 {
1714 };
1715
1716 template <>
1717 struct concat_<>
1718 {
1719 using type = list<>;
1720 };
1721
1722 template <typename... L1>
1723 struct concat_<list<L1...>>
1724 {
1725 using type = list<L1...>;
1726 };
1727
1728 template <typename... L1, typename... L2>
1729 struct concat_<list<L1...>, list<L2...>>
1730 {
1731 using type = list<L1..., L2...>;
1732 };
1733
1734 template <typename... L1, typename... L2, typename... L3>
1735 struct concat_<list<L1...>, list<L2...>, list<L3...>>
1736 {
1737 using type = list<L1..., L2..., L3...>;
1738 };
1739
1740 template <typename... L1, typename... L2, typename... L3, typename... Rest>
1741 struct concat_<list<L1...>, list<L2...>, list<L3...>, Rest...>
1742 : concat_<list<L1..., L2..., L3...>, Rest...>
1743 {
1744 };
1745
1746 template <typename... L1, typename... L2, typename... L3, typename... L4,
1747 typename... L5, typename... L6, typename... L7, typename... L8,
1748 typename... L9, typename... L10, typename... Rest>
1749 struct concat_<list<L1...>, list<L2...>, list<L3...>, list<L4...>, list<L5...>,
1750 list<L6...>, list<L7...>, list<L8...>, list<L9...>, list<L10...>,
1751 Rest...>
1752 : concat_<list<L1..., L2..., L3..., L4..., L5..., L6..., L7..., L8..., L9..., L10...>,
1753 Rest...>
1754 {
1755 };
1756 } // namespace detail
1757 /// \endcond
1758
1759 /// Concatenates several lists into a single list.
1760 /// \pre The parameters must all be instantiations of \c meta::list.
1761 /// \par Complexity
1762 /// `O(L)` where `L` is the number of lists in the list of lists.
1763 /// \ingroup transformation
1764 template <META_TYPE_CONSTRAINT(list_like)... Ls>
1765 using concat_ = _t<detail::concat_<Ls...>>;
1766
1767 template <typename... Lists>
1768 using concat = concat_<Lists...>;
1769
1770 namespace lazy
1771 {
1772 /// \sa 'meta::concat'
1773 /// \ingroup lazy_transformation
1774 template <typename... Lists>
1775 using concat = defer<concat, Lists...>;
1776 } // namespace lazy
1777
1778 /// Joins a list of lists into a single list.
1779 /// \pre The parameter must be an instantiation of \c meta::list\<T...\>
1780 /// where each \c T is itself an instantiation of \c meta::list.
1781 /// \par Complexity
1782 /// `O(L)` where `L` is the number of lists in the list of
1783 /// lists.
1784 /// \ingroup transformation
1785 template <META_TYPE_CONSTRAINT(list_like) ListOfLists>
1786 using join = apply<quote<concat>, ListOfLists>;
1787
1788 namespace lazy
1789 {
1790 /// \sa 'meta::join'
1791 /// \ingroup lazy_transformation
1792 template <typename ListOfLists>
1793 using join = defer<join, ListOfLists>;
1794 } // namespace lazy
1795
1796 ///////////////////////////////////////////////////////////////////////////////////////////
1797 // transform
1798 /// \cond
1799 namespace detail
1800 {
1801 #ifdef META_CONCEPT
1802 template <typename... Args>
1803 struct transform_
1804 {
1805 };
1806
1807 template <typename... Ts, typename Fn>
1808 requires invocable<Fn> && and_v<valid<invoke, Fn, Ts>...>
1809 struct transform_<list<Ts...>, Fn>
1810 {
1811 using type = list<invoke<Fn, Ts>...>;
1812 };
1813
1814 template <typename... Ts, typename... Us, typename Fn>
1815 requires invocable<Fn> && and_v<valid<invoke, Fn, Ts, Us>...>
1816 struct transform_<list<Ts...>, list<Us...>, Fn>
1817 {
1818 using type = list<invoke<Fn, Ts, Us>...>;
1819 };
1820 #else
1821 template <typename, typename = void>
1822 struct transform_
1823 {
1824 };
1825
1826 template <typename... Ts, typename Fn>
1827 struct transform_<list<list<Ts...>, Fn>, void_<invoke<Fn, Ts>...>>
1828 {
1829 using type = list<invoke<Fn, Ts>...>;
1830 };
1831
1832 template <typename... Ts0, typename... Ts1, typename Fn>
1833 struct transform_<list<list<Ts0...>, list<Ts1...>, Fn>,
1834 void_<invoke<Fn, Ts0, Ts1>...>>
1835 {
1836 using type = list<invoke<Fn, Ts0, Ts1>...>;
1837 };
1838 #endif
1839 } // namespace detail
1840 /// \endcond
1841
1842 /// Return a new \c meta::list constructed by transforming all the
1843 /// elements in \p L with the unary invocable \p Fn. \c transform can
1844 /// also be called with two lists of the same length and a binary
1845 /// invocable, in which case it returns a new list constructed with the
1846 /// results of calling \c Fn with each element in the lists, pairwise.
1847 /// \par Complexity
1848 /// `O(N)`.
1849 /// \ingroup transformation
1850 #ifdef META_CONCEPT
1851 template <typename... Args>
1852 using transform = _t<detail::transform_<Args...>>;
1853 #else
1854 template <typename... Args>
1855 using transform = _t<detail::transform_<list<Args...>>>;
1856 #endif
1857
1858 namespace lazy
1859 {
1860 /// \sa 'meta::transform'
1861 /// \ingroup lazy_transformation
1862 template <typename... Args>
1863 using transform = defer<transform, Args...>;
1864 } // namespace lazy
1865
1866 ///////////////////////////////////////////////////////////////////////////////////////////
1867 // repeat_n
1868 /// \cond
1869 namespace detail
1870 {
1871 template <typename T, std::size_t>
1872 using first_ = T;
1873
1874 template <typename T, typename Ints>
1875 struct repeat_n_c_
1876 {
1877 };
1878
1879 template <typename T, std::size_t... Is>
1880 struct repeat_n_c_<T, index_sequence<Is...>>
1881 {
1882 using type = list<first_<T, Is>...>;
1883 };
1884 } // namespace detail
1885 /// \endcond
1886
1887 /// Generate `list<T,T,T...T>` of size \p N arguments.
1888 /// \par Complexity
1889 /// `O(log N)`.
1890 /// \ingroup list
1891 template <std::size_t N, typename T = void>
1892 using repeat_n_c = _t<detail::repeat_n_c_<T, make_index_sequence<N>>>;
1893
1894 /// Generate `list<T,T,T...T>` of size \p N arguments.
1895 /// \par Complexity
1896 /// `O(log N)`.
1897 /// \ingroup list
1898 template <META_TYPE_CONSTRAINT(integral) N, typename T = void>
1899 using repeat_n = repeat_n_c<N::type::value, T>;
1900
1901 namespace lazy
1902 {
1903 /// \sa 'meta::repeat_n'
1904 /// \ingroup lazy_list
1905 template <typename N, typename T = void>
1906 using repeat_n = defer<repeat_n, N, T>;
1907
1908 /// \sa 'meta::repeat_n_c'
1909 /// \ingroup lazy_list
1910 template <std::size_t N, typename T = void>
1911 using repeat_n_c = defer<repeat_n, meta::size_t<N>, T>;
1912 } // namespace lazy
1913
1914 ///////////////////////////////////////////////////////////////////////////////////////////
1915 // at
1916 /// \cond
1917 namespace detail
1918 {
1919 #if META_HAS_TYPE_PACK_ELEMENT && !defined(META_DOXYGEN_INVOKED)
1920 template <typename L, std::size_t N, typename = void>
1921 struct at_
1922 {
1923 };
1924
1925 template <typename... Ts, std::size_t N>
1926 struct at_<list<Ts...>, N, void_<__type_pack_element<N, Ts...>>>
1927 {
1928 using type = __type_pack_element<N, Ts...>;
1929 };
1930 #else
1931 template <typename VoidPtrs>
1932 struct at_impl_;
1933
1934 template <typename... VoidPtrs>
1935 struct at_impl_<list<VoidPtrs...>>
1936 {
1937 static nil_ eval(...);
1938
1939 template <typename T, typename... Us>
1940 static T eval(VoidPtrs..., T *, Us *...);
1941 };
1942
1943 template <typename L, std::size_t N>
1944 struct at_
1945 {
1946 };
1947
1948 template <typename... Ts, std::size_t N>
1949 struct at_<list<Ts...>, N>
1950 : decltype(at_impl_<repeat_n_c<N, void *>>::eval(static_cast<id<Ts> *>(nullptr)...))
1951 {
1952 };
1953 #endif // META_HAS_TYPE_PACK_ELEMENT
1954 } // namespace detail
1955 /// \endcond
1956
1957 /// Return the \p N th element in the \c meta::list \p L.
1958 /// \par Complexity
1959 /// Amortized `O(1)`.
1960 /// \ingroup list
1961 template <META_TYPE_CONSTRAINT(list_like) L, std::size_t N>
1962 using at_c = _t<detail::at_<L, N>>;
1963
1964 /// Return the \p N th element in the \c meta::list \p L.
1965 /// \par Complexity
1966 /// Amortized `O(1)`.
1967 /// \ingroup list
1968 template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(integral) N>
1969 using at = at_c<L, N::type::value>;
1970
1971 namespace lazy
1972 {
1973 /// \sa 'meta::at'
1974 /// \ingroup lazy_list
1975 template <typename L, typename N>
1976 using at = defer<at, L, N>;
1977 } // namespace lazy
1978
1979 ///////////////////////////////////////////////////////////////////////////////////////////
1980 // drop
1981 /// \cond
1982 namespace detail
1983 {
1984 ///////////////////////////////////////////////////////////////////////////////////////
1985 /// drop_impl_
1986 template <typename VoidPtrs>
1987 struct drop_impl_
1988 {
1989 static nil_ eval(...);
1990 };
1991
1992 template <typename... VoidPtrs>
1993 struct drop_impl_<list<VoidPtrs...>>
1994 {
1995 static nil_ eval(...);
1996
1997 template <typename... Ts>
1998 static id<list<Ts...>> eval(VoidPtrs..., id<Ts> *...);
1999 };
2000
2001 template <>
2002 struct drop_impl_<list<>>
2003 {
2004 template <typename... Ts>
2005 static id<list<Ts...>> eval(id<Ts> *...);
2006 };
2007
2008 template <typename L, std::size_t N>
2009 struct drop_
2010 {
2011 };
2012
2013 template <typename... Ts, std::size_t N>
2014 struct drop_<list<Ts...>, N>
2015 #if META_CXX_VARIABLE_TEMPLATES
2016 : decltype(drop_impl_<repeat_n_c<N, void *>>::eval(detail::nullptr_v<id<Ts>>...))
2017 #else
2018 : decltype(drop_impl_<repeat_n_c<N, void *>>::eval(detail::_nullptr_v<id<Ts>>()...))
2019 #endif
2020 {
2021 };
2022 } // namespace detail
2023 /// \endcond
2024
2025 /// Return a new \c meta::list by removing the first \p N elements from \p L.
2026 /// \par Complexity
2027 /// `O(1)`.
2028 /// \ingroup transformation
2029 template <META_TYPE_CONSTRAINT(list_like) L, std::size_t N>
2030 using drop_c = _t<detail::drop_<L, N>>;
2031
2032 /// Return a new \c meta::list by removing the first \p N elements from \p L.
2033 /// \par Complexity
2034 /// `O(1)`.
2035 /// \ingroup transformation
2036 template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(integral) N>
2037 using drop = drop_c<L, N::type::value>;
2038
2039 namespace lazy
2040 {
2041 /// \sa 'meta::drop'
2042 /// \ingroup lazy_transformation
2043 template <typename L, typename N>
2044 using drop = defer<drop, L, N>;
2045 } // namespace lazy
2046
2047 ///////////////////////////////////////////////////////////////////////////////////////////
2048 // front
2049 /// \cond
2050 namespace detail
2051 {
2052 template <typename L>
2053 struct front_
2054 {
2055 };
2056
2057 template <typename Head, typename... Tail>
2058 struct front_<list<Head, Tail...>>
2059 {
2060 using type = Head;
2061 };
2062 } // namespace detail
2063 /// \endcond
2064
2065 /// Return the first element in \c meta::list \p L.
2066 /// \par Complexity
2067 /// `O(1)`.
2068 /// \ingroup list
2069 template <META_TYPE_CONSTRAINT(list_like) L>
2070 using front = _t<detail::front_<L>>;
2071
2072 namespace lazy
2073 {
2074 /// \sa 'meta::front'
2075 /// \ingroup lazy_list
2076 template <typename L>
2077 using front = defer<front, L>;
2078 } // namespace lazy
2079
2080 ///////////////////////////////////////////////////////////////////////////////////////////
2081 // back
2082 /// \cond
2083 namespace detail
2084 {
2085 template <typename L>
2086 struct back_
2087 {
2088 };
2089
2090 template <typename Head, typename... Tail>
2091 struct back_<list<Head, Tail...>>
2092 {
2093 using type = at_c<list<Head, Tail...>, sizeof...(Tail)>;
2094 };
2095 } // namespace detail
2096 /// \endcond
2097
2098 /// Return the last element in \c meta::list \p L.
2099 /// \par Complexity
2100 /// Amortized `O(1)`.
2101 /// \ingroup list
2102 template <META_TYPE_CONSTRAINT(list_like) L>
2103 using back = _t<detail::back_<L>>;
2104
2105 namespace lazy
2106 {
2107 /// \sa 'meta::back'
2108 /// \ingroup lazy_list
2109 template <typename L>
2110 using back = defer<back, L>;
2111 } // namespace lazy
2112
2113 ///////////////////////////////////////////////////////////////////////////////////////////
2114 // push_front
2115 /// Return a new \c meta::list by adding the element \c T to the front of \p L.
2116 /// \par Complexity
2117 /// `O(1)`.
2118 /// \ingroup transformation
2119 template <META_TYPE_CONSTRAINT(list_like) L, typename... Ts>
2120 using push_front = apply<bind_front<quote<list>, Ts...>, L>;
2121
2122 namespace lazy
2123 {
2124 /// \sa 'meta::push_front'
2125 /// \ingroup lazy_transformation
2126 template <typename... Ts>
2127 using push_front = defer<push_front, Ts...>;
2128 } // namespace lazy
2129
2130 ///////////////////////////////////////////////////////////////////////////////////////////
2131 // pop_front
2132 /// \cond
2133 namespace detail
2134 {
2135 template <typename L>
2136 struct pop_front_
2137 {
2138 };
2139
2140 template <typename Head, typename... L>
2141 struct pop_front_<list<Head, L...>>
2142 {
2143 using type = list<L...>;
2144 };
2145 } // namespace detail
2146 /// \endcond
2147
2148 /// Return a new \c meta::list by removing the first element from the
2149 /// front of \p L.
2150 /// \par Complexity
2151 /// `O(1)`.
2152 /// \ingroup transformation
2153 template <META_TYPE_CONSTRAINT(list_like) L>
2154 using pop_front = _t<detail::pop_front_<L>>;
2155
2156 namespace lazy
2157 {
2158 /// \sa 'meta::pop_front'
2159 /// \ingroup lazy_transformation
2160 template <typename L>
2161 using pop_front = defer<pop_front, L>;
2162 } // namespace lazy
2163
2164 ///////////////////////////////////////////////////////////////////////////////////////////
2165 // push_back
2166 /// Return a new \c meta::list by adding the element \c T to the back of \p L.
2167 /// \par Complexity
2168 /// `O(1)`.
2169 /// \note \c pop_back not provided because it cannot be made to meet the
2170 /// complexity guarantees one would expect.
2171 /// \ingroup transformation
2172 template <META_TYPE_CONSTRAINT(list_like) L, typename... Ts>
2173 using push_back = apply<bind_back<quote<list>, Ts...>, L>;
2174
2175 namespace lazy
2176 {
2177 /// \sa 'meta::push_back'
2178 /// \ingroup lazy_transformation
2179 template <typename... Ts>
2180 using push_back = defer<push_back, Ts...>;
2181 } // namespace lazy
2182
2183 /// \cond
2184 namespace detail
2185 {
2186 template <typename T, typename U>
2187 using min_ = if_<less<U, T>, U, T>;
2188
2189 template <typename T, typename U>
2190 using max_ = if_<less<U, T>, T, U>;
2191 } // namespace detail
2192 /// \endcond
2193
2194 /// An integral constant wrapper around the minimum of `Ts::type::value...`
2195 /// \ingroup math
2196 template <META_TYPE_CONSTRAINT(integral)... Ts>
2197 using min_ = fold<pop_front<list<Ts...>>, front<list<Ts...>>, quote<detail::min_>>;
2198
2199 template <typename... Ts>
2200 using min = min_<Ts...>;
2201
2202 /// An integral constant wrapper around the maximum of `Ts::type::value...`
2203 /// \ingroup math
2204 template <META_TYPE_CONSTRAINT(integral)... Ts>
2205 using max_ = fold<pop_front<list<Ts...>>, front<list<Ts...>>, quote<detail::max_>>;
2206
2207 template <typename... Ts>
2208 using max = max_<Ts...>;
2209
2210 namespace lazy
2211 {
2212 /// \sa 'meta::min'
2213 /// \ingroup lazy_math
2214 template <typename... Ts>
2215 using min = defer<min, Ts...>;
2216
2217 /// \sa 'meta::max'
2218 /// \ingroup lazy_math
2219 template <typename... Ts>
2220 using max = defer<max, Ts...>;
2221 } // namespace lazy
2222
2223 ///////////////////////////////////////////////////////////////////////////////////////////
2224 // empty
2225 /// An Boolean integral constant wrapper around \c true if \p L is an
2226 /// empty type list; \c false, otherwise.
2227 /// \par Complexity
2228 /// `O(1)`.
2229 /// \ingroup list
2230 template <META_TYPE_CONSTRAINT(list_like) L>
2231 using empty = bool_<0 == size<L>::type::value>;
2232
2233 namespace lazy
2234 {
2235 /// \sa 'meta::empty'
2236 /// \ingroup lazy_list
2237 template <typename L>
2238 using empty = defer<empty, L>;
2239 } // namespace lazy
2240
2241 ///////////////////////////////////////////////////////////////////////////////////////////
2242 // pair
2243 /// A list with exactly two elements
2244 /// \ingroup list
2245 template <typename F, typename S>
2246 using pair = list<F, S>;
2247
2248 /// Retrieve the first element of the \c pair \p Pair
2249 /// \ingroup list
2250 template <typename Pair>
2251 using first = front<Pair>;
2252
2253 /// Retrieve the first element of the \c pair \p Pair
2254 /// \ingroup list
2255 template <typename Pair>
2256 using second = front<pop_front<Pair>>;
2257
2258 namespace lazy
2259 {
2260 /// \sa 'meta::first'
2261 /// \ingroup lazy_list
2262 template <typename Pair>
2263 using first = defer<first, Pair>;
2264
2265 /// \sa 'meta::second'
2266 /// \ingroup lazy_list
2267 template <typename Pair>
2268 using second = defer<second, Pair>;
2269 } // namespace lazy
2270
2271 ///////////////////////////////////////////////////////////////////////////////////////////
2272 // find_index
2273 /// \cond
2274 namespace detail
2275 {
2276 // With thanks to Peter Dimov:
2277 constexpr std::size_t find_index_i_(bool const *const first, bool const *const last,
2278 std::size_t N = 0)
2279 {
2280 return first == last ? npos::value
2281 : *first ? N : find_index_i_(first + 1, last, N + 1);
2282 }
2283
2284 template <typename L, typename T>
2285 struct find_index_
2286 {
2287 };
2288
2289 template <typename V>
2290 struct find_index_<list<>, V>
2291 {
2292 using type = npos;
2293 };
2294
2295 template <typename... T, typename V>
2296 struct find_index_<list<T...>, V>
2297 {
2298 #ifdef META_WORKAROUND_LLVM_28385
2299 static constexpr bool s_v[sizeof...(T)] = {META_IS_SAME(T, V)...};
2300 #else
2301 static constexpr bool s_v[] = {META_IS_SAME(T, V)...};
2302 #endif
2303 using type = size_t<find_index_i_(s_v, s_v + sizeof...(T))>;
2304 };
2305 } // namespace detail
2306 /// \endcond
2307
2308 /// Finds the index of the first occurrence of the type \p T within the list \p L.
2309 /// Returns `#meta::npos` if the type \p T was not found.
2310 /// \par Complexity
2311 /// `O(N)`.
2312 /// \ingroup query
2313 /// \sa `meta::npos`
2314 template <META_TYPE_CONSTRAINT(list_like) L, typename T>
2315 using find_index = _t<detail::find_index_<L, T>>;
2316
2317 namespace lazy
2318 {
2319 /// \sa 'meta::find_index'
2320 /// \ingroup lazy_query
2321 template <typename L, typename T>
2322 using find_index = defer<find_index, L, T>;
2323 } // namespace lazy
2324
2325 ///////////////////////////////////////////////////////////////////////////////////////////
2326 // reverse_find_index
2327 /// \cond
2328 namespace detail
2329 {
2330 // With thanks to Peter Dimov:
2331 constexpr std::size_t reverse_find_index_i_(bool const *const first,
2332 bool const *const last, std::size_t N)
2333 {
2334 return first == last
2335 ? npos::value
2336 : *(last - 1) ? N - 1 : reverse_find_index_i_(first, last - 1, N - 1);
2337 }
2338
2339 template <typename L, typename T>
2340 struct reverse_find_index_
2341 {
2342 };
2343
2344 template <typename V>
2345 struct reverse_find_index_<list<>, V>
2346 {
2347 using type = npos;
2348 };
2349
2350 template <typename... T, typename V>
2351 struct reverse_find_index_<list<T...>, V>
2352 {
2353 #ifdef META_WORKAROUND_LLVM_28385
2354 static constexpr bool s_v[sizeof...(T)] = {META_IS_SAME(T, V)...};
2355 #else
2356 static constexpr bool s_v[] = {META_IS_SAME(T, V)...};
2357 #endif
2358 using type = size_t<reverse_find_index_i_(s_v, s_v + sizeof...(T), sizeof...(T))>;
2359 };
2360 } // namespace detail
2361 /// \endcond
2362
2363 /// Finds the index of the last occurrence of the type \p T within the
2364 /// list \p L. Returns `#meta::npos` if the type \p T was not found.
2365 /// \par Complexity
2366 /// `O(N)`.
2367 /// \ingroup query
2368 /// \sa `#meta::npos`
2369 template <META_TYPE_CONSTRAINT(list_like) L, typename T>
2370 using reverse_find_index = _t<detail::reverse_find_index_<L, T>>;
2371
2372 namespace lazy
2373 {
2374 /// \sa 'meta::reverse_find_index'
2375 /// \ingroup lazy_query
2376 template <typename L, typename T>
2377 using reverse_find_index = defer<reverse_find_index, L, T>;
2378 } // namespace lazy
2379
2380 ////////////////////////////////////////////////////////////////////////////////////
2381 // find
2382 /// Return the tail of the list \p L starting at the first occurrence of
2383 /// \p T, if any such element exists; the empty list, otherwise.
2384 /// \par Complexity
2385 /// `O(N)`.
2386 /// \ingroup query
2387 template <META_TYPE_CONSTRAINT(list_like) L, typename T>
2388 using find = drop<L, min<find_index<L, T>, size<L>>>;
2389
2390 namespace lazy
2391 {
2392 /// \sa 'meta::find'
2393 /// \ingroup lazy_query
2394 template <typename L, typename T>
2395 using find = defer<find, L, T>;
2396 } // namespace lazy
2397
2398 ////////////////////////////////////////////////////////////////////////////////////
2399 // reverse_find
2400 /// \cond
2401 namespace detail
2402 {
2403 template <typename L, typename T, typename State = list<>>
2404 struct reverse_find_
2405 {
2406 };
2407
2408 template <typename T, typename State>
2409 struct reverse_find_<list<>, T, State>
2410 {
2411 using type = State;
2412 };
2413
2414 template <typename Head, typename... L, typename T, typename State>
2415 struct reverse_find_<list<Head, L...>, T, State> : reverse_find_<list<L...>, T, State>
2416 {
2417 };
2418
2419 template <typename... L, typename T, typename State>
2420 struct reverse_find_<list<T, L...>, T, State>
2421 : reverse_find_<list<L...>, T, list<T, L...>>
2422 {
2423 };
2424 } // namespace detail
2425 /// \endcond
2426
2427 /// Return the tail of the list \p L starting at the last occurrence of \p T, if any such
2428 /// element exists; the empty list, otherwise.
2429 /// \par Complexity
2430 /// `O(N)`.
2431 /// \ingroup query
2432 template <META_TYPE_CONSTRAINT(list_like) L, typename T>
2433 using reverse_find = drop<L, min<reverse_find_index<L, T>, size<L>>>;
2434
2435 namespace lazy
2436 {
2437 /// \sa 'meta::rfind'
2438 /// \ingroup lazy_query
2439 template <typename L, typename T>
2440 using reverse_find = defer<reverse_find, L, T>;
2441 } // namespace lazy
2442
2443 ///////////////////////////////////////////////////////////////////////////////////////////
2444 // find_if
2445 /// \cond
2446 namespace detail
2447 {
2448 #ifdef META_CONCEPT
2449 template <typename L, typename Fn>
2450 struct find_if_
2451 {
2452 };
2453
2454 template <typename Fn>
2455 struct find_if_<list<>, Fn>
2456 {
2457 using type = list<>;
2458 };
2459
2460 template <typename Head, typename... L, typename Fn>
2461 requires integral<invoke<Fn, Head>>
2462 struct find_if_<list<Head, L...>, Fn>
2463 : if_<invoke<Fn, Head>, id<list<Head, L...>>, find_if_<list<L...>, Fn>>
2464 {
2465 };
2466 #else
2467 constexpr bool const *find_if_i_(bool const *const begin, bool const *const end)
2468 {
2469 return begin == end || *begin ? begin : find_if_i_(begin + 1, end);
2470 }
2471
2472 template <typename L, typename Fn, typename = void>
2473 struct find_if_
2474 {
2475 };
2476
2477 template <typename Fn>
2478 struct find_if_<list<>, Fn>
2479 {
2480 using type = list<>;
2481 };
2482
2483 template <typename... L, typename Fn>
2484 struct find_if_<list<L...>, Fn,
2485 void_<integer_sequence<bool, bool(invoke<Fn, L>::type::value)...>>>
2486 {
2487 #ifdef META_WORKAROUND_LLVM_28385
2488 static constexpr bool s_v[sizeof...(L)] = {invoke<Fn, L>::type::value...};
2489 #else
2490 static constexpr bool s_v[] = {invoke<Fn, L>::type::value...};
2491 #endif
2492 using type =
2493 drop_c<list<L...>, detail::find_if_i_(s_v, s_v + sizeof...(L)) - s_v>;
2494 };
2495 #endif
2496 } // namespace detail
2497 /// \endcond
2498
2499 /// Return the tail of the list \p L starting at the first element `A`
2500 /// such that `invoke<Fn, A>::%value` is \c true, if any such element
2501 /// exists; the empty list, otherwise.
2502 /// \par Complexity
2503 /// `O(N)`.
2504 /// \ingroup query
2505 template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
2506 using find_if = _t<detail::find_if_<L, Fn>>;
2507
2508 namespace lazy
2509 {
2510 /// \sa 'meta::find_if'
2511 /// \ingroup lazy_query
2512 template <typename L, typename Fn>
2513 using find_if = defer<find_if, L, Fn>;
2514 } // namespace lazy
2515
2516 ////////////////////////////////////////////////////////////////////////////////////
2517 // reverse_find_if
2518 /// \cond
2519 namespace detail
2520 {
2521 #ifdef META_CONCEPT
2522 template <typename L, typename Fn, typename State = list<>>
2523 struct reverse_find_if_
2524 {
2525 };
2526
2527 template <typename Fn, typename State>
2528 struct reverse_find_if_<list<>, Fn, State>
2529 {
2530 using type = State;
2531 };
2532
2533 template <typename Head, typename... L, typename Fn, typename State>
2534 requires integral<invoke<Fn, Head>>
2535 struct reverse_find_if_<list<Head, L...>, Fn, State>
2536 : reverse_find_if_<list<L...>, Fn, if_<invoke<Fn, Head>, list<Head, L...>, State>>
2537 {
2538 };
2539 #else
2540 constexpr bool const *reverse_find_if_i_(bool const *const begin, bool const *const pos,
2541 bool const *const end)
2542 {
2543 return begin == pos
2544 ? end
2545 : *(pos - 1) ? pos - 1 : reverse_find_if_i_(begin, pos - 1, end);
2546 }
2547
2548 template <typename L, typename Fn, typename = void>
2549 struct reverse_find_if_
2550 {
2551 };
2552
2553 template <typename Fn>
2554 struct reverse_find_if_<list<>, Fn>
2555 {
2556 using type = list<>;
2557 };
2558
2559 template <typename... L, typename Fn>
2560 struct reverse_find_if_<
2561 list<L...>, Fn,
2562 void_<integer_sequence<bool, bool(invoke<Fn, L>::type::value)...>>>
2563 {
2564 #ifdef META_WORKAROUND_LLVM_28385
2565 static constexpr bool s_v[sizeof...(L)] = {invoke<Fn, L>::type::value...};
2566 #else
2567 static constexpr bool s_v[] = {invoke<Fn, L>::type::value...};
2568 #endif
2569 using type =
2570 drop_c<list<L...>, detail::reverse_find_if_i_(s_v, s_v + sizeof...(L),
2571 s_v + sizeof...(L)) -
2572 s_v>;
2573 };
2574 #endif
2575 } // namespace detail
2576 /// \endcond
2577
2578 /// Return the tail of the list \p L starting at the last element `A`
2579 /// such that `invoke<Fn, A>::%value` is \c true, if any such element
2580 /// exists; the empty list, otherwise.
2581 /// \par Complexity
2582 /// `O(N)`.
2583 /// \ingroup query
2584 template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
2585 using reverse_find_if = _t<detail::reverse_find_if_<L, Fn>>;
2586
2587 namespace lazy
2588 {
2589 /// \sa 'meta::rfind_if'
2590 /// \ingroup lazy_query
2591 template <typename L, typename Fn>
2592 using reverse_find_if = defer<reverse_find_if, L, Fn>;
2593 } // namespace lazy
2594
2595 ///////////////////////////////////////////////////////////////////////////////////////////
2596 // replace
2597 /// \cond
2598 namespace detail
2599 {
2600 template <typename L, typename T, typename U>
2601 struct replace_
2602 {
2603 };
2604
2605 template <typename... L, typename T, typename U>
2606 struct replace_<list<L...>, T, U>
2607 {
2608 using type = list<if_c<META_IS_SAME(T, L), U, L>...>;
2609 };
2610 } // namespace detail
2611 /// \endcond
2612
2613 /// Return a new \c meta::list where all instances of type \p T have
2614 /// been replaced with \p U.
2615 /// \par Complexity
2616 /// `O(N)`.
2617 /// \ingroup transformation
2618 template <META_TYPE_CONSTRAINT(list_like) L, typename T, typename U>
2619 using replace = _t<detail::replace_<L, T, U>>;
2620
2621 namespace lazy
2622 {
2623 /// \sa 'meta::replace'
2624 /// \ingroup lazy_transformation
2625 template <typename L, typename T, typename U>
2626 using replace = defer<replace, T, U>;
2627 } // namespace lazy
2628
2629 ///////////////////////////////////////////////////////////////////////////////////////////
2630 // replace_if
2631 /// \cond
2632 namespace detail
2633 {
2634 #ifdef META_CONCEPT
2635 template <typename L, typename C, typename U>
2636 struct replace_if_
2637 {
2638 };
2639
2640 template <typename... L, typename C, typename U>
2641 requires and_v<integral<invoke<C, L>>...>
2642 struct replace_if_<list<L...>, C, U>
2643 {
2644 using type = list<if_<invoke<C, L>, U, L>...>;
2645 };
2646 #else
2647 template <typename L, typename C, typename U, typename = void>
2648 struct replace_if_
2649 {
2650 };
2651
2652 template <typename... L, typename C, typename U>
2653 struct replace_if_<list<L...>, C, U,
2654 void_<integer_sequence<bool, bool(invoke<C, L>::type::value)...>>>
2655 {
2656 using type = list<if_<invoke<C, L>, U, L>...>;
2657 };
2658 #endif
2659 } // namespace detail
2660 /// \endcond
2661
2662 /// Return a new \c meta::list where all elements \c A of the list \p L
2663 /// for which `invoke<C,A>::%value` is \c true have been replaced with
2664 /// \p U.
2665 /// \par Complexity
2666 /// `O(N)`.
2667 /// \ingroup transformation
2668 template <META_TYPE_CONSTRAINT(list_like) L, typename C, typename U>
2669 using replace_if = _t<detail::replace_if_<L, C, U>>;
2670
2671 namespace lazy
2672 {
2673 /// \sa 'meta::replace_if'
2674 /// \ingroup lazy_transformation
2675 template <typename L, typename C, typename U>
2676 using replace_if = defer<replace_if, C, U>;
2677 } // namespace lazy
2678
2679 ///////////////////////////////////////////////////////////////////////////////////////
2680 // count
2681 namespace detail
2682 {
2683 template <typename, typename>
2684 struct count_
2685 {
2686 };
2687
2688 #if (defined(META_CONCEPT) || META_CXX_VARIABLE_TEMPLATES) && META_CXX_FOLD_EXPRESSIONS
2689 template <typename... Ts, typename T>
2690 struct count_<list<Ts...>, T>
2691 {
2692 using type = meta::size_t<((std::size_t)META_IS_SAME(T, Ts) + ...)>;
2693 };
2694 #else
2695 constexpr std::size_t count_i_(bool const *const begin, bool const *const end,
2696 std::size_t n)
2697 {
2698 return begin == end ? n : detail::count_i_(begin + 1, end, n + *begin);
2699 }
2700
2701 template <typename T>
2702 struct count_<list<>, T>
2703 {
2704 using type = meta::size_t<0>;
2705 };
2706
2707 template <typename... L, typename T>
2708 struct count_<list<L...>, T>
2709 {
2710 #ifdef META_WORKAROUND_LLVM_28385
2711 static constexpr bool s_v[sizeof...(L)] = {META_IS_SAME(T, L)...};
2712 #else
2713 static constexpr bool s_v[] = {META_IS_SAME(T, L)...};
2714 #endif
2715 using type = meta::size_t<detail::count_i_(s_v, s_v + sizeof...(L), 0u)>;
2716 };
2717 #endif
2718 } // namespace detail
2719
2720 /// Count the number of times a type \p T appears in the list \p L.
2721 /// \par Complexity
2722 /// `O(N)`.
2723 /// \ingroup query
2724 template <META_TYPE_CONSTRAINT(list_like) L, typename T>
2725 using count = _t<detail::count_<L, T>>;
2726
2727 namespace lazy
2728 {
2729 /// \sa `meta::count`
2730 /// \ingroup lazy_query
2731 template <typename L, typename T>
2732 using count = defer<count, L, T>;
2733 } // namespace lazy
2734
2735 ///////////////////////////////////////////////////////////////////////////////////////
2736 // count_if
2737 namespace detail
2738 {
2739 #if defined(META_CONCEPT) && META_CXX_FOLD_EXPRESSIONS
2740 template <typename, typename>
2741 struct count_if_
2742 {
2743 };
2744
2745 template <typename... Ts, typename Fn>
2746 requires (integral<invoke<Fn, Ts>> && ...)
2747 struct count_if_<list<Ts...>, Fn>
2748 {
2749 using type = meta::size_t<((std::size_t)(bool)_v<invoke<Fn, Ts>> + ...)>;
2750 };
2751 #else
2752 template <typename L, typename Fn, typename = void>
2753 struct count_if_
2754 {
2755 };
2756
2757 template <typename Fn>
2758 struct count_if_<list<>, Fn>
2759 {
2760 using type = meta::size_t<0>;
2761 };
2762
2763 template <typename... L, typename Fn>
2764 struct count_if_<list<L...>, Fn,
2765 void_<integer_sequence<bool, bool(invoke<Fn, L>::type::value)...>>>
2766 {
2767 #if META_CXX_FOLD_EXPRESSIONS
2768 using type = meta::size_t<((std::size_t)(bool)invoke<Fn, L>::type::value + ...)>;
2769 #else
2770 #ifdef META_WORKAROUND_LLVM_28385
2771 static constexpr bool s_v[sizeof...(L)] = {invoke<Fn, L>::type::value...};
2772 #else
2773 static constexpr bool s_v[] = {invoke<Fn, L>::type::value...};
2774 #endif
2775 using type = meta::size_t<detail::count_i_(s_v, s_v + sizeof...(L), 0u)>;
2776 #endif // META_CXX_FOLD_EXPRESSIONS
2777 };
2778 #endif // META_CONCEPT
2779 } // namespace detail
2780
2781 /// Count the number of times the predicate \p Fn evaluates to true for all the elements in
2782 /// the list \p L.
2783 /// \par Complexity
2784 /// `O(N)`.
2785 /// \ingroup query
2786 template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
2787 using count_if = _t<detail::count_if_<L, Fn>>;
2788
2789 namespace lazy
2790 {
2791 /// \sa `meta::count_if`
2792 /// \ingroup lazy_query
2793 template <typename L, typename Fn>
2794 using count_if = defer<count_if, L, Fn>;
2795 } // namespace lazy
2796
2797 ///////////////////////////////////////////////////////////////////////////////////////////
2798 // filter
2799 /// \cond
2800 namespace detail
2801 {
2802 template <typename Pred>
2803 struct filter_
2804 {
2805 template <typename A>
2806 using invoke = if_c<invoke<Pred, A>::type::value, list<A>, list<>>;
2807 };
2808 } // namespace detail
2809 /// \endcond
2810
2811 /// Returns a new meta::list where only those elements of \p L that satisfy the
2812 /// Callable \p Pred such that `invoke<Pred,A>::%value` is \c true are present.
2813 /// That is, those elements that don't satisfy the \p Pred are "removed".
2814 /// \par Complexity
2815 /// `O(N)`.
2816 /// \ingroup transformation
2817 template <typename L, typename Pred>
2818 using filter = join<transform<L, detail::filter_<Pred>>>;
2819
2820 namespace lazy
2821 {
2822 /// \sa 'meta::filter'
2823 /// \ingroup lazy_transformation
2824 template <typename L, typename Fn>
2825 using filter = defer<filter, L, Fn>;
2826 } // namespace lazy
2827
2828 ///////////////////////////////////////////////////////////////////////////////////////////
2829 // static_const
2830 ///\cond
2831 namespace detail
2832 {
2833 template <typename T>
2834 struct static_const
2835 {
2836 static constexpr T value{};
2837 };
2838
2839 // Avoid potential ODR violations with global objects:
2840 template <typename T>
2841 constexpr T static_const<T>::value;
2842 } // namespace detail
2843
2844 ///\endcond
2845
2846 ///////////////////////////////////////////////////////////////////////////////////////////
2847 // for_each
2848 /// \cond
2849 namespace detail
2850 {
2851 struct for_each_fn
2852 {
2853 template <class Fn, class... Args>
2854 constexpr auto operator()(list<Args...>, Fn f) const -> Fn
2855 {
2856 return (void)std::initializer_list<int>{((void)f(Args{}), 0)...}, f;
2857 }
2858 };
2859 } // namespace detail
2860 /// \endcond
2861
2862 #if META_CXX_INLINE_VARIABLES
2863 /// `for_each(L, Fn)` calls the \p Fn for each
2864 /// argument in the \p L.
2865 /// \ingroup runtime
2866 inline constexpr detail::for_each_fn for_each{};
2867 #else
2868 ///\cond
2869 namespace
2870 {
2871 /// \endcond
2872
2873 /// `for_each(List, UnaryFunction)` calls the \p UnaryFunction for each
2874 /// argument in the \p List.
2875 /// \ingroup runtime
2876 constexpr auto &&for_each = detail::static_const<detail::for_each_fn>::value;
2877
2878 /// \cond
2879 }
2880 /// \endcond
2881 #endif
2882
2883 ///////////////////////////////////////////////////////////////////////////////////////////
2884 // transpose
2885 /// Given a list of lists of types \p ListOfLists, transpose the elements from the lists.
2886 /// \par Complexity
2887 /// `O(N * M)`, where `N` is the size of the outer list, and
2888 /// `M` is the size of the inner lists.
2889 /// \ingroup transformation
2890 template <META_TYPE_CONSTRAINT(list_like) ListOfLists>
2891 using transpose = fold<ListOfLists, repeat_n<size<front<ListOfLists>>, list<>>,
2892 bind_back<quote<transform>, quote<push_back>>>;
2893
2894 namespace lazy
2895 {
2896 /// \sa 'meta::transpose'
2897 /// \ingroup lazy_transformation
2898 template <typename ListOfLists>
2899 using transpose = defer<transpose, ListOfLists>;
2900 } // namespace lazy
2901
2902 ///////////////////////////////////////////////////////////////////////////////////////////
2903 // zip_with
2904 /// Given a list of lists of types \p ListOfLists and an invocable \p Fn, construct a new
2905 /// list by calling \p Fn with the elements from the lists pairwise.
2906 /// \par Complexity
2907 /// `O(N * M)`, where `N` is the size of the outer list, and
2908 /// `M` is the size of the inner lists.
2909 /// \ingroup transformation
2910 template <META_TYPE_CONSTRAINT(invocable) Fn, META_TYPE_CONSTRAINT(list_like) ListOfLists>
2911 using zip_with = transform<transpose<ListOfLists>, uncurry<Fn>>;
2912
2913 namespace lazy
2914 {
2915 /// \sa 'meta::zip_with'
2916 /// \ingroup lazy_transformation
2917 template <typename Fn, typename ListOfLists>
2918 using zip_with = defer<zip_with, Fn, ListOfLists>;
2919 } // namespace lazy
2920
2921 ///////////////////////////////////////////////////////////////////////////////////////////
2922 // zip
2923 /// Given a list of lists of types \p ListOfLists, construct a new list by grouping the
2924 /// elements from the lists pairwise into `meta::list`s.
2925 /// \par Complexity
2926 /// `O(N * M)`, where `N` is the size of the outer list, and `M`
2927 /// is the size of the inner lists.
2928 /// \ingroup transformation
2929 template <META_TYPE_CONSTRAINT(list_like) ListOfLists>
2930 using zip = transpose<ListOfLists>;
2931
2932 namespace lazy
2933 {
2934 /// \sa 'meta::zip'
2935 /// \ingroup lazy_transformation
2936 template <typename ListOfLists>
2937 using zip = defer<zip, ListOfLists>;
2938 } // namespace lazy
2939
2940 ///////////////////////////////////////////////////////////////////////////////////////////
2941 // as_list
2942 /// \cond
2943 namespace detail
2944 {
2945 template <typename T>
2946 using uncvref_t = _t<std::remove_cv<_t<std::remove_reference<T>>>>;
2947
2948 // Indirection here needed to avoid Core issue 1430
2949 // https://wg21.link/cwg1430
2950 template <typename Sequence>
2951 struct as_list_ : lazy::invoke<uncurry<quote<list>>, Sequence>
2952 {
2953 };
2954 } // namespace detail
2955 /// \endcond
2956
2957 /// Turn a type into an instance of \c meta::list in a way determined by
2958 /// \c meta::apply.
2959 /// \ingroup list
2960 template <typename Sequence>
2961 using as_list = _t<detail::as_list_<detail::uncvref_t<Sequence>>>;
2962
2963 namespace lazy
2964 {
2965 /// \sa 'meta::as_list'
2966 /// \ingroup lazy_list
2967 template <typename Sequence>
2968 using as_list = defer<as_list, Sequence>;
2969 } // namespace lazy
2970
2971 ///////////////////////////////////////////////////////////////////////////////////////////
2972 // reverse
2973 /// \cond
2974 namespace detail
2975 {
2976 template <typename L, typename State = list<>>
2977 struct reverse_ : lazy::fold<L, State, quote<push_front>>
2978 {
2979 };
2980
2981 template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
2982 typename T6, typename T7, typename T8, typename T9, typename... Ts,
2983 typename... Us>
2984 struct reverse_<list<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, Ts...>, list<Us...>>
2985 : reverse_<list<Ts...>, list<T9, T8, T7, T6, T5, T4, T3, T2, T1, T0, Us...>>
2986 {
2987 };
2988 }
2989 /// \endcond
2990
2991 /// Return a new \c meta::list by reversing the elements in the list \p L.
2992 /// \par Complexity
2993 /// `O(N)`.
2994 /// \ingroup transformation
2995 template <META_TYPE_CONSTRAINT(list_like) L>
2996 using reverse = _t<detail::reverse_<L>>;
2997
2998 namespace lazy
2999 {
3000 /// \sa 'meta::reverse'
3001 /// \ingroup lazy_transformation
3002 template <typename L>
3003 using reverse = defer<reverse, L>;
3004 } // namespace lazy
3005
3006 /// Logically negate the result of invocable \p Fn.
3007 /// \ingroup trait
3008 template <META_TYPE_CONSTRAINT(invocable) Fn>
3009 using not_fn = compose<quote<not_>, Fn>;
3010
3011 namespace lazy
3012 {
3013 /// \sa 'meta::not_fn'
3014 /// \ingroup lazy_trait
3015 template <typename Fn>
3016 using not_fn = defer<not_fn, Fn>;
3017 } // namespace lazy
3018
3019 ///////////////////////////////////////////////////////////////////////////////////////////
3020 // all_of
3021 /// A Boolean integral constant wrapper around \c true if `invoke<Fn, A>::%value` is \c true
3022 /// for all elements \c A in \c meta::list \p L; \c false, otherwise.
3023 /// \par Complexity
3024 /// `O(N)`.
3025 /// \ingroup query
3026 template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
3027 using all_of = empty<find_if<L, not_fn<Fn>>>;
3028
3029 namespace lazy
3030 {
3031 /// \sa 'meta::all_of'
3032 /// \ingroup lazy_query
3033 template <typename L, typename Fn>
3034 using all_of = defer<all_of, L, Fn>;
3035 } // namespace lazy
3036
3037 ///////////////////////////////////////////////////////////////////////////////////////////
3038 // any_of
3039 /// A Boolean integral constant wrapper around \c true if `invoke<Fn, A>::%value` is
3040 /// \c true for any element \c A in \c meta::list \p L; \c false, otherwise.
3041 /// \par Complexity
3042 /// `O(N)`.
3043 /// \ingroup query
3044 template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
3045 using any_of = not_<empty<find_if<L, Fn>>>;
3046
3047 namespace lazy
3048 {
3049 /// \sa 'meta::any_of'
3050 /// \ingroup lazy_query
3051 template <typename L, typename Fn>
3052 using any_of = defer<any_of, L, Fn>;
3053 } // namespace lazy
3054
3055 ///////////////////////////////////////////////////////////////////////////////////////////
3056 // none_of
3057 /// A Boolean integral constant wrapper around \c true if `invoke<Fn, A>::%value` is
3058 /// \c false for all elements \c A in \c meta::list \p L; \c false, otherwise.
3059 /// \par Complexity
3060 /// `O(N)`.
3061 /// \ingroup query
3062 template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
3063 using none_of = empty<find_if<L, Fn>>;
3064
3065 namespace lazy
3066 {
3067 /// \sa 'meta::none_of'
3068 /// \ingroup lazy_query
3069 template <typename L, META_TYPE_CONSTRAINT(invocable) Fn>
3070 using none_of = defer<none_of, L, Fn>;
3071 } // namespace lazy
3072
3073 ///////////////////////////////////////////////////////////////////////////////////////////
3074 // in
3075 /// A Boolean integral constant wrapper around \c true if there is at least one occurrence
3076 /// of \p T in \p L.
3077 /// \par Complexity
3078 /// `O(N)`.
3079 /// \ingroup query
3080 template <META_TYPE_CONSTRAINT(list_like) L, typename T>
3081 using in = not_<empty<find<L, T>>>;
3082
3083 namespace lazy
3084 {
3085 /// \sa 'meta::in'
3086 /// \ingroup lazy_query
3087 template <typename L, typename T>
3088 using in = defer<in, L, T>;
3089 } // namespace lazy
3090
3091 ///////////////////////////////////////////////////////////////////////////////////////////
3092 // inherit
3093 /// \cond
3094 namespace detail
3095 {
3096 template <typename L>
3097 struct inherit_
3098 {
3099 };
3100
3101 template <typename... L>
3102 struct inherit_<list<L...>> : L...
3103 {
3104 using type = inherit_;
3105 };
3106 } // namespace detail
3107 /// \endcond
3108
3109 /// A type that inherits from all the types in the list
3110 /// \pre The types in the list must be unique
3111 /// \pre All the types in the list must be non-final class types
3112 /// \ingroup datatype
3113 template <META_TYPE_CONSTRAINT(list_like) L>
3114 using inherit = meta::_t<detail::inherit_<L>>;
3115
3116 namespace lazy
3117 {
3118 /// \sa 'meta::inherit'
3119 /// \ingroup lazy_datatype
3120 template <typename L>
3121 using inherit = defer<inherit, L>;
3122 } // namespace lazy
3123
3124 ///////////////////////////////////////////////////////////////////////////////////////////
3125 // unique
3126 /// \cond
3127 namespace detail
3128 {
3129 template <typename Set, typename T>
3130 struct in_
3131 {
3132 };
3133
3134 template <typename... Set, typename T>
3135 struct in_<list<Set...>, T> : bool_<META_IS_BASE_OF(id<T>, inherit<list<id<Set>...>>)>
3136 {
3137 };
3138
3139 template <typename Set, typename T>
3140 struct insert_back_
3141 {
3142 };
3143
3144 template <typename... Set, typename T>
3145 struct insert_back_<list<Set...>, T>
3146 {
3147 using type = if_<in_<list<Set...>, T>, list<Set...>, list<Set..., T>>;
3148 };
3149 } // namespace detail
3150 /// \endcond
3151
3152 /// Return a new \c meta::list where all duplicate elements have been removed.
3153 /// \par Complexity
3154 /// `O(N^2)`.
3155 /// \ingroup transformation
3156 template <META_TYPE_CONSTRAINT(list_like) L>
3157 using unique = fold<L, list<>, quote_trait<detail::insert_back_>>;
3158
3159 namespace lazy
3160 {
3161 /// \sa 'meta::unique'
3162 /// \ingroup lazy_transformation
3163 template <typename L>
3164 using unique = defer<unique, L>;
3165 } // namespace lazy
3166
3167 ///////////////////////////////////////////////////////////////////////////////////////////
3168 // partition
3169 /// \cond
3170 namespace detail
3171 {
3172 template <typename Fn>
3173 struct partition_
3174 {
3175 #ifdef META_CONCEPT
3176 template <typename, typename>
3177 #else
3178 template <typename, typename, typename = void>
3179 #endif
3180 struct impl
3181 {
3182 };
3183 template <typename... Yes, typename... No, typename A>
3184 #ifdef META_CONCEPT
3185 requires integral<invoke<Fn, A>>
3186 struct impl<pair<list<Yes...>, list<No...>>, A>
3187 #else
3188 struct impl<pair<list<Yes...>, list<No...>>, A,
3189 void_<bool_<invoke<Fn, A>::type::value>>>
3190 #endif
3191 {
3192 using type = if_<meta::invoke<Fn, A>, pair<list<Yes..., A>, list<No...>>,
3193 pair<list<Yes...>, list<No..., A>>>;
3194 };
3195
3196 template <typename State, typename A>
3197 using invoke = _t<impl<State, A>>;
3198 };
3199 } // namespace detail
3200 /// \endcond
3201
3202 /// Returns a pair of lists, where the elements of \p L that satisfy the
3203 /// invocable \p Fn such that `invoke<Fn,A>::%value` is \c true are present in the
3204 /// first list and the rest are in the second.
3205 /// \par Complexity
3206 /// `O(N)`.
3207 /// \ingroup transformation
3208 template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
3209 using partition = fold<L, pair<list<>, list<>>, detail::partition_<Fn>>;
3210
3211 namespace lazy
3212 {
3213 /// \sa 'meta::partition'
3214 /// \ingroup lazy_transformation
3215 template <typename L, typename Fn>
3216 using partition = defer<partition, L, Fn>;
3217 } // namespace lazy
3218
3219 ///////////////////////////////////////////////////////////////////////////////////////////
3220 // sort
3221 /// \cond
3222 namespace detail
3223 {
3224 template <META_TYPE_CONSTRAINT(invocable) Fn, typename A, typename B, typename... Ts>
3225 using part_ = partition<list<B, Ts...>, bind_back<Fn, A>>;
3226 #ifdef META_CONCEPT
3227 template <typename L, typename Fn>
3228 requires list_like<L> && invocable<Fn>
3229 #else
3230 template <typename, typename, typename = void>
3231 #endif
3232 struct sort_
3233 {
3234 };
3235 template <typename Fn>
3236 struct sort_<list<>, Fn>
3237 {
3238 using type = list<>;
3239 };
3240
3241 template <typename A, typename Fn>
3242 struct sort_<list<A>, Fn>
3243 {
3244 using type = list<A>;
3245 };
3246
3247 template <typename A, typename B, typename... Ts, typename Fn>
3248 #ifdef META_CONCEPT
3249 requires trait<sort_<first<part_<Fn, A, B, Ts...>>, Fn>> &&
3250 trait<sort_<second<part_<Fn, A, B, Ts...>>, Fn>>
3251 struct sort_<list<A, B, Ts...>, Fn>
3252 #else
3253 struct sort_<
3254 list<A, B, Ts...>, Fn,
3255 void_<_t<sort_<first<part_<Fn, A, B, Ts...>>, Fn>>>>
3256 #endif
3257 {
3258 using P = part_<Fn, A, B, Ts...>;
3259 using type = concat<_t<sort_<first<P>, Fn>>, list<A>, _t<sort_<second<P>, Fn>>>;
3260 };
3261 } // namespace detail
3262 /// \endcond
3263
3264 // clang-format off
3265 /// Return a new \c meta::list that is sorted according to invocable predicate \p Fn.
3266 /// \par Complexity
3267 /// Expected: `O(N log N)`
3268 /// Worst case: `O(N^2)`.
3269 /// \code
3270 /// using L0 = list<char[5], char[3], char[2], char[6], char[1], char[5], char[10]>;
3271 /// using L1 = meta::sort<L0, lambda<_a, _b, lazy::less<lazy::sizeof_<_a>, lazy::sizeof_<_b>>>>;
3272 /// static_assert(std::is_same_v<L1, list<char[1], char[2], char[3], char[5], char[5], char[6], char[10]>>, "");
3273 /// \endcode
3274 /// \ingroup transformation
3275 // clang-format on
3276 template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
3277 using sort = _t<detail::sort_<L, Fn>>;
3278
3279 namespace lazy
3280 {
3281 /// \sa 'meta::sort'
3282 /// \ingroup lazy_transformation
3283 template <typename L, typename Fn>
3284 using sort = defer<sort, L, Fn>;
3285 } // namespace lazy
3286
3287 ////////////////////////////////////////////////////////////////////////////
3288 // lambda_
3289 /// \cond
3290 namespace detail
3291 {
3292 template <typename T, int = 0>
3293 struct protect_;
3294
3295 template <typename, int = 0>
3296 struct vararg_;
3297
3298 template <typename T, int = 0>
3299 struct is_valid_;
3300
3301 // Returns which branch to evaluate
3302 template <typename If, typename... Ts>
3303 #ifdef META_CONCEPT
3304 using lazy_if_ = lazy::_t<defer<_if_, If, protect_<Ts>...>>;
3305 #else
3306 using lazy_if_ = lazy::_t<defer<_if_, list<If, protect_<Ts>...>>>;
3307 #endif
3308
3309 template <typename A, typename T, typename Fn, typename Ts>
3310 struct subst1_
3311 {
3312 using type = list<list<T>>;
3313 };
3314 template <typename T, typename Fn, typename Ts>
3315 struct subst1_<Fn, T, Fn, Ts>
3316 {
3317 using type = list<>;
3318 };
3319 template <typename A, typename T, typename Fn, typename Ts>
3320 struct subst1_<vararg_<A>, T, Fn, Ts>
3321 {
3322 using type = list<Ts>;
3323 };
3324
3325 template <typename As, typename Ts>
3326 using substitutions_ = push_back<
3327 join<transform<
3328 concat<As, repeat_n_c<size<Ts>{} + 2 - size<As>{}, back<As>>>,
3329 concat<Ts, repeat_n_c<2, back<As>>>,
3330 bind_back<quote_trait<subst1_>, back<As>, drop_c<Ts, size<As>{} - 2>>>>,
3331 list<back<As>>>;
3332
3333 #if 0//def META_CONCEPT
3334 template <list_like As, list_like Ts>
3335 requires (_v<size<Ts>> + 2 >= _v<size<As>>)
3336 using substitutions = substitutions_<As, Ts>;
3337 #else // ^^^ concepts / no concepts vvv
3338 template <typename As, typename Ts>
3339 using substitutions =
3340 #ifdef META_WORKAROUND_MSVC_702792
3341 invoke<if_c<(size<Ts>::value + 2 >= size<As>::value), quote<substitutions_>>, As,
3342 Ts>;
3343 #else // ^^^ workaround ^^^ / vvv no workaround vvv
3344 invoke<if_c<(size<Ts>{} + 2 >= size<As>{}), quote<substitutions_>>, As, Ts>;
3345 #endif // META_WORKAROUND_MSVC_702792
3346 #endif // META_CONCEPT
3347
3348 template <typename T>
3349 struct is_vararg_ : std::false_type
3350 {
3351 };
3352 template <typename T>
3353 struct is_vararg_<vararg_<T>> : std::true_type
3354 {
3355 };
3356
3357 template <META_TYPE_CONSTRAINT(list_like) Tags>
3358 using is_variadic_ = is_vararg_<at<push_front<Tags, void>, dec<size<Tags>>>>;
3359
3360 template <META_TYPE_CONSTRAINT(list_like) Tags, bool IsVariadic = is_variadic_<Tags>::value>
3361 struct lambda_;
3362
3363 // Non-variadic lambda implementation
3364 template <typename... As>
3365 struct lambda_<list<As...>, false>
3366 {
3367 private:
3368 static constexpr std::size_t arity = sizeof...(As) - 1;
3369 using Tags = list<As...>; // Includes the lambda body as the last arg!
3370 using Fn = back<Tags>;
3371 template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
3372 struct impl;
3373 template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
3374 using lazy_impl_ = lazy::_t<defer<impl, T, protect_<Args>>>;
3375 #if 0//def META_CONCEPT
3376 template <typename, list_like>
3377 #else
3378 template <typename, typename, typename = void>
3379 #endif
3380 struct subst_
3381 {
3382 };
3383 template <template <typename...> class C, typename... Ts, typename Args>
3384 #if 0//def META_CONCEPT
3385 requires valid<C, _t<impl<Ts, Args>>...> struct subst_<defer<C, Ts...>, Args>
3386 #else
3387 struct subst_<defer<C, Ts...>, Args, void_<C<_t<impl<Ts, Args>>...>>>
3388 #endif
3389 {
3390 using type = C<_t<impl<Ts, Args>>...>;
3391 };
3392 template <typename T, template <T...> class C, T... Is, typename Args>
3393 #if 0//def META_CONCEPT
3394 requires valid_i<T, C, Is...> struct subst_<defer_i<T, C, Is...>, Args>
3395 #else
3396 struct subst_<defer_i<T, C, Is...>, Args, void_<C<Is...>>>
3397 #endif
3398 {
3399 using type = C<Is...>;
3400 };
3401 template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
3402 struct impl : if_c<(reverse_find_index<Tags, T>() != npos()),
3403 lazy::at<Args, reverse_find_index<Tags, T>>, id<T>>
3404 {
3405 };
3406 template <typename T, typename Args>
3407 struct impl<protect_<T>, Args>
3408 {
3409 using type = T;
3410 };
3411 template <typename T, typename Args>
3412 struct impl<is_valid_<T>, Args>
3413 {
3414 using type = is_trait<impl<T, Args>>;
3415 };
3416 template <typename If, typename... Ts, typename Args>
3417 struct impl<defer<if_, If, Ts...>, Args> // Short-circuit if_
3418 : impl<lazy_impl_<lazy_if_<If, Ts...>, Args>, Args>
3419 {
3420 };
3421 template <typename B, typename... Bs, typename Args>
3422 struct impl<defer<and_, B, Bs...>, Args> // Short-circuit and_
3423 : impl<lazy_impl_<lazy_if_<B, lazy::and_<Bs...>, protect_<std::false_type>>, Args>,
3424 Args>
3425 {
3426 };
3427 template <typename B, typename... Bs, typename Args>
3428 struct impl<defer<or_, B, Bs...>, Args> // Short-circuit or_
3429 : impl<lazy_impl_<lazy_if_<B, protect_<std::true_type>, lazy::or_<Bs...>>, Args>,
3430 Args>
3431 {
3432 };
3433 template <template <typename...> class C, typename... Ts, typename Args>
3434 struct impl<defer<C, Ts...>, Args> : subst_<defer<C, Ts...>, Args>
3435 {
3436 };
3437 template <typename T, template <T...> class C, T... Is, typename Args>
3438 struct impl<defer_i<T, C, Is...>, Args> : subst_<defer_i<T, C, Is...>, Args>
3439 {
3440 };
3441 template <template <typename...> class C, typename... Ts, typename Args>
3442 struct impl<C<Ts...>, Args> : subst_<defer<C, Ts...>, Args>
3443 {
3444 };
3445 template <typename... Ts, typename Args>
3446 struct impl<lambda_<list<Ts...>, false>, Args>
3447 {
3448 using type = compose<uncurry<lambda_<list<As..., Ts...>, false>>,
3449 curry<bind_front<quote<concat>, Args>>>;
3450 };
3451 template <typename... Bs, typename Args>
3452 struct impl<lambda_<list<Bs...>, true>, Args>
3453 {
3454 using type = compose<typename lambda_<list<As..., Bs...>, true>::thunk,
3455 bind_front<quote<concat>, transform<Args, quote<list>>>,
3456 curry<bind_front<quote<substitutions>, list<Bs...>>>>;
3457 };
3458
3459 public:
3460 template <typename... Ts>
3461 #ifdef META_CONCEPT
3462 requires (sizeof...(Ts) == arity) using invoke = _t<impl<Fn, list<Ts..., Fn>>>;
3463 #else
3464 using invoke = _t<if_c<sizeof...(Ts) == arity, impl<Fn, list<Ts..., Fn>>>>;
3465 #endif
3466 };
3467
3468 // Lambda with variadic placeholder (broken out due to less efficient compile-time
3469 // resource usage)
3470 template <typename... As>
3471 struct lambda_<list<As...>, true>
3472 {
3473 private:
3474 template <META_TYPE_CONSTRAINT(list_like) T, bool IsVar>
3475 friend struct lambda_;
3476 using Tags = list<As...>; // Includes the lambda body as the last arg!
3477 template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
3478 struct impl;
3479 template <META_TYPE_CONSTRAINT(list_like) Args>
3480 using eval_impl_ = bind_back<quote_trait<impl>, Args>;
3481 template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
3482 using lazy_impl_ = lazy::_t<defer<impl, T, protect_<Args>>>;
3483 template <template <typename...> class C, META_TYPE_CONSTRAINT(list_like) Args,
3484 META_TYPE_CONSTRAINT(list_like) Ts>
3485 using try_subst_ = apply<quote<C>, join<transform<Ts, eval_impl_<Args>>>>;
3486 #if 0//def META_CONCEPT
3487 template <typename, list_like>
3488 #else
3489 template <typename, typename, typename = void>
3490 #endif
3491 struct subst_
3492 {
3493 };
3494 template <template <typename...> class C, typename... Ts, typename Args>
3495 #if 0//def META_CONCEPT
3496 requires is_true<try_subst_<C, Args, list<Ts...>>> struct subst_<defer<C, Ts...>, Args>
3497 #else
3498 struct subst_<defer<C, Ts...>, Args, void_<try_subst_<C, Args, list<Ts...>>>>
3499 #endif
3500 {
3501 using type = list<try_subst_<C, Args, list<Ts...>>>;
3502 };
3503 template <typename T, template <T...> class C, T... Is, typename Args>
3504 #if 0//def META_CONCEPT
3505 requires valid_i<T, C, Is...> struct subst_<defer_i<T, C, Is...>, Args>
3506 #else
3507 struct subst_<defer_i<T, C, Is...>, Args, void_<C<Is...>>>
3508 #endif
3509 {
3510 using type = list<C<Is...>>;
3511 };
3512 template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
3513 struct impl : if_c<(reverse_find_index<Tags, T>() != npos()),
3514 lazy::at<Args, reverse_find_index<Tags, T>>, id<list<T>>>
3515 {
3516 };
3517 template <typename T, typename Args>
3518 struct impl<protect_<T>, Args>
3519 {
3520 using type = list<T>;
3521 };
3522 template <typename T, typename Args>
3523 struct impl<is_valid_<T>, Args>
3524 {
3525 using type = list<is_trait<impl<T, Args>>>;
3526 };
3527 template <typename If, typename... Ts, typename Args>
3528 struct impl<defer<if_, If, Ts...>, Args> // Short-circuit if_
3529 : impl<lazy_impl_<lazy_if_<If, Ts...>, Args>, Args>
3530 {
3531 };
3532 template <typename B, typename... Bs, typename Args>
3533 struct impl<defer<and_, B, Bs...>, Args> // Short-circuit and_
3534 : impl<lazy_impl_<lazy_if_<B, lazy::and_<Bs...>, protect_<std::false_type>>, Args>,
3535 Args>
3536 {
3537 };
3538 template <typename B, typename... Bs, typename Args>
3539 struct impl<defer<or_, B, Bs...>, Args> // Short-circuit or_
3540 : impl<lazy_impl_<lazy_if_<B, protect_<std::true_type>, lazy::or_<Bs...>>, Args>,
3541 Args>
3542 {
3543 };
3544 template <template <typename...> class C, typename... Ts, typename Args>
3545 struct impl<defer<C, Ts...>, Args> : subst_<defer<C, Ts...>, Args>
3546 {
3547 };
3548 template <typename T, template <T...> class C, T... Is, typename Args>
3549 struct impl<defer_i<T, C, Is...>, Args> : subst_<defer_i<T, C, Is...>, Args>
3550 {
3551 };
3552 template <template <typename...> class C, typename... Ts, typename Args>
3553 struct impl<C<Ts...>, Args> : subst_<defer<C, Ts...>, Args>
3554 {
3555 };
3556 template <typename... Bs, bool IsVar, typename Args>
3557 struct impl<lambda_<list<Bs...>, IsVar>, Args>
3558 {
3559 using type =
3560 list<compose<typename lambda_<list<As..., Bs...>, true>::thunk,
3561 bind_front<quote<concat>, Args>,
3562 curry<bind_front<quote<substitutions>, list<Bs...>>>>>;
3563 };
3564 struct thunk
3565 {
3566 template <typename S, typename R = _t<impl<back<Tags>, S>>>
3567 #ifdef META_CONCEPT
3568 requires (_v<size<R>> == 1) using invoke = front<R>;
3569 #else
3570 using invoke = if_c<size<R>{} == 1, front<R>>;
3571 #endif
3572 };
3573
3574 public:
3575 template <typename... Ts>
3576 using invoke = invoke<thunk, substitutions<Tags, list<Ts...>>>;
3577 };
3578 } // namespace detail
3579 /// \endcond
3580
3581 ///////////////////////////////////////////////////////////////////////////////////////////
3582 // lambda
3583 /// For creating anonymous Invocables.
3584 /// \code
3585 /// using L = lambda<_a, _b, std::pair<_b, std::pair<_a, _a>>>;
3586 /// using P = invoke<L, int, short>;
3587 /// static_assert(std::is_same_v<P, std::pair<short, std::pair<int, int>>>, "");
3588 /// \endcode
3589 /// \ingroup trait
3590 template <typename... Ts>
3591 #ifdef META_CONCEPT
3592 requires (sizeof...(Ts) > 0) using lambda = detail::lambda_<list<Ts...>>;
3593 #else
3594 using lambda = if_c<(sizeof...(Ts) > 0), detail::lambda_<list<Ts...>>>;
3595 #endif
3596
3597 ///////////////////////////////////////////////////////////////////////////////////////////
3598 // is_valid
3599 /// For testing whether a deferred computation will succeed in a \c let or a \c lambda.
3600 /// \ingroup trait
3601 template <typename T>
3602 using is_valid = detail::is_valid_<T>;
3603
3604 ///////////////////////////////////////////////////////////////////////////////////////////
3605 // vararg
3606 /// For defining variadic placeholders.
3607 template <typename T>
3608 using vararg = detail::vararg_<T>;
3609
3610 ///////////////////////////////////////////////////////////////////////////////////////////
3611 // protect
3612 /// For preventing the evaluation of a nested `defer`ed computation in a \c let or
3613 /// \c lambda expression.
3614 template <typename T>
3615 using protect = detail::protect_<T>;
3616
3617 ///////////////////////////////////////////////////////////////////////////////////////////
3618 // var
3619 /// For use when defining local variables in \c meta::let expressions
3620 /// \sa `meta::let`
3621 template <typename Tag, typename Value>
3622 struct var;
3623
3624 /// \cond
3625 namespace detail
3626 {
3627 template <typename...>
3628 struct let_
3629 {
3630 };
3631 template <typename Fn>
3632 struct let_<Fn>
3633 {
3634 using type = lazy::invoke<lambda<Fn>>;
3635 };
3636 template <typename Tag, typename Value, typename... Rest>
3637 struct let_<var<Tag, Value>, Rest...>
3638 {
3639 using type = lazy::invoke<lambda<Tag, _t<let_<Rest...>>>, Value>;
3640 };
3641 } // namespace detail
3642 /// \endcond
3643
3644 /// A lexically scoped expression with local variables.
3645 ///
3646 /// \code
3647 /// template <typename T, typename L>
3648 /// using find_index_ = let<
3649 /// var<_a, L>,
3650 /// var<_b, lazy::find<_a, T>>,
3651 /// lazy::if_<
3652 /// std::is_same<_b, list<>>,
3653 /// meta::npos,
3654 /// lazy::minus<lazy::size<_a>, lazy::size<_b>>>>;
3655 /// static_assert(find_index_<int, list<short, int, float>>{} == 1, "");
3656 /// static_assert(find_index_<double, list<short, int, float>>{} == meta::npos{}, "");
3657 /// \endcode
3658 /// \ingroup trait
3659 template <typename... As>
3660 using let = _t<_t<detail::let_<As...>>>;
3661
3662 namespace lazy
3663 {
3664 /// \sa `meta::let`
3665 /// \ingroup lazy_trait
3666 template <typename... As>
3667 using let = defer<let, As...>;
3668 } // namespace lazy
3669
3670 // Some argument placeholders for use in \c lambda expressions.
3671 /// \ingroup trait
3672 inline namespace placeholders
3673 {
3674 // regular placeholders:
3675 struct _a;
3676 struct _b;
3677 struct _c;
3678 struct _d;
3679 struct _e;
3680 struct _f;
3681 struct _g;
3682 struct _h;
3683 struct _i;
3684
3685 // variadic placeholders:
3686 using _args = vararg<void>;
3687 using _args_a = vararg<_a>;
3688 using _args_b = vararg<_b>;
3689 using _args_c = vararg<_c>;
3690 } // namespace placeholders
3691
3692 ///////////////////////////////////////////////////////////////////////////////////////////
3693 // cartesian_product
3694 /// \cond
3695 namespace detail
3696 {
3697 template <typename M2, typename M>
3698 struct cartesian_product_fn
3699 {
3700 template <typename X>
3701 struct lambda0
3702 {
3703 template <typename Xs>
3704 using lambda1 = list<push_front<Xs, X>>;
3705 using type = join<transform<M2, quote<lambda1>>>;
3706 };
3707 using type = join<transform<M, quote_trait<lambda0>>>;
3708 };
3709 } // namespace detail
3710 /// \endcond
3711
3712 /// Given a list of lists \p ListOfLists, return a new list of lists that is the Cartesian
3713 /// Product. Like the `sequence` function from the Haskell Prelude.
3714 /// \par Complexity
3715 /// `O(N * M)`, where `N` is the size of the outer list, and
3716 /// `M` is the size of the inner lists.
3717 /// \ingroup transformation
3718 template <META_TYPE_CONSTRAINT(list_like) ListOfLists>
3719 using cartesian_product =
3720 reverse_fold<ListOfLists, list<list<>>, quote_trait<detail::cartesian_product_fn>>;
3721
3722 namespace lazy
3723 {
3724 /// \sa 'meta::cartesian_product'
3725 /// \ingroup lazy_transformation
3726 template <typename ListOfLists>
3727 using cartesian_product = defer<cartesian_product, ListOfLists>;
3728 } // namespace lazy
3729
3730 /// \cond
3731 ///////////////////////////////////////////////////////////////////////////////////////////
3732 // add_const_if
3733 namespace detail
3734 {
3735 template <bool>
3736 struct add_const_if
3737 {
3738 template <typename T>
3739 using invoke = T const;
3740 };
3741 template <>
3742 struct add_const_if<false>
3743 {
3744 template <typename T>
3745 using invoke = T;
3746 };
3747 } // namespace detail
3748 template <bool If>
3749 using add_const_if_c = detail::add_const_if<If>;
3750 template <META_TYPE_CONSTRAINT(integral) If>
3751 using add_const_if = add_const_if_c<If::type::value>;
3752 /// \endcond
3753
3754 /// \cond
3755 ///////////////////////////////////////////////////////////////////////////////////////////
3756 // const_if
3757 template <bool If, typename T>
3758 using const_if_c = typename add_const_if_c<If>::template invoke<T>;
3759 template <typename If, typename T>
3760 using const_if = typename add_const_if<If>::template invoke<T>;
3761 /// \endcond
3762
3763 /// \cond
3764 namespace detail
3765 {
3766 template <typename State, typename Ch>
3767 using atoi_ = if_c<(Ch::value >= '0' && Ch::value <= '9'),
3768 std::integral_constant<typename State::value_type,
3769 State::value * 10 + (Ch::value - '0')>>;
3770 }
3771 /// \endcond
3772
3773 inline namespace literals
3774 {
3775 /// A user-defined literal that generates objects of type \c meta::size_t.
3776 /// \ingroup integral
3777 template <char... Chs>
3778 constexpr fold<list<char_<Chs>...>, meta::size_t<0>, quote<detail::atoi_>>
3779 operator"" _z()
3780 {
3781 return {};
3782 }
3783 } // namespace literals
3784 } // namespace meta
3785
3786 /// \cond
3787 // Non-portable forward declarations of standard containers
3788 #ifndef META_NO_STD_FORWARD_DECLARATIONS
3789 #if defined(__apple_build_version__) || (defined(__clang__) && __clang_major__ < 6)
3790 META_BEGIN_NAMESPACE_STD
3791 META_BEGIN_NAMESPACE_VERSION
3792 template <class>
3793 class META_TEMPLATE_VIS allocator;
3794 template <class, class>
3795 struct META_TEMPLATE_VIS pair;
3796 template <class>
3797 struct META_TEMPLATE_VIS hash;
3798 template <class>
3799 struct META_TEMPLATE_VIS less;
3800 template <class>
3801 struct META_TEMPLATE_VIS equal_to;
3802 template <class>
3803 struct META_TEMPLATE_VIS char_traits;
3804 #if defined(_GLIBCXX_USE_CXX11_ABI) && _GLIBCXX_USE_CXX11_ABI
3805 inline namespace __cxx11 {
3806 #endif
3807 template <class, class, class>
3808 class META_TEMPLATE_VIS basic_string;
3809 #if defined(_GLIBCXX_USE_CXX11_ABI) && _GLIBCXX_USE_CXX11_ABI
3810 }
3811 #endif
3812 META_END_NAMESPACE_VERSION
3813 META_BEGIN_NAMESPACE_CONTAINER
3814 #if defined(__GLIBCXX__)
3815 inline namespace __cxx11 {
3816 #endif
3817 template <class, class>
3818 class META_TEMPLATE_VIS list;
3819 #if defined(__GLIBCXX__)
3820 }
3821 #endif
3822 template <class, class>
3823 class META_TEMPLATE_VIS forward_list;
3824 template <class, class>
3825 class META_TEMPLATE_VIS vector;
3826 template <class, class>
3827 class META_TEMPLATE_VIS deque;
3828 template <class, class, class, class>
3829 class META_TEMPLATE_VIS map;
3830 template <class, class, class, class>
3831 class META_TEMPLATE_VIS multimap;
3832 template <class, class, class>
3833 class META_TEMPLATE_VIS set;
3834 template <class, class, class>
3835 class META_TEMPLATE_VIS multiset;
3836 template <class, class, class, class, class>
3837 class META_TEMPLATE_VIS unordered_map;
3838 template <class, class, class, class, class>
3839 class META_TEMPLATE_VIS unordered_multimap;
3840 template <class, class, class, class>
3841 class META_TEMPLATE_VIS unordered_set;
3842 template <class, class, class, class>
3843 class META_TEMPLATE_VIS unordered_multiset;
3844 template <class, class>
3845 class META_TEMPLATE_VIS queue;
3846 template <class, class, class>
3847 class META_TEMPLATE_VIS priority_queue;
3848 template <class, class>
3849 class META_TEMPLATE_VIS stack;
3850 META_END_NAMESPACE_CONTAINER
3851 META_END_NAMESPACE_STD
3852
3853 namespace meta
3854 {
3855 namespace detail
3856 {
3857 template <typename T, typename A = std::allocator<T>>
3858 using std_list = std::list<T, A>;
3859 template <typename T, typename A = std::allocator<T>>
3860 using std_forward_list = std::forward_list<T, A>;
3861 template <typename T, typename A = std::allocator<T>>
3862 using std_vector = std::vector<T, A>;
3863 template <typename T, typename A = std::allocator<T>>
3864 using std_deque = std::deque<T, A>;
3865 template <typename T, typename C = std::char_traits<T>, typename A = std::allocator<T>>
3866 using std_basic_string = std::basic_string<T, C, A>;
3867 template <typename K, typename V, typename C = std::less<K>,
3868 typename A = std::allocator<std::pair<K const, V>>>
3869 using std_map = std::map<K, V, C, A>;
3870 template <typename K, typename V, typename C = std::less<K>,
3871 typename A = std::allocator<std::pair<K const, V>>>
3872 using std_multimap = std::multimap<K, V, C, A>;
3873 template <typename K, typename C = std::less<K>, typename A = std::allocator<K>>
3874 using std_set = std::set<K, C, A>;
3875 template <typename K, typename C = std::less<K>, typename A = std::allocator<K>>
3876 using std_multiset = std::multiset<K, C, A>;
3877 template <typename K, typename V, typename H = std::hash<K>,
3878 typename C = std::equal_to<K>,
3879 typename A = std::allocator<std::pair<K const, V>>>
3880 using std_unordered_map = std::unordered_map<K, V, H, C, A>;
3881 template <typename K, typename V, typename H = std::hash<K>,
3882 typename C = std::equal_to<K>,
3883 typename A = std::allocator<std::pair<K const, V>>>
3884 using std_unordered_multimap = std::unordered_multimap<K, V, H, C, A>;
3885 template <typename K, typename H = std::hash<K>, typename C = std::equal_to<K>,
3886 typename A = std::allocator<K>>
3887 using std_unordered_set = std::unordered_set<K, H, C, A>;
3888 template <typename K, typename H = std::hash<K>, typename C = std::equal_to<K>,
3889 typename A = std::allocator<K>>
3890 using std_unordered_multiset = std::unordered_multiset<K, H, C, A>;
3891 template <typename T, typename C = std_deque<T>>
3892 using std_queue = std::queue<T, C>;
3893 template <typename T, typename C = std_vector<T>,
3894 class D = std::less<typename C::value_type>>
3895 using std_priority_queue = std::priority_queue<T, C, D>;
3896 template <typename T, typename C = std_deque<T>>
3897 using std_stack = std::stack<T, C>;
3898 } // namespace detail
3899
3900 template <>
3901 struct quote<::std::list> : quote<detail::std_list>
3902 {
3903 };
3904 template <>
3905 struct quote<::std::deque> : quote<detail::std_deque>
3906 {
3907 };
3908 template <>
3909 struct quote<::std::forward_list> : quote<detail::std_forward_list>
3910 {
3911 };
3912 template <>
3913 struct quote<::std::vector> : quote<detail::std_vector>
3914 {
3915 };
3916 template <>
3917 struct quote<::std::basic_string> : quote<detail::std_basic_string>
3918 {
3919 };
3920 template <>
3921 struct quote<::std::map> : quote<detail::std_map>
3922 {
3923 };
3924 template <>
3925 struct quote<::std::multimap> : quote<detail::std_multimap>
3926 {
3927 };
3928 template <>
3929 struct quote<::std::set> : quote<detail::std_set>
3930 {
3931 };
3932 template <>
3933 struct quote<::std::multiset> : quote<detail::std_multiset>
3934 {
3935 };
3936 template <>
3937 struct quote<::std::unordered_map> : quote<detail::std_unordered_map>
3938 {
3939 };
3940 template <>
3941 struct quote<::std::unordered_multimap> : quote<detail::std_unordered_multimap>
3942 {
3943 };
3944 template <>
3945 struct quote<::std::unordered_set> : quote<detail::std_unordered_set>
3946 {
3947 };
3948 template <>
3949 struct quote<::std::unordered_multiset> : quote<detail::std_unordered_multiset>
3950 {
3951 };
3952 template <>
3953 struct quote<::std::queue> : quote<detail::std_queue>
3954 {
3955 };
3956 template <>
3957 struct quote<::std::priority_queue> : quote<detail::std_priority_queue>
3958 {
3959 };
3960 template <>
3961 struct quote<::std::stack> : quote<detail::std_stack>
3962 {
3963 };
3964 } // namespace meta
3965
3966 #endif
3967 #endif
3968 /// \endcond
3969
3970 #ifdef __clang__
3971 #pragma GCC diagnostic pop
3972 #endif
3973 #endif