C++: convert tuple to type T

834 views Asked by At

I'm trying to make a class, called tuple_cnv with an (implicit) conversion operator to construct any object from a tuple (like the C++17 std::make_from_tuple function), but of recursive nature, in such a way that, if a tuple consists of other tuples, it will transform any ìnner tuple to tuple_cnv to allow a recursive in-place construction 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()))...}

must expand the tuple in a comma-separated list of its elements (inside a {...} to get a initializer-list), but replacing every tuple element by a corresponding tuple_cnv to allow the creation of a tree of initializer-lists through the (implicit) conversion operator of each inner tuple_cnv when constructing the object T.

See the commented "intended equivalent" expresion inside the main function.

The thing is that I get a compiler error so big that I'm not able to understand what is wrong with my implementation:

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()))...}; }
                                                   ^

What is that compiler error about? What is what I don't see?

NOTE: Following the suggestion from @Barry, I have changed the implementation using apply, but called tuple_to_args instead because the implemention is not completly equivalent (std::apply uses std::invoke, what deals with different kind of functions like pointer to member functions):

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

And using tuple_to_args as auxiliary function, the implementation of the conversion operator has changed to:

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

The non-static p_convert function has been also removed, but the compiler error is still pretty similar:

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))...};
2

There are 2 answers

10
Barry On

The problem is here:

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

You made this as hard as possible to find errors in. You have two functions named the same that do different things (the p_convert() that gives you a T and the p_convert() that handles the recursion). That's confusing.

Instead, implement apply (since you're on C++14). Then use apply:

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

The problem was the p_convert function was returning an invalid value. Instead of returning a tuple_cnv<std::tuple<Ts...> > it returned a tuple_cnv<Ts...>.

Since tuple_cnv<Ts...> isn't an invalid type, because there was no instantiation of tuple allowing non-tuple types, the compiler has replaced that "unknown type", to int, because in old C, when a variable had no type specified (in very old C the variables could be introduced without an explicit type specified), by default was int.

So, the compiler was somehow trying to convert both inner std::tuple<int, int, int> to int, which isn't an invalid conversion.

The nice compiler error output was shown when writting return B{p_convert(std::forward<decltype(tuple)>(tuple))...} instead of just the initializer-list, which showed the full expression instead of the resolved types.

That's the complete code with some improvements:

#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
}