Any Solution to Unpack a Vector to Function Arguments in C++?

前端 未结 2 1759
感动是毒
感动是毒 2020-11-30 11:39

I am actually thinking of something similar to the \'*\' operator in python like this:

args = [1,2,4]
f(*args)

Is there a similar solution

2条回答
  •  悲&欢浪女
    2020-11-30 12:22

    Update to @Fernandes's answer.

    Yes, there does be a way to remove the need to specifying num_args in template parameter. This is because num_args is determined by the function signature, not the vector. What should be checked at run-time is the size of the vector and the arity of the function.

    See the following example.

    #include 
    #include 
    #include 
    #include 
    
    namespace util {
    template 
    struct function_traits_defs {
      static constexpr size_t arity = sizeof...(Args);
    
      using result_type = ReturnType;
    
      template 
      struct arg {
        using type = typename std::tuple_element>::type;
      };
    };
    
    template 
    struct function_traits_impl;
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits_impl
        : function_traits_defs {};
    
    template 
    struct function_traits
        : function_traits_impl {};
    
    template 
    struct function_traits
        : function_traits_impl {};
    
    template 
    struct indices {
      using next = indices;
    };
    template 
    struct build_indices {
      using type = typename build_indices::type::next;
    };
    template <>
    struct build_indices<0> {
      using type = indices<>;
    };
    template 
    using BuildIndices = typename build_indices::type;
    
    namespace details {
    template ,
              typename ReturnT = typename Traits::result_type>
    ReturnT do_call(FuncType& func,
                    VecType& args,
               indices ) {
      assert(args.size() >= Traits::arity);
      return func(args[I]...);
    }
    }  // namespace details
    
    template ,
              typename ReturnT = typename Traits::result_type>
    ReturnT unpack_caller(FuncType& func,
                    VecType& args) {
      return details::do_call(func, args, BuildIndices());
    }
    }  // namespace util
    
    int func(int a, int b, int c) {
      return a + b + c;
    }
    
    int main() {
      std::vector args = {1, 2, 3};
    
      int j = util::unpack_caller(func, args);
      std::cout << j << std::endl;
    
      return 0;
    }
    

提交回复
热议问题