3

Currently I think I can do the following

apply([](auto&... e) { forward_as_tuple((e.do_something(), 0)...); }, t);

Here t is the tuple. This way is not easy to read. Any other ways? Thanks.

user1899020
  • 13,167
  • 21
  • 79
  • 154

1 Answers1

4

Basically, no. Because tuples are heterogeneous, you cannot have something like:

for (auto e : my_tuple)

because e would potentially need to be a different type in each iteration - and that's a very different kind of iteration than what we're used to in the language. P1306 is proposing a completely new kind of for loop to address this problem, spelled for...

Now, I said you need a different type in each iteration. That's not strictly true. If your tuple is homogenous, you could just turn it into a std::array. And even if your tuple is heterogeneous, still turn it into an array of std::any or an array of std::variant<Ts...>, consisting of all the unique types that the tuple happens to contain. Maybe that solves your problem, maybe it doesn't.

Otherwise, the only way in the language today is to unpack your tuple through a new scope. That could either be std::apply (which gives you all the elements in one go) or you could write your own version of it which passes in one element at a time, so you get unary lambdas.

Barry
  • 286,269
  • 29
  • 621
  • 977