2

I found this pieces for C++17 of code in case std::tuple/std::index_sequence should be handled by recursion:

Example 1)

template <class ... Ts>
constexpr T foo (const std::tuple<Ts...>&)
{ return ( bar(Ts{}) + ... ); }

In C++11/14 some people tend to use these approaches.

Example 2)

auto res = 0;
using unused = int[];
(void)unused { 0, ((void)(res += bar(Ts{})), 0)... };

Example 3)

auto res = 0;
const int dummy[] = {0, res += bar(Ts{})...};
(void) dummy;

However, there are two points which are not clear for me. Firstly, I want to know what the compiler is actually doing in 2) and 3)? Additionally, I noticed, that 3) fails for complex types (e.g. glm::vec3 and I wonder why not both fail, and both 2) and 3), have problems in type deduction for res because auto is declared before the result can be added while iterating. Can this be circumvented without adding an additional template parameter in C++11/14? I also wonder why the brackets Ts{} are necessary for 3)?

dgrat
  • 2,214
  • 4
  • 24
  • 46

0 Answers0