【发布时间】:2017-01-04 15:40:55
【问题描述】:
我有一个第三方库,它使用同质输入参数实现重载函数,例如:
int foo(int a);
int foo(int a, int b);
int foo(int a, int b, int c);
...
现在我想编写一个包装器来接受包装在向量(或数组)中的参数:
int foo_wrapper(vector<int>& foo_args);
我该怎么做?
我查看了this answer on converting vector to tuple 的提示,但它给了我一个错误:
$ 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);
^
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);
}
【问题讨论】:
-
要消除该错误,答案很明显,请提供
N:vectorToTuple<2>(v);不知道这是否真的解决了您的问题,您在哪里调用foo?