我需要为任意元组中的每个元素调用-template或重载函数。确切地说,我需要在元素在元组中指定时对其调用此函数。
例如。我有一个元组std::tuple<int, float> t{1, 2.0f};
和一个函数
class Lambda{
public:
template<class T>
void operator()(T arg){ std::cout << arg << "; "; }
};
我需要一些结构/函数Apply
,如果像Apply<Lambda, int, float>()(Lambda(), t)
一样调用,它将产生:
1; 2.0f;
而非CCD_ 4。
请注意,如果将"原始"参数包传递给函数,我知道解决方案,并且我知道如何以相反的顺序对元组执行此操作。但是以下部分专用化Apply
的尝试失败了:
template<class Func, size_t index, class ...Components>
class ForwardsApplicator{
public:
void operator()(Func func, const std::tuple<Components...>& t){
func(std::get<index>(t));
ForwardsApplicator<Func, index + 1, Components...>()(func, t);
}
};
template<class Func, class... Components>
class ForwardsApplicator < Func, sizeof...(Components), Components... > {
public:
void operator()(Func func, const std::tuple<Components...>& t){}
};
int main{
ForwardsApplicator<Lambda, 0, int, float>()(Lambda{}, std::make_tuple(1, 2.0f));
}
代码已编译,但只打印第一个参数。但是,如果我将ForwardsApplicator
专业化替换为
template<class Func, class... Components>
class ForwardsApplicator < Func, 2, Components... >{...}
它工作正常,但当然,只适用于长度为2的元组。如果可能的话,对于任意长度的元组,我该如何做到这一点?
编辑:谢谢大家的回答!三者都非常切中要害,并从所有可能的有利位置解释了这个问题。
这是integer_sequence
技巧的教科书案例。
template<class Func, class Tuple, size_t...Is>
void for_each_in_tuple(Func f, Tuple&& tuple, std::index_sequence<Is...>){
using expander = int[];
(void)expander { 0, ((void)f(std::get<Is>(std::forward<Tuple>(tuple))), 0)... };
}
template<class Func, class Tuple>
void for_each_in_tuple(Func f, Tuple&& tuple){
for_each_in_tuple(f, std::forward<Tuple>(tuple),
std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>());
}
演示。
std::index_sequence
和friends是C++14,但它是一个纯库扩展,可以很容易地在C++11中实现。您可以很容易地在SO上找到六个实现。
问题是size...(Components)
不能用于未知类型列表Components
的专用化。GCC对此抱怨错误:
prog.cpp:16:7: error: template argument 'sizeof... (Components)' involves template parameter(s) class ForwardsApplicator < Func, sizeof...(Components), Components... > { ^
我建议采取稍微不同的方法。首先,将类型列表Components
移动到operator()
的模板参数中,即:
template<class ...Components>
void operator()(Func func, const std::tuple<Components...>& t) {
...
}
然后,颠倒调用顺序:首先进行递归调用,然后使用index-1
调用(即调用最后一个元组元素)。从index = sizeof...(Components)
开始这个递归,一直到index = 0
,这是noop(所以专门化有0
,独立于sizeof...(Components)
,这是我开始讨论的问题)。
要帮助调用此函数,请添加一个用于模板参数推导的函数:
// General case (recursion)
template<class Func, size_t index>
class ForwardsApplicator{
public:
template<class ...Components>
void operator()(Func func, const std::tuple<Components...>& t){
ForwardsApplicator<Func, index - 1>()(func, t);
func(std::get<index - 1>(t));
}
};
// Special case (stop recursion)
template<class Func>
class ForwardsApplicator<Func, 0> {
public:
template<class ...Components>
void operator()(Func func, const std::tuple<Components...>& t){}
};
// Helper function for template type deduction
template<class Func, class ...Components>
void apply(Func func, const std::tuple<Components...>& t) {
ForwardsApplicator<Func, sizeof...(Components)>()(func, t);
}
这样就可以很容易地调用,而不需要调用站点上的任何模板参数:
apply(Lambda{}, std::make_tuple(1, 2.0f));
实时演示
模板中的倒计数并不一定意味着您以相同的顺序处理元组元素。从头到尾处理元组的一种简单方法是头递归(与尾递归相反):
#include <tuple>
#include <iostream>
// Our entry point is the tuple size
template<typename Tuple, std::size_t i = std::tuple_size<Tuple>::value>
struct tuple_applicator {
template<typename Func> static void apply(Tuple &t, Func &&f) {
// but we recurse before we process the element associated with that
// number, reversing the order so that the front elements are processed
// first.
tuple_applicator<Tuple, i - 1>::apply(t, std::forward<Func>(f));
std::forward<Func>(f)(std::get<i - 1>(t));
}
};
// The recursion stops here.
template<typename Tuple>
struct tuple_applicator<Tuple, 0> {
template<typename Func> static void apply(Tuple &, Func &&) { }
};
// and this is syntactical sugar
template<typename Tuple, typename Func>
void tuple_apply(Tuple &t, Func &&f) {
tuple_applicator<Tuple>::apply(t, std::forward<Func>(f));
}
int main() {
std::tuple<int, double> t { 1, 2.3 };
// The generic lambda requires C++14, the rest
// works with C++11 as well. Put your Lambda here instead.
tuple_apply(t, [](auto x) { std::cout << x * 2 << 'n'; });
}
输出为
2
4.6