Unpacking variadic tuples in c++17

戏子无情 提交于 2021-02-04 17:27:50

问题


Is there anything better in c++17 (maybe C++2a) than the classic C++14 way to unpack variadic tuple with std::index_sequence?

Anything better than this:

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.


回答1:


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 );

note: for your information, the (void) thing is just to avoid any custom comma operator to kick in... you never know :)



来源:https://stackoverflow.com/questions/48464393/unpacking-variadic-tuples-in-c17

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!