17

I am working on a aux module to pass values between polymorphic objects and at some point I have

std::array<void*, N>

and need to send forward

std::tuple<void*, void*, /* N times */>

I can figure some solution with the use of index_sequence or/and recursions, but all of those look bulky and difficult to read.
Is there any more straightforward way to do this by the means of the standard library?

Also, just in case - am I right that the layout of std::array is a dense set of respective objects, thus equal to, lets say, void** of respective length, whereas the layout of tuple allows gaps?

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Glib
  • 304
  • 1
  • 13
  • Not actually a duplicate. The former question is about more specific problem and for some reason has no satisfying answer (so asking it again anyway was useful). – Glib Aug 15 '19 at 18:17

1 Answers1

21

If your implementation supports it, you can use std::tuple_cat. Under some implementations it concatenates any number of objects that respect the tuple interface (which std::array does) into a single flat tuple. Concatenating a single tuple like object will just produce a tuple that holds copies of the members of said source "tuple".

std::array<void*, N> a;
auto b = std::tuple_cat(a);

Also, just in case - am I right that the layout of std::array is a dense set of respective objects, thus equal to, lets say, void** of respective length, whereas the layout of tuple allows gaps?

A std::array is an aggregate the will hold a void*[N] internally. So yes, the elements will be without padding in between them. The layout of the tuple's elements is not specified to such an extent.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458