tlx
vmap_foreach.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/meta/vmap_foreach.hpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2016-2017 Timo Bingmann <tb@panthema.net>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
11 #ifndef TLX_META_VMAP_FOREACH_HEADER
12 #define TLX_META_VMAP_FOREACH_HEADER
13 
14 #include <tuple>
15 #include <utility>
16 
17 namespace tlx {
18 
19 //! \addtogroup tlx_meta
20 //! \{
21 
22 /******************************************************************************/
23 // Variadic Template Expander: run a generic templated functor (like a generic
24 // lambda) for each of the variadic template parameters, and collect the return
25 // values in a generic std::tuple.
26 
27 namespace meta_detail {
28 
29 //! helper for vmap_foreach: base case
30 template <typename Functor, typename Arg>
31 auto vmap_foreach_impl(Functor&& f, Arg&& arg) {
32  return std::make_tuple(
33  std::forward<Functor>(f)(std::forward<Arg>(arg)));
34 }
35 
36 //! helper for vmap_foreach: general recursive case
37 template <typename Functor, typename Arg, typename... MoreArgs>
38 auto vmap_foreach_impl(Functor&& f, Arg&& arg, MoreArgs&& ... rest) {
39  auto x = std::forward<Functor>(f)(std::forward<Arg>(arg));
40  return std::tuple_cat(
41  std::make_tuple(std::move(x)),
43  std::forward<Functor>(f), std::forward<MoreArgs>(rest) ...));
44 }
45 
46 } // namespace meta_detail
47 
48 //! Call a generic functor (like a generic lambda) for each variadic template
49 //! argument.
50 template <typename Functor, typename... Args>
51 auto vmap_foreach(Functor&& f, Args&& ... args) {
53  std::forward<Functor>(f), std::forward<Args>(args) ...);
54 }
55 
56 //! \}
57 
58 } // namespace tlx
59 
60 #endif // !TLX_META_VMAP_FOREACH_HEADER
61 
62 /******************************************************************************/
auto vmap_foreach(Functor &&f, Args &&...args)
Call a generic functor (like a generic lambda) for each variadic template argument.
auto vmap_foreach_impl(Functor &&f, Arg &&arg)
helper for vmap_foreach: base case