【问题标题】:c++11: Calling a variadic function with the elements of a vectorc++11:使用向量的元素调用可变参数函数
【发布时间】:2013-07-31 15:12:13
【问题描述】:

关于如何使用元组的元素调用可变参数函数有很多问题。 例如: How do I expand a tuple into variadic template function's arguments? 我的问题有点不同:

我有一系列函数:

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

【问题讨论】:

  • args的个数有上限吗?
  • @BenjaminLindley 那应该是Arity,我想。
  • 你想用这个完成什么?
  • 解决元组问题的相同技巧,也解决向量问题(只需将std::get&lt;I&gt;(t)...替换为v[I]...
  • @jrok:可以轻松测试。我所知道的是我希望调用的函数的数量。 @Captaion Obvlious:我有 db 绑定,它给了我一系列 bind 电话。我(静态地)知道我想调用哪一个,并将所有参数放在 vector 中。

标签: c++ templates c++11 variadic-templates


【解决方案1】:

这是一个工作示例:

#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);
}

【讨论】:

    【解决方案2】:

    Working example:

    #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;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      相关资源
      最近更新 更多