C ++: convert tuple to type T

I am trying to create a class called tuple_cnvwith a (implicit) conversion operator to build any object from a tuple (for example, C ++ 17 std::make_from_tuple), but of a recursive nature, in a way that, if a tuple consists of other tuples, converts any "tuple" into tuple_cnvto enable the recursive construct in place of the target type:

#include <iostream>
#include <utility>
#include <tuple>
#include <functional>

struct A { int i1, i2, i3; };
struct B { A a1, a2; };

template<class T> struct tuple_cnv;

template<class... Ts>
struct tuple_cnv<std::tuple<Ts...> >
{
    using tuple_t = std::tuple<Ts...>;
    std::reference_wrapper<tuple_t const> ref;

    tuple_cnv(tuple_t const& t) : ref(t) {}

    template<class T>
    operator T() const 
    { return p_convert<T>(std::index_sequence_for<Ts...>{}); }

private:
    template<class T>
    static T const& p_convert(T const& t) { return t; }

    template<class... Tss>
    static tuple_cnv<Tss...> p_convert(std::tuple<Tss...> const& t)
    { return tuple_cnv<std::tuple<Tss...> >(t); }

    template<class T, std::size_t... I>
    T p_convert(std::index_sequence<I...>) const
    { return {p_convert(std::get<I>(ref.get()))...}; }
};

template<class T>
auto make_tuple_cnv(T const& t) { return tuple_cnv<T>(t); }

using tup = std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> >;

int main()
{
    tup t{{3, 4, 5}, {1, 7, 9}};

    // Equivalent to: B b{{3,4,5}, {1,7,9}};
    B b = make_tuple_cnv(t);

    std::cout << b.a2.i3 << std::endl;
}

In case of doubt, the line:

{p_convert(std::get<I>(ref.get()))...}

should expand the tuple in the comma-separated list of its elements (inside {...}to get a list of initializers), but replacing each tuple element with the corresponding tuple_cnvone to allow the creation of the -List initializer tree via the (implicit) conversion operator of each internal tuple_cnvwhen constructing the object T.

. " " main.

, , , :

main.cpp: In instantiation of 'T tuple_cnv<std::tuple<_Tps ...> >::p_convert(std::index_sequence<I ...>) const [with T = B; long unsigned int ...I = {0, 1}; Ts = {std::tuple<int, int, int>, std::tuple<int, int, int>}; std::index_sequence<I ...> = std::integer_sequence<long unsigned int, 0, 1>]':
main.cpp:28:26:   required from 'tuple_cnv<std::tuple<_Tps ...> >::operator T() const [with T = B; Ts = {std::tuple<int, int, int>, std::tuple<int, int, int>}]'
main.cpp:53:27:   required from here
main.cpp:40:51: error: could not convert '{tuple_cnv<std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> > >::p_convert<std::tuple<int, int, int> >((* & std::get<0, std::tuple<int, int, int>, std::tuple<int, int, int> >((* &((const tuple_cnv<std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> > >*)this)->tuple_cnv<std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> > >::ref.std::reference_wrapper<const std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> > >::get())))), tuple_cnv<std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> > >::p_convert<std::tuple<int, int, int> >((* & std::get<1, std::tuple<int, int, int>, std::tuple<int, int, int> >((* &((const tuple_cnv<std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> > >*)this)->tuple_cnv<std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> > >::ref.std::reference_wrapper<const std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> > >::get()))))}' from '<brace-enclosed initializer list>' to 'B'
     { return {p_convert(std::get<I>(ref.get()))...}; }
                                                   ^

? ?

.. @Barry, apply, tuple_to_args , (std::apply std::invoke, , -):

template<class... Ts>
constexpr auto indexes(std::tuple<Ts...> const&)
{ return std::index_sequence_for<Ts...>{}; }

template<class fun_t, class tuple_t, std::size_t... I>
decltype(auto) tuple_to_args(fun_t&& f, tuple_t&& tuple, std::index_sequence<I...> const&)
{ return f(std::get<I>(std::forward<tuple_t>(tuple))...); }

template<class fun_t, class tuple_t>
decltype(auto) tuple_to_args(fun_t&& f, tuple_t&& t)
{ return tuple_to_args(std::forward<fun_t>(f), std::forward<tuple_t>(t), indexes(t)); }

tuple_to_args , :

template<class T>
operator T() const 
{
    auto inner_f = [](auto&&... tuple) -> T {
        return {p_convert(std::forward<decltype(tuple)>(tuple))...};
    };

    return tuple_to_args(inner_f, ref.get());
}

p_convert , :

