Swap furthest element type of an array
Andrej Mitrovic
andrej.mitrovich at gmail.com
Mon Jun 18 09:29:41 PDT 2012
I just had a need for this but couldn't find it in Phobos:
import std.stdio;
import std.traits;
import std.range;
template SwapElem(Arr, Type)
{
static if (isArray!(ElementType!Arr))
{
static if (isDynamicArray!Arr)
alias SwapElem!(ElementType!Arr, Type)[] SwapElem;
else
alias SwapElem!(ElementType!Arr, Type)[Arr.length] SwapElem;
}
else
{
static if (isDynamicArray!Arr)
alias Type[] SwapElem;
else
alias Type[Arr.length] SwapElem;
}
}
void main()
{
alias int[2][1] IntArr;
alias SwapElem!(IntArr, float) FloatArr;
writeln(typeid(FloatArr)); // float[2][1]
}
It doesn't handle complex declarations like "int[2]*[1]". It seems
hard to do since I can't pass a naked pointer as a type to a template.
Some other clever tricks would have to be used here. But it's a worthy
start. If you can improve it feel free to do so. :)
More information about the Digitalmars-d-learn
mailing list