Error - Structured binding with TTuple or TPair shows errors in 4.26

AFAIK, in order to support structured binding for non-std:: types, one has to:

  1. Specialize the following
    1. std::tuple_size
    2. std::tuple_element
  2. Provide
    1. Either a specialization of std::get()
    2. Or a get() function that is in the global namespace
    3. Or a get() method (i.e. part of the class)

In my tests, the following seems to do the trick for TTuple

// Add support for structured binding to TTuple

#include <tuple>        // tuple...
#include <type_traits>  // integral_constant

namespace std
{
    template <typename... Types> struct tuple_size<TTuple<Types...>> : integral_constant<size_t, sizeof...(Types)> {};

    template <size_t Idx, typename... Types> struct tuple_element<Idx, TTuple<Types...>> : tuple_element<Idx, tuple<Types...>> {};

    template <size_t Idx, typename... Types> decltype(auto) get(const TTuple<Types...>&  t) { return(t.template Get<Idx>()); }
    template <size_t Idx, typename... Types> decltype(auto) get(const TTuple<Types...>&& t) { return(t.template Get<Idx>()); }
    template <size_t Idx, typename... Types> decltype(auto) get(      TTuple<Types...>&& t) { return(t.template Get<Idx>()); }
}

// Test support for structured binding in TTuple

#include <cinttypes>  // PRId32... https://en.cppreference.com/w/c/types/integer#Format_macro_constants

{
    TTuple<int32, int32> Tuple{1,2};
    auto [x, y] = Tuple;
    UE_LOG(LogTemp, Warning, TEXT("[x:%"PRId32"] [y:%"PRId32"]"), x, y);  // LogTemp: Warning: [x:1] [y:2]
}

HTH

1 Like