Casting between tuples

Jesse Phillips jessekphillips+D at
Wed Jan 12 14:20:14 PST 2011

Sean Eskapp Wrote:

> I have a variable of type TypeTuple!(int, int), and I want to convert all its
> elements into a variable of type TypeTuple!(string, string). Is there a way to
> do this? Using a loop fails compilation with "Error: Integer constant
> expression expected instead of i".
> I'd also like to be able to convert from arrays of base classes (static size)
> to TypeTuples of derived classes. "static for" doesn't work either. Is there a
> way to do this.. without a recursive template function?

Maybe I don't understand what you want, but it sounds like you want to convert runtime information into compile time information?

If you have a Tuple of int you can't convert the elements to string because you don't know what they are at compile time.

Converting a TypeTuple doesn't make sense all as it has is Type information and if you want a TypeTuple!(string, string) then use that.

auto something = cast(string) int;


An array of base class, Base[3] foo does not make sense to convert to TypeTuple!(Dirived1, Dirived2, Dirived1) bar.

But if you know that is how Base class is laid out then you can convert them to the proper type (I don't think this will compile, and if it did, wouldn't do what you expected):

foreach(i, item; foo)
    auto myClass = to!(bar[i])(item);

But that is still a weird. what is you are trying to do?

More information about the Digitalmars-d-learn mailing list