main.cpp: In instantiation of 'tuple_cnv<std::tuple<_Tps ...> >::operator T() const::<lambda(auto:1&& ...)> [with auto:1 = {const std::tuple<int, int, int>&, const std::tuple<int, int, int>&}; T = B; Ts = {std::tuple<int, int, int>, std::tuple<int, int, int>}]':
main.cpp:15:11:   required from 'decltype(auto) tuple_to_args(fun_t&&, tuple_t&&, std::index_sequence<I ...>&) [with fun_t = tuple_cnv<std::tuple<_Tps ...> >::operator T() const [with T = B; Ts = {std::tuple<int, int, int>, std::tuple<int, int, int>}]::<lambda(auto:1&& ...)>&; tuple_t = const std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> >&; long unsigned int ...I = {0, 1}; std::index_sequence<I ...> = std::integer_sequence<long unsigned int, 0, 1>]'
main.cpp:19:23:   required from 'decltype(auto) tuple_to_args(fun_t&&, tuple_t&&) [with fun_t = tuple_cnv<std::tuple<_Tps ...> >::operator T() const [with T = B; Ts = {std::tuple<int, int, int>, std::tuple<int, int, int>}]::<lambda(auto:1&& ...)>&; tuple_t = const std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> >&]'
main.cpp:38:29:   required from 'tuple_cnv<std::tuple<_Tps ...> >::operator T() const [with T = B; Ts = {std::tuple<int, int, int>, std::tuple<int, int, int>}]'
main.cpp:60:27:   required from here
main.cpp:35:71: error: could not convert '{tuple_cnv<std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> > >::p_convert<std::tuple<int, int, int> >((* & std::forward<const std::tuple<int, int, int>&>((* & tuple#0)))), tuple_cnv<std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> > >::p_convert<std::tuple<int, int, int> >((* & std::forward<const std::tuple<int, int, int>&>((* & tuple#1))))}' from '<brace-enclosed initializer list>' to 'B'
             return {p_convert(std::forward<decltype(tuple)>(tuple))...};
+4
2

:

template<class... Tss>
static tuple_cnv<Tss...> p_convert(std::tuple<Tss...> const& t)
{ return tuple_cnv<std::tuple<Tss...> >(t); }

, . , , (p_convert(), T p_convert(), ). .

apply ( ++ 14). apply:

template <class T>
operator T() const {
    return std::apply([](auto const&... elems) -> T {
        return {p_convert(elems)...};
    }, ref);
}
+4

, p_convert . tuple_cnv<std::tuple<Ts...> > tuple_cnv<Ts...>.

tuple_cnv<Ts...> , tuple, , " " int, C, ( C ), int.

, - std::tuple<int, int, int> int, .

return B{p_convert(std::forward<decltype(tuple)>(tuple))...} , .

:

#include <iostream>
#include <utility>
#include <tuple>
#include <functional>

struct A { int i1, i2, i3; };
struct B { A a1, a2; };

template<class... Ts>
constexpr auto indexes(std::tuple<Ts...> const&)
{ return std::index_sequence_for<Ts...>{}; }

namespace impl {

    template<class fun_t, class tuple_t, std::size_t... I>
    decltype(auto) tuple_to_args(fun_t&& f, tuple_t&& tuple, std::index_sequence<I...> const&)
    { return f(std::get<I>(std::forward<tuple_t>(tuple))...); }

}

template<class fun_t, class tuple_t>
decltype(auto) tuple_to_args(fun_t&& f, tuple_t&& t)
{ return impl::tuple_to_args(std::forward<fun_t>(f), std::forward<tuple_t>(t), indexes(t)); }

namespace impl {
    template<class T>
    struct tuple_cnv;
}

template<class T>
auto tuple_cnv(T const& t) { return impl::tuple_cnv<T>(t); }

namespace impl {

template<class tuple_t>
class tuple_cnv
{
    std::reference_wrapper<tuple_t const> ref;

public:   
    explicit tuple_cnv(tuple_t const& t) : ref(t) {}

    template<class T>
    operator T() const 
    {
        auto inner_f = [](auto&&... elements) -> T {
            return {p_convert(std::forward<decltype(elements)>(elements))...};
        };

        return ::tuple_to_args(inner_f, ref.get());
    }

private:
    template<class T>
    static decltype(auto) p_convert(T&& t) { return std::forward<T>(t); }

    template<class... Tss>
    static auto p_convert(std::tuple<Tss...> const& t)
    { return ::tuple_cnv(t); }
};

}

using tup = std::tuple<std::tuple<int, int, int>, std::tuple<int, int, int> >;

int main()
{
    tup t{{3, 4, 5}, {1, 7, 9}};
    B b = tuple_cnv(t);

    std::cout << b.a2.i3 << '\n'; // It prints 9
}
0

Source: https://habr.com/ru/post/1685555/


All Articles