在C ++ 17中解压缩可变参数元组 [英] Unpacking variadic tuples in c++17
本文介绍了在C ++ 17中解压缩可变参数元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Is there anything better in c++17 (maybe C++2a) than the classic C++14 way to unpack variadic tuple with std::index_sequence
?
有什么比这更好的了:
template <typename ...I>
class MultiIterator
{
public:
MultiIterator(I const& ...i)
: i(i...)
{}
MultiIterator& operator ++()
{
increment(std::index_sequence_for<I...>{});
return *this;
}
private:
template <std::size_t ...C>
void increment(std::index_sequence<C...>)
{
std::ignore = std::make_tuple(++std::get<C>(i)...);
}
std::tuple<I...> i;
};
类似于折叠表达式,
Like fold expression, structured-bindings? Any hint? I can accept answer why I cannot use these mentioned C++17 features here - but I prefer "solution.
推荐答案
自C ++ 14起,我们具有通用的lambda,而自C ++ 17起,我们具有有效的折叠表达式和 std :: apply
隐藏通常的拆包逻辑:
Since C++14 we have generic lambdas, and since C++17 we have fold expressions and std::apply
effectively hiding the usual unpack logic:
std::apply( [](auto&... i){ ((void)++i,...); }, some_tuple );
注意:为您提供信息,(void)只是为了避免任何自定义逗号运算符插入...您永远不会知道:)
这篇关于在C ++ 17中解压缩可变参数元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文