Vladp Vladp - 9 months ago 50
C++ Question

Thrust tuple of dynamic size

I have an input of arrays of different types (


for example two arrays of
, and I want to make an array of tuples out of them.

I the number of arrays and their types is unknown at the compilation time, so I can't just
, I can split it into all the possibilities, but since I can have 10 types and 10 arrays it will be 100 hardcoded lines of

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 tuples 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download