How to get the element type of an array?

Harry Gillanders contact at harrygillanders.com
Tue Aug 25 04:08:32 UTC 2020


On Tuesday, 25 August 2020 at 03:41:06 UTC, Jon Degenhardt wrote:
> What's the best way to get the element type of an array at 
> compile time?
>
> Something like std.range.ElementType except that works on any 
> array type. There is std.traits.ForeachType, but it wasn't 
> clear if that was the right thing.
>
> --Jon

One way (maybe not the best way) is to use the type of the 
array's ptr property when dereferenced, e.g.

	import std.traits : isArray;

	template ArrayElementOf (Array)
	if (isArray!Array)
	{
		alias ArrayElementOf = typeof(*Array.init.ptr);
	}



More information about the Digitalmars-d-learn mailing list