Ferenc Szontágh
2024-07-01 abf49b44cc47f39d6cceb83866f915bc03b7d900
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef _APPLY_TUPLE_H_
#define _APPLY_TUPLE_H_
 
#include <stddef.h>
#include <tuple>
#include <type_traits>
#include <utility>
 
namespace qtl
{
 
    namespace detail
    {
 
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
 
        template <class F, class Tuple>
        inline constexpr decltype(auto) apply_tuple(F &&f, Tuple &&t)
        {
            return std::apply(std::forward<F>(f), std::forward<Tuple>(t));
        }
 
#else
 
        namespace detail
        {
            template <size_t N>
            struct apply
            {
                template <typename F, typename T, typename... A>
                static inline auto apply_tuple(F &&f, T &&t, A &&...a)
                    -> decltype(apply<N - 1>::apply_tuple(
                        std::forward<F>(f), std::forward<T>(t),
                        std::get<N - 1>(std::forward<T>(t)), std::forward<A>(a)...))
                {
                    return apply<N - 1>::apply_tuple(std::forward<F>(f), std::forward<T>(t),
                                                     std::get<N - 1>(std::forward<T>(t)), std::forward<A>(a)...);
                }
            };
 
            template <>
            struct apply<0>
            {
                template <typename F, typename T, typename... A>
                static inline typename std::result_of<F(A...)>::type apply_tuple(F &&f, T &&, A &&...a)
                {
                    return std::forward<F>(f)(std::forward<A>(a)...);
                }
            };
        }
 
        template <typename F, typename T>
        inline auto apply_tuple(F &&f, T &&t)
            -> decltype(detail::apply<std::tuple_size<
                            typename std::decay<T>::type>::value>::apply_tuple(std::forward<F>(f), std::forward<T>(t)))
        {
            return detail::apply<std::tuple_size<
                typename std::decay<T>::type>::value>::apply_tuple(std::forward<F>(f), std::forward<T>(t));
        }
 
#endif // C++17
 
    }
 
}
 
#endif //_APPLY_TUPLE_H_