5

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)
Sebasgr
  • 71
  • 3

1 Answers1

6

(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)...);
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
  • This is what I was somehow suspecting from the beginning but the compiler error confused me some how. Maybe, it's caused as the result of the "comma expression" is applied as first argument to the function call i.e. the last type is used for the first argument... – Scheff's Cat May 06 '22 at 05:52
  • Thanks for the answer! But now I have another problem with the template arguments, because the last arguments is used as the first and screw how the algorithm of my function that depend on the order of arguments – Sebasgr May 06 '22 at 20:17