Rom*_*man 1 c++ arguments c++14
我有一个第三方库,它使用同类输入参数实现重载函数,例如:
int foo(int a);
int foo(int a, int b);
int foo(int a, int b, int c);
...
Run Code Online (Sandbox Code Playgroud)
现在我想编写一个包装器来接受包含在向量中的参数(或者数组):
int foo_wrapper(vector<int>& foo_args);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我已经看过这个关于将向量转换为元组 以获得提示的答案,但它给了我一个错误:
$ 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);
                    ^
Run Code Online (Sandbox Code Playgroud)
与test.cpp存在
#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);
}
Run Code Online (Sandbox Code Playgroud)
    这个问题一般很难回答,std::vector但用std::array或相对简单std::tuple.既然您在问题中指定了没问题,std::array那么只需进行一些调整就可以使您的示例正常工作.
#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;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
这也适用于std::tuple通过改变std::array来std::tuple和更改报表模板进行匹配.
#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;
}
Run Code Online (Sandbox Code Playgroud)