Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tuple>
- // Определение списка целых чисел времени компиляции IntList
- template <int ... Ints>
- struct IntList;
- template <int H, int ... I>
- struct IntList <H, I...>
- {
- static int const Head = H;
- using Tail = IntList<I...>;
- };
- template <>
- struct IntList <> {};
- // определите метафункцию Length для вычисления длины списка
- template <typename TL>
- struct Length
- {
- static int const value = 1 +
- Length <typename TL::Tail>::value;
- };
- template <>
- struct Length <IntList<>>
- {
- static int const value = 0;
- };
- template<int N, typename IL>
- struct IntCons;
- template<int N, int... Ints>
- struct IntCons<N, IntList<Ints...>>
- {
- using type = IntList<N, Ints...>;
- };
- template<typename IL, int N>
- struct IntAdd;
- template<int... Ints, int N>
- struct IntAdd<IntList<Ints...>, N>
- {
- using type = IntList<Ints..., N>;
- };
- template <int N, typename IL = IntList<>>
- struct Generate;
- template <int N, int... Ints>
- struct Generate<N, IntList<Ints...>>
- {
- using type = typename IntAdd<typename Generate<N - 1, IntList<>>::type, N - 1>::type;
- };
- template <>
- struct Generate<0>
- {
- static const int length = 0;
- using type = IntList<>;
- };
- template<typename Tf, typename ...Args>
- auto apply(Tf f, std::tuple<Args...> t) -> decltype(apply_f(f, t, typename Generate<sizeof ... (Args)>::type()))
- {
- return apply_f(f, t, Generate<sizeof ... (Args)>::type());
- }
- template<typename Tf, typename ...Args, int ...Ints>
- auto apply_f(Tf f, std::tuple<Args...> t, IntList<Ints...>)
- {
- return f(std::forward<Args...>(std::get<Ints>(t)));
- //return 0;
- }
- int main()
- {
- auto f = [](int x, double y, double z) { return x + y + z; };
- auto t = std::make_tuple(30, 5.0, 1.6); // std::tuple<int, double, double>
- auto x = f(std::get<0>(t), std::get<1>(t), std::get<2>(t));
- auto res = apply(f, t); // res = 36.6
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment