C++11:使用向量元素调用可变函数

3

关于如何使用元组的元素调用可变参数函数有很多问题。例如:如何将元组展开为可变模板函数的参数?我的问题略有不同:

我有一组函数:

void f(int arg1);
void f(int arg1, int arg2);
...

我需要一个模板:

template<size_t Arity>
void call(std::vector<int> args) {
   ???
}

这将使用args [0],args [1] ...调用适当的f函数。


函数的参数数量有上限吗? - undefined
@BenjaminLindley 那应该是Arity,我想。 - undefined
你在尝试做什么? - undefined
2
解决元组的相同技巧也适用于向量(只需将 std::get<I>(t)... 替换为 v[I]...)。 - undefined
@TillVaroquaux 包括你的失败尝试通常是一个好主意。很可能你只是犯了一个微小的错误:它几乎与“tuple”情况完全相同。也许用 Arity 替换 sizeof...(Ts) - undefined
显示剩余2条评论
2个回答

6
这是一个可行的示例:
#include <vector>

// indices machinery

template< std::size_t... Ns >
struct indices {
    typedef indices< Ns..., sizeof...( Ns ) > next;
};

template< std::size_t N >
struct make_indices {
    typedef typename make_indices< N - 1 >::type::next type;
};

template<>
struct make_indices< 0 > {
    typedef indices<> type;
};

void f(int) {}
void f(int, int) {}

// helper function because we need a way
// to deduce indices pack

template<size_t... Is>
void call_helper(const std::vector<int>& args, indices<Is...>)
{
    f( args[Is]... ); // expand the indices pack
}

template<std::size_t Arity>
void call(const std::vector<int>& args)
{
    if (args.size() < Arity) throw 42;
    call_helper(args, typename make_indices<Arity>::type());
}

int main()
{
    std::vector<int> v(2);
    call<2>(v);
}

4

运行示例:

#include <cassert>
#include <cstddef>
#include <iostream>
#include <vector>

using namespace std;

template <size_t... I>
struct index_sequence {};

template <size_t N, size_t... I>
struct make_index_sequence : public make_index_sequence<N - 1, N - 1, I...> {};

template <size_t... I>
struct make_index_sequence<0, I...> : public index_sequence<I...> {};

int f(int a, int b) {
  return a + b;
}
void f(int a, int b, int c) {
  cout << "args = (" << a << ", " << b << ", " << c << ")\n";
}

template <typename T, size_t... I>
auto call_(const vector<T>& vec, index_sequence<I...>)
  -> decltype(f(vec[I]...)) {
  return f(vec[I]...);
}

template <size_t Arity, typename T>
auto call(const vector<T>& vec)
  -> decltype(call_(vec, make_index_sequence<Arity>())) {
  assert(vec.size() >= Arity);
  return call_(vec, make_index_sequence<Arity>());
}

int main() {
  vector<int> values = {0, 1, 2, 3, 4, 5};
  call<3>(values);
  cout << "call<2>(values) = " << call<2>(values) << endl;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接