我想要一个对于元组来说就像std::transform
一样的函数。基本上要实现的功能是
template<size_t From, size_t To, class Tuple, class Func>
void tuple_transform(Tuple&& source, Tuple&& target, Func f)
{
// elements <From, To> of `target` ti become `f(si)`, where
// si is the corresponding element of `source`
};
我相信要实现这一点,我需要一个编译时整数范围结构,std::index_sequence
的推广,我用{{1}实现了它here }}。我也相信this类型的编译时间遍历在这里是理想的:
cti::range
任何人都可以帮我实施吗?
@MohitJain在代码链接(tuple_transform)中就像没有考虑到这一点(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>());
}
只有一种类型)。如果这样解决了,我可以通过传递模板模板参数Func
轻松扩展它,并强制我的转换类型是这样的
template<class> class Func
然后在元组变换体内,每个变换函数都可以调用如下:
template<typename T>
struct Func
{
static void apply(T& val) { ... }
}
刚刚发表了一段闪电般的话题@ accu 2015。以上是演示结束时的CodeKata。我将离开here演示文稿,希望它对任何实现尝试都有帮助(我认为几乎所有需要的工具都会出现,所以我们会有更多的尝试)
答案 0 :(得分:4)
以下是使用here中的index_range
的解决方案。
template<size_t SN, size_t DN, class TSrc, class TDest, class Func>
void tuple_call_assign(TSrc&& source, TDest& target, Func f)
{
std::get<DN>(target) = f(std::get<SN>(std::forward<TSrc>(source)));
}
template<size_t From, size_t To, class TSrc, class TDest, class Func, size_t...Is, size_t...DIs>
void tuple_transform(TSrc&& source, TDest& target, Func f,
std::index_sequence<Is...>, std::index_sequence<DIs...>)
{
using expander = int[];
(void)expander { 0, (tuple_call_assign<Is,DIs>(std::forward<TSrc>(source),target,f), 0)... };
}
template<size_t From, size_t To, size_t FromDest, class TSrc, class TDest, class Func>
void tuple_transform(TSrc&& source, TDest& target, Func f)
{
static_assert(To > From, "Range must be increasing");
static_assert(To <= std::tuple_size<std::decay_t<TSrc>>::value+1,
"Range must be valid for source tuple");
constexpr size_t RangeSize = To-From;
static_assert(FromDest+RangeSize <= std::tuple_size<std::decay_t<TDest>>::value,
"Range must be valid for target tuple");
tuple_transform<From,To>(std::forward<TSrc>(source), target, f,
index_range<From,To>(), index_range<FromDest, FromDest+RangeSize>());
}
这需要第三个模板参数来指定转换为目标元组的起始索引。
答案 1 :(得分:1)
已提及index_sequence
解决方案:
template <std::size_t From, size_t... indices, typename T1, typename T2, typename Func>
void transform(T1&& s, T2& t, Func f, std::index_sequence<indices...>)
{
(void)std::initializer_list<int>{
(std::get<indices+From>(t) = f(std::get<indices>(std::forward<T1>(s))), 0)...};
}
template <std::size_t From, std::size_t To, typename T1, typename T2, typename Func>
void transform(T1&& s, T2& t, Func f)
{
transform<From>(std::forward<T1>(s), t, f, std::make_index_sequence<To-From+1>());
}