tlx
fold_right.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/meta/fold_right.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2018 Hung Tran <hung@ae.cs.uni-frankfurt.de>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #ifndef TLX_META_FOLD_RIGHT_HEADER
12 #define TLX_META_FOLD_RIGHT_HEADER
13 
15 #include <tuple>
16 
17 namespace tlx {
18 
19 //! \addtogroup tlx_meta
20 //! \{
21 
22 /******************************************************************************/
23 // Variadic Template Expander: Implements fold_right() on the variadic template
24 // arguments. Implements (pack ... op ... init) of C++17.
25 
26 namespace meta_detail {
27 
28 //! helper for fold_right: base case
29 template <typename Reduce, typename Initial, typename Arg>
30 auto fold_right_impl(Reduce&& r, Initial&& init, Arg&& arg) {
31  return std::forward<Reduce>(r)(
32  std::forward<Arg>(arg), std::forward<Initial>(init));
33 }
34 
35 //! helper for fold_right: general recursive case
36 template <typename Reduce, typename Initial, typename Arg, typename... MoreArgs>
37 auto fold_right_impl(Reduce&& r, Initial&& init,
38  Arg&& arg, MoreArgs&& ... rest) {
39  return std::forward<Reduce>(r)(
40  std::forward<Arg>(arg),
41  fold_right_impl(std::forward<Reduce>(r), std::forward<Initial>(init),
42  std::forward<MoreArgs>(rest) ...));
43 }
44 
45 } // namespace meta_detail
46 
47 //! Implements fold_right() -- (a * (b * c)) -- with a binary Reduce operation
48 //! and initial value.
49 template <typename Reduce, typename Initial, typename... Args>
50 auto fold_right(Reduce&& r, Initial&& init, Args&& ... args) {
52  std::forward<Reduce>(r), std::forward<Initial>(init),
53  std::forward<Args>(args) ...);
54 }
55 
56 //! \}
57 
58 } // namespace tlx
59 
60 #endif // !TLX_META_FOLD_RIGHT_HEADER
61 
62 /******************************************************************************/
auto fold_right_impl(Reduce &&r, Initial &&init, Arg &&arg)
helper for fold_right: base case
Definition: fold_right.hpp:30
auto fold_right(Reduce &&r, Initial &&init, Args &&...args)
Implements fold_right() – (a * (b * c)) – with a binary Reduce operation and initial value...
Definition: fold_right.hpp:50