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

You can use a pack of indices:

template <size_t num_args>
struct unpack_caller
{
private:
    template <typename FuncType, size_t... I>
    void call(FuncType &f, std::vector<int> &args, indices<I...>){
        f(args[I]...);
    }

public:
    template <typename FuncType>
    void operator () (FuncType &f, std::vector<int> &args){
        assert(args.size() == num_args); // just to be sure
        call(f, args, BuildIndices<num_args>{});
    }
};

There’s no way to remove the need to specify the size in the template though, because the size of a vector is a runtime construct, and we need the size at compile-time.

Leave a Comment