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 。

  • 相关阅读:
    腾讯云短信接口完成验证码功能
    git使用的简要介绍
    drf分页组件补充
    drf中的jwt使用与手动签发效验
    django的认证演变过程分析
    drf三大认证补充
    drf三大认证
    IO事件
    配置Java环境变量
    各种O
  • 原文地址:https://www.cnblogs.com/qicosmos/p/4916404.html
Copyright © 2011-2022 走看看