On 2/21/18 1:45 AM, thorstein wrote:
Hi,
I'm going circles... ;) I read a string that contains an array of
unknown dimension like:
a = [1,2,3,4] or
a = [[1,2],[3,4]] or
a = [[[1,2],[3,4]],[[5,6],[7,8]]]
With that I want to perform specified operations e.g. also provided on
command line.
Because at compile time the dimension of the array is unkown I did not
find any possibility to do something like:
auto arr = func(a);
where 'auto func(string str)' should return either a 1D, 2D, 3D array
and arr is not locked in an if(){} scope.
Is there any way and What should I'm looking for?
If you want a specific type that the type system cares about, you have
to implement every possibility. Then you can use Variant.
But if you want a recursive strategy, where you need the dimensions at
runtime only, then you can write a struct with a tagged union which
gives you all the info you need:
struct NDimArray
{
size_t dimensions; // if 0, then this is a value
union {
NDimArray[] elements;
int value;
}
}
You can probably figure out the rest. Probably you can eliminate the
dimensions field somehow to save space.
This is how things like JSON DOM structures work.
-Steve