Base type for arrays

Alex Parrill via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 17 13:31:32 PDT 2015


On Wednesday, 17 June 2015 at 20:20:29 UTC, jmh530 wrote:
> On Wednesday, 17 June 2015 at 20:06:54 UTC, Alex Parrill wrote:
>
>>
>> Try:
>>
>> void foo(T)(T[] arg) {
>>     // In here, T should be the element type, and T[] the 
>> array type.
>> }
>>
>> Not a general solution, but you mentioned that you wanted this 
>> for a function parameter.
>
> I don't think this works for multi-dimensional arrays. I tried
>
> void foo(T)(T[] arg)
> 	if (isArray!(T[]) && is(T == real))
> {
>     writeln("foo has run for ", arg);
> }
>
> and it only worked on the one-dimensional one.

Yea, it doesn't, it only goes one-level deep. I misunderstood.

Here's a recursive template:

template ArrayBaseType(T) {
	static if(is(T : U[], U))
		alias ArrayBaseType = ArrayBaseType!U;
	else
		alias ArrayBaseType = T;
}

pragma(msg, ArrayBaseType!(int[][][])); // prints 'int'



More information about the Digitalmars-d-learn mailing list