tlx
fold_left.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/meta/fold_left.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_LEFT_HEADER
12 #define TLX_META_FOLD_LEFT_HEADER
13 
15 #include <tuple>
16 
17 namespace tlx {
18 
19 //! \addtogroup tlx_meta
20 //! \{
21 
22 /******************************************************************************/
23 // Variadic Template Expander: Implements fold_left() on the variadic template
24 // arguments. Implements (init ... op ... pack) of C++17.
25 
26 namespace meta_detail {
27 
28 //! helper for fold_left(): base case
29 template <typename Reduce, typename Initial, typename Arg>
30 auto fold_left_impl(Reduce&& r, Initial&& init, Arg&& arg) {
31  return std::forward<Reduce>(r)(
32  std::forward<Initial>(init), std::forward<Arg>(arg));
33 }
34 
35 //! helper for fold_left(): general recursive case
36 template <typename Reduce, typename Initial, typename Arg, typename... MoreArgs>
37 auto fold_left_impl(Reduce&& r, Initial&& init,
38  Arg&& arg, MoreArgs&& ... rest) {
39  return fold_left_impl(
40  std::forward<Reduce>(r),
41  std::forward<Reduce>(r)(
42  std::forward<Initial>(init), std::forward<Arg>(arg)),
43  std::forward<MoreArgs>(rest) ...);
44 }
45 
46 } // namespace meta_detail
47 
48 //! Implements fold_left() -- ((a * b) * c) -- with a binary Reduce operation
49 //! and initial value.
50 template <typename Reduce, typename Initial, typename... Args>
51 auto fold_left(Reduce&& r, Initial&& init, Args&& ... args) {
53  std::forward<Reduce>(r), std::forward<Initial>(init),
54  std::forward<Args>(args) ...);
55 }
56 
57 //! \}
58 
59 } // namespace tlx
60 
61 #endif // !TLX_META_FOLD_LEFT_HEADER
62 
63 /******************************************************************************/
auto fold_left_impl(Reduce &&r, Initial &&init, Arg &&arg)
helper for fold_left(): base case
Definition: fold_left.hpp:30
auto fold_left(Reduce &&r, Initial &&init, Args &&...args)
Implements fold_left() – ((a * b) * c) – with a binary Reduce operation and initial value...
Definition: fold_left.hpp:51