内容简介:昨天 jsteward 问我,有一个接受若干个同类型参数的函数这篇来解决这个问题。首先,待执行的函数的参数个数,在编译时就能知道;比如这里是 3 个。于是,很自然地,我们最终肯定需要类似这样的代码:
昨天 jsteward 问我,有一个接受若干个同类型参数的函数 template <typename U, typename T> U func(T a, T b, T c)
,现在有一个 std::vector<T> args
,希望将 std::vector<T>
当中的元素作为函数参数传进去,要怎么办。
这篇来解决这个问题。
首先,待执行的函数的参数个数,在编译时就能知道;比如这里是 3 个。于是,很自然地,我们最终肯定需要类似这样的代码:
func(args[0], args[1], args[2]);
现在的问题是,我们希望这种调用代码能够自动生成,而不是手动去写。因为 func
的参数,在实际情况下,可能不止 3 个。C++ 11 引入了参数包(parameter pack)的概念,能够将若干个模板参数,打包在一起,然后再用 ...
的方式展开。参数包可以是类型参数包,比如 template <typename... Args>
,这样 (*Args)...
就是各个类型的指针;参数包也可以是变量参数包,比如 size_t... I
,这样 args[I]...
就是 args[0], args[1], args[2]
这样的展开。后者正是我们要的。
于是,为了利用参数包,我们首先需要根据函数 func
的参数数量,构建一个参数包,作为索引。
namespace util {
template <size_t... Indices>
struct indices {
using next = indices<Indices..., sizeof...(Indices)>;
};
template <size_t N>
struct build_indices {
using type = typename build_indices<N - 1>::type::next;
};
template <>
struct build_indices<0> {
using type = indices<>;
};
template <size_t N>
using BuildIndices = typename build_indices<N>::type;
} // namespace util
这里用到了前作提到过的技巧。我们看:
-
BuildIndices<0>是build_indices<0>::type也就是全特化的indices<>; -
BuildIndices<1>是build_indices<1>::type也就是build_indices<0>::type::next也就是indices<>::next也就是indices<0>; -
同理,
BuildIndices<2>是build_indices<2>::type也就是build_indices<1>::type::next也就是build_indices<0>::type::next::next也就是indices<0>::next也就是indices<0, 1>; -
以此类推
BuildIndices<N>是indices<0, 1, ..., N - 1>。
于是我们很容易构建出 caller 如下:
template <typename Func, typename T, size_t... I>
void call(Func& func, std::vector<T>& args, indices<I...>) {
f(args[I]...)
}
使用时只需要这样既可:
call(func, args, BuildIndices<num_args>());
我们将整个封装起来,写成一个完整的例子如下:
#include <iostream>
#include <utility>
#include <vector>
namespace util {
template <size_t... Indices>
struct indices {
using next = indices<Indices..., sizeof...(Indices)>;
};
template <size_t N>
struct build_indices {
using type = typename build_indices<N-1>::type::next;
};
template <>
struct build_indices<0> {
using type = indices<>;
};
template <size_t N>
using BuildIndices = typename build_indices<N>::type;
template <typename returnT, typename valueT, size_t num_args>
struct unpack_caller {
private:
template <typename FuncType, size_t... I>
returnT call(FuncType& f, std::vector<valueT>& args, indices<I...>) {
return f(args[I]...);
}
public:
template <typename FuncType>
returnT operator()(FuncType& f, std::vector<valueT>& args) {
return call(f, args, BuildIndices<num_args>());
}
};
} // namespace util
int func(int a, int b, int c) {
return a + b + c;
}
int main() {
std::vector<int> args = {1, 2, 3};
int i = util::unpack_caller<int, int, 3>()(func, args);
std::cout << i << std::endl;
return 0;
}
以上所述就是小编给大家介绍的《在 C++ 中解包 std::vector 作为函数参数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Golang 中函数作为值与类型
- Python之在函数中使用列表作为默认参数
- golang-101-hacks(12)——切片作为函数参数传递
- Vue 中,如何将函数作为 props 传递给组件
- 将数组和矩阵传递给函数,作为C中指针的指针和指针
- React 组件模式-有状态组件 x 无状态组件、容器组件 x 展示组件、高阶组件 x 渲染回调(函数作为子组件)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Coding the Matrix
Philip N. Klein / Newtonian Press / 2013-7-26 / $35.00
An engaging introduction to vectors and matrices and the algorithms that operate on them, intended for the student who knows how to program. Mathematical concepts and computational problems are motiva......一起来看看 《Coding the Matrix》 这本书的介绍吧!