tlx
Loading...
Searching...
No Matches
call_foreach_with_index.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * tlx/meta/call_foreach_with_index.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_CALL_FOREACH_WITH_INDEX_HEADER
12#define TLX_META_CALL_FOREACH_WITH_INDEX_HEADER
13
14#include <utility>
15
17
18namespace tlx {
19
20//! \addtogroup tlx_meta
21//! \{
22
23/******************************************************************************/
24// Variadic Template Expander: run a generic templated functor (like a generic
25// lambda) for each of the variadic template parameters.
26//
27// Called with func(StaticIndex<> index, Argument arg).
28
29namespace meta_detail {
30
31//! helper for call_foreach_with_index: base case
32template <size_t Index, typename Functor, typename Arg>
33void call_foreach_with_index_impl(Functor&& f, Arg&& arg) {
34 std::forward<Functor>(f)(StaticIndex<Index>(), std::forward<Arg>(arg));
35}
36
37//! helper for call_foreach_with_index: general recursive case
38template <size_t Index, typename Functor, typename Arg, typename... MoreArgs>
39void call_foreach_with_index_impl(Functor&& f, Arg&& arg, MoreArgs&& ... rest) {
40 std::forward<Functor>(f)(StaticIndex<Index>(), std::forward<Arg>(arg));
42 std::forward<Functor>(f), std::forward<MoreArgs>(rest) ...);
43}
44
45} // namespace meta_detail
46
47//! Call a generic functor (like a generic lambda) for each variadic template
48//! argument together with its zero-based index.
49template <typename Functor, typename... Args>
50void call_foreach_with_index(Functor&& f, Args&& ... args) {
52 std::forward<Functor>(f), std::forward<Args>(args) ...);
53}
54
55//! \}
56
57} // namespace tlx
58
59#endif // !TLX_META_CALL_FOREACH_WITH_INDEX_HEADER
60
61/******************************************************************************/
void call_foreach_with_index(Functor &&f, Args &&... args)
Call a generic functor (like a generic lambda) for each variadic template argument together with its ...
void call_foreach_with_index_impl(Functor &&f, Arg &&arg)
helper for call_foreach_with_index: base case
Helper for call_foreach_with_index() to save the index as a compile-time index.