将std::array转换成std::tuple

template<typename Array, std::size_t... Index>
decltype(auto) array2tuple_impl(const Array& a, std::index_sequence<Index...>)
{
    return std::make_tuple(a[Index]...);
}

template<typename T, std::size_t N>
decltype(auto) array2tuple(const std::array<T, N>& a)
{
    return array2tuple_impl(a, std::make_index_sequence<N>{});
}

使用:

std::array<int, 4> a { 1, 2, 3, 4 };
auto t = array2tuple(arr);

这里用到了C++14的std::index_sequence,std::index_sequence很有用,它可以将std::array和std::tuple转换成序列。

原文链接: https://www.cnblogs.com/highway-9/p/5955519.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    将std::array转换成std::tuple

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/242028

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午10:02
下一篇 2023年2月13日 下午10:04

相关推荐