Vladp - 4 months ago 22

C++ Question

I have an input of arrays of different types (

`void**`

for example two arrays of

`ints`

`chars`

I the number of arrays and their types is unknown at the compilation time, so I can't just

`thrust::make_tuple`

`make_tuple`

I'm looking for a more convenient way to do so, I tried this simple example of turning array into a tuple:

`template<typename T>`

thrust::tuple<int, T> makeBiggerTuple(T tuple, int* array, int size)

{

if (size > 1)

return makeBiggerTuple(thrust::make_tuple(*array, tuple), array++, size - 1);

return thrust::make_tuple(*array, tuple);

}

but this won't compile at all because it expand it self on and on until an error occurs.

So is there any way of avoiding lots of hardcoded lines for every case?

I actually need a tuple of their iterators but it's an equivalent question.

Answer Source

You want a `std::vector`

of `boost::variant`

, or a hand rolled equivalent.

Doing what you asked at compile time with `tuple`

s results in exponential bloat and is rarely the right thing to do. While I could write code that takes arrays of up to size 10 with up to 10 elements and calls a function with a custom tuple, the resulting 10 billion generated functions would blow up your compiler: and unless you want every function in your binary to also be 10 billion times larger, we would have to type erase the result anyhow.

As a general rule, use dynamic structures for dynamic data: we have a dynamic length, so `vector`

, and the data is of run time typed from a finite set, so `variant`

.