[英]Calling a function for each variadic template argument and an array
所以我有一些X
型:
typedef ... X;
和模板函数f
:
class <typename T>
void f(X& x_out, const T& arg_in);
然后是一个函数g
:
void g(const X* x_array, size_t x_array_size);
我需要编写一个variadic模板函数h
来执行此操作:
template<typename... Args>
void h(Args... args)
{
constexpr size_t nargs = sizeof...(args); // get number of args
X x_array[nargs]; // create X array of that size
for (int i = 0; i < nargs; i++) // foreach arg
f(x_array[i], args[i]); // call f (doesn't work)
g(x_array, nargs); // call g with x_array
}
它不起作用的原因是因为你不能在运行时下标这样的args。
替换h
的中间部分的最佳技术是什么?
获胜者是Xeo:
template<class T> X fv(const T& t) { X x; f(x,t); return x; }
template<class... Args>
void h(Args... args)
{
X x_array[] = { fv(args)... };
g(x_array, sizeof...(Args));
}
(实际上在我的具体情况下,我可以重写f以按值返回x而不是作为out参数,所以我甚至不需要上面的fv)
你可以重构或包装f
来返回一个新的X
而不是让它通过,因为这会将包扩展到手中并使函数真正简洁:
template<class T>
X fw(T const& t){ X x; f(x, t); return x; }
template<class... Args>
void h(Args... args){
X xs[] = { fw(args)... };
g(xs, sizeof...(Args));
}
如果您可以将g
更改为只接受std::initializer_list
,则会更简洁:
template<class... Args>
void h(Args... args){
g({f(args)...});
}
实例。 或(也许更好),你也可以提供只是一个包装g
转发到真正的g
:
void g(X const*, unsigned){}
void g(std::initializer_list<X> const& xs){ g(xs.begin(), xs.size()); }
template<class... Args>
void h(Args... args){
g({f(args)...});
}
实例。
编辑:另一个选项是使用临时数组:
template<class T>
using Alias = T;
template<class T>
T& as_lvalue(T&& v){ return v; }
template<class... Args>
void h(Args... args){
g(as_lvalue(Alias<X[]>{f(args)...}), sizeof...(Args));
}
实例。 请注意, as_lvalue
函数很危险,数组仍然只存在于完整表达式的末尾(在本例中为g
),因此在使用它时要小心。 需要Alias
,因为语言语法只允许X[]{ ... }
。
如果所有这些都不可能,您将需要递归来访问args
包的所有元素。
#include <tuple>
template<unsigned> struct uint_{}; // compile-time integer for "iteration"
template<unsigned N, class Tuple>
void h_helper(X (&)[N], Tuple const&, uint_<N>){}
template<unsigned N, class Tuple, unsigned I = 0>
void h_helper(X (&xs)[N], Tuple const& args, uint_<I> = {}){
f(xs[I], std::get<I>(args));
h_helper(xs, args, uint_<I+1>());
}
template<typename... Args>
void h(Args... args)
{
static constexpr unsigned nargs = sizeof...(Args);
X xs[nargs];
h_helper(xs, std::tie(args...));
g(xs, nargs);
}
编辑:受到ecatmur评论的启发,我使用了索引技巧 ,使其仅用于包扩展和f
和g
原样,而不需要改变它们。
template<unsigned... Indices>
struct indices{
using next = indices<Indices..., sizeof...(Indices)>;
};
template<unsigned N>
struct build_indices{
using type = typename build_indices<N-1>::type::next;
};
template <>
struct build_indices<0>{
using type = indices<>;
};
template<unsigned N>
using IndicesFor = typename build_indices<N>::type;
template<unsigned N, unsigned... Is, class... Args>
void f_them_all(X (&xs)[N], indices<Is...>, Args... args){
int unused[] = {(f(xs[Is], args), 1)...};
(void)unused;
}
template<class... Args>
void h(Args... args){
static constexpr unsigned nargs = sizeof...(Args);
X xs[nargs];
f_them_all(xs, IndicesFor<nargs>(), args...);
g(xs, nargs);
}
很明显:你不使用迭代而是递归。 当处理可变参数模板时,总会有一些递归。甚至在使用tie()
将元素绑定到std::tuple<...>
它也是递归的:它恰好发生了递归业务由元组完成。 在你的情况下,你似乎想要这样的东西(可能有一些拼写错误,但总的来说这应该有效):
template <int Index, int Size>
void h_aux(X (&)[Size]) {
}
template <int Index, int Size, typename Arg, typename... Args>
void h_aux(X (&xs)[Size], Arg arg, Args... args) {
f(xs[Index], arg);
h_aux<Index + 1, Size>(xs, args...);
}
template <typename... Args>
void h(Args... args)
{
X xs[sizeof...(args)];
h_aux<0, sizeof...(args)>(xs, args...);
g(xs, sizeof...(args));
}
我认为你将无法使用nargs
来定义数组的大小:没有任何东西向编译器表明它应该是一个常量表达式。
好的模板作为问题第一部分的答案:
template <class F, class... Args>
void for_each_argument(F f, Args&&... args) {
[](...){}((f(std::forward<Args>(args)), 0)...);
}
即使您无法重写f
以按值返回输出参数,使用参数包扩展也相当简单:
struct pass { template<typename ...T> pass(T...) {} };
template<typename... Args>
void h(Args... args)
{
const size_t nargs = sizeof...(args); // get number of args
X x_array[nargs]; // create X array of that size
X *x = x_array;
int unused[]{(f(*x++, args), 1)...}; // call f
pass{unused};
g(x_array, nargs); // call g with x_array
}
应该可以写
pass{(f(*x++, args), 1)...}; // call f
但似乎g ++(至少4.7.1)有一个错误,它无法将brace-initializer-list参数的评估命令为类初始化者。 数组初始化器虽然可以; 有关更多信息和示例,请参见可变扩展中的排序 。
实例 。
作为替代方案,这是Xeo使用生成的索引包提到的技术; 不幸的是它确实需要额外的函数调用和参数,但它相当优雅(特别是如果你碰巧有一个索引包生成器):
template<int... I> struct index {
template<int n> using append = index<I..., n>; };
template<int N> struct make_index { typedef typename
make_index<N - 1>::type::template append<N - 1> type; };
template<> struct make_index<0> { typedef index<> type; };
template<int N> using indexer = typename make_index<N>::type;
template<typename... Args, int... i>
void h2(index<i...>, Args... args)
{
const size_t nargs = sizeof...(args); // get number of args
X x_array[nargs]; // create X array of that size
pass{(f(x_array[i], args), 1)...}; // call f
g(x_array, nargs); // call g with x_array
}
template<typename... Args>
void h(Args... args)
{
h2(indexer<sizeof...(args)>(), std::forward<Args>(args)...);
}
参见C ++ 11:我可以从多个args转到元组,但是我可以从元组转到多个args吗? 欲获得更多信息。 实例 。
Xeo是正确的想法 - 你想构建某种“可变迭代器”,它从其余的代码中隐藏了很多这种肮脏的东西。
我将索引内容隐藏在std :: vector之后的迭代器接口后面,因为std :: tuple也是数据的线性容器。 然后,您可以重新使用它的所有可变参数函数和类,而无需在其他任何地方显式递归代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.