Roman
Roman

Reputation: 2365

Conversion of vector or array to a list of arguments in C++

I have a third-party library that implements overloaded functions with homogeneous input parameters, like:

int foo(int a);
int foo(int a, int b);
int foo(int a, int b, int c);
...

Now I would like to write a wrapper that would accept the parameters packed in vector (or alternatively an array):

int foo_wrapper(vector<int>& foo_args);

How can I do this?

I have looked at this answer on converting vector to tuple for a hint, but it gives me an error:

$ g++ -std=c++14 -o test.a test.cpp
test.cpp: In function 'int main(int, char**)':
test.cpp:23:20: error: no matching function for call to 'vectorToTuple(std::vector<int>&)'
     vectorToTuple(v);
                    ^
test.cpp:16:6: note: candidate: template<long unsigned int N, class T> auto vectorToTuple(const std::vector<T>&)
 auto vectorToTuple(const std::vector<T>& v) {
      ^~~~~~~~~~~~~
test.cpp:16:6: note:   template argument deduction/substitution failed:
test.cpp:23:20: note:   couldn't deduce template parameter 'N'
     vectorToTuple(v);
                    ^

with test.cpp being

#include <iostream>
#include <vector>
#include <tuple>
#include <utility>

int foo(int a) {return a;};
int foo(int a, int b) {return a+b;};
int foo(int a, int b, int c) {return a+b+c;};

template <typename T, std::size_t... Indices>
auto vectorToTupleHelper(const std::vector<T>& v, std::index_sequence<Indices...>) {
  return std::make_tuple(v[Indices]...);
}

template <std::size_t N, typename T>
auto vectorToTuple(const std::vector<T>& v) {
  //assert(v.size() >= N);
  return vectorToTupleHelper(v, std::make_index_sequence<N>());
}

int main(int argc, char** argv) {
    std::vector<int> v={1,2};
    vectorToTuple(v);
}

Upvotes: 2

Views: 1575

Answers (2)

Fran&#231;ois Andrieux
Fran&#231;ois Andrieux

Reputation: 29022

This question is very difficult to answer generically with std::vector but relatively simple with std::array or std::tuple. Since you specified in your question that std::array would be fine, your example can be made to work fine with just a few tweaks.

#include <array>
#include <iostream>
#include <tuple>
#include <utility>

int foo(int a) {return a;};
int foo(int a, int b) {return a+b;};
int foo(int a, int b, int c) {return a+b+c;};

template <std::size_t N, typename T, std::size_t... Indices>
auto call_foo_helper(const std::array<T, N>& v, std::index_sequence<Indices...>) {
  return foo(std::get<Indices>(v)...);
}

template <std::size_t N, typename T>
auto call_foo(const std::array<T, N>& v) {
  return call_foo_helper<N>(v, std::make_index_sequence<N>());
}

int main(int argc, char** argv) {
    std::array<int, 2> v={1,2};
    auto x = call_foo(v); // x = 1 + 2 = 3
    return 0;
}

Edit:

This also works for std::tuple by changing std::array to std::tuple and changing the template statements to match.

#include <iostream>
#include <tuple>
#include <utility>

int foo(int a) {return a;};
double foo(int a, double b) {return a+b;};
double foo(int a, double b, int c) {return a+b+c;};

template <typename... T, std::size_t... Indices>
auto call_foo_helper(const std::tuple<T...>& v, std::index_sequence<Indices...>) {
  return foo(std::get<Indices>(v)...);
}

template <typename... T>
auto call_foo(const std::tuple<T...>& v) {
  return call_foo_helper(v, std::make_index_sequence<sizeof...(T)>());
}

int main(int argc, char** argv) {
    auto v = std::make_tuple(1, 2.0);
    auto x = call_foo(v);
    return 0;
}

Upvotes: 6

&#208;аn
&#208;аn

Reputation: 10875

Function overload resolution is done at compile-time, and since the size of std::vector<> is only known at run-time, you have to execute code:

int foo_wrapper(std::vector<int>& foo_args)
{
    switch (foo_args.size())
    {
        case 1: return foo(foo_args[0]);
        case 2: return foo(foo_args[0], foo_args[1]);
        case 3: return foo(foo_args[0], foo_args[1], foo_args[2]);
        default:
            throw std::range_error("Incorrect number of arguments.");
    }
}

int main(int argc, char** argv) {
    std::vector<int> v = { 1,2 };
    std::cout << foo_wrapper(v);
}

As mentioned in another answer, the size of std::array<> is known at compile time, which allows for a different solution.

Upvotes: 2

Related Questions