zoukankan      html  css  js  c++  java
  • C++14 make code cleaner

    在C++11中我们如果要写一个通过tuple实现函数调用的函数要这样写:

        template<int...>
        struct IndexTuple{};
    
        template<int N, int... Indexes>
        struct MakeIndexes : MakeIndexes<N - 1, N - 1, Indexes...>    {};
    
        template<int... indexes>
        struct MakeIndexes<0, indexes...>
        {
            typedef IndexTuple<indexes...> type;
        };
    
        template<typename F, int ... Indexes, typename ... Args>
        static void call_helper(F f, IndexTuple<Indexes...>, const std::tuple<Args...>& tup)
        {
            f(std::get<Indexes>(tup)...);
        }
    
        template<typename F, typename ... Args>
        static void call(F f, const std::tuple<Args...>& tp)
        {
            call_helper(f, typename MakeIndexes<sizeof... (Args)>::type(), tp);
        }

    在C++14中MakeIndexes和IndexTuple已经由utility库提供了,我们可以写得更简洁了,两个函数就可以了。

        template<typename F, size_t... I, typename ... Args>
        static void call_helper(F f, std::index_sequence<I...>, const std::tuple<Args...>& tup)
        {
            f(std::get<I>(tup)...);
        }
    
        template<typename F, typename ... Args>
        static void call(F f, const std::tuple<Args...>& tp)
        {
            call_helper(f, std::make_index_sequence<sizeof... (Args)>(), tp);
        }

    这样写更省心啦o(∩_∩)o 。

  • 相关阅读:
    day01
    day02
    Linux安装Redis、PHP安装Redis扩展模块
    数据类型
    Redis常用命令
    Redis高级实用特性
    php操作redis案例
    (转)java二维数组的深度学习(静态与动态)
    java二维数组学习(转)
    java一维数组学习
  • 原文地址:https://www.cnblogs.com/qicosmos/p/4916404.html
Copyright © 2011-2022 走看看