'Creating a tuple from a folding expression return values

I try using the returning values of DecodeInternal function to create a tuple like this.

    std::tuple<Types...> Decode(const uint8_t *bufferBegin)
    {
        uint8_t *offset = (uint8_t *)bufferBegin;
        // (DecodeInternal<Types>(f, &offset), ...);
        return std::make_tuple<Types...>((DecodeInternal<Types>(&offset), ...));
    }

But when I compile with this template arguments

decoder.Decode<int, float, bool, std::string, std::string>(encoder.GetBuffer().data());

I get this error from the compiler

In file included from ./borsh-cpp/src/main.cpp:4:
./borsh-cpp/src/BorshCpp.hpp: In instantiation of ‘std::tuple<_Tps ...> BorshDecoder::Decode(const uint8_t*) [with Types = {int, float, bool, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >}; uint8_t = unsigned char]’:
./borsh-cpp/src/main.cpp:42:110:   required from here
./borsh-cpp/src/BorshCpp.hpp:244:35: error: invalid initialization of reference of type ‘int&&’ from expression of type ‘std::__cxx11::basic_string<char>’
  244 |   return std::make_tuple<Types...>((DecodeInternal<Types>(f, &offset), ...));
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/functional:54,
                 from ./borsh-cpp/src/BorshCpp.hpp:4,
                 from ./borsh-cpp/src/main.cpp:4:
/usr/include/c++/9/tuple:1470:27: note: in passing argument 1 of ‘constexpr std::tuple<typename std::__decay_and_strip<_Elements>::__type ...> std::make_tuple(_Elements&& ...) [with _Elements = {int, float, bool, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >}]’
 1470 |     make_tuple(_Elements&&... __args)


Solution 1:[1]

(DecodeInternal<Types>(&offset), ...) is a comma operator whose value is equal to the result of the last expression. Also, you don't need to explicitly specify template arguments for make_tuple, so just

return std::make_tuple(DecodeInternal<Types>(&offset)...);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 康桓瑋