Iterate over symbols in tupleof without instance

Rikki Cattermole alphaglosined at gmail.com
Tue Apr 15 01:44:10 PDT 2014


On Tuesday, 15 April 2014 at 08:18:31 UTC, Benjamin Thaut wrote:
> I would like to iterate over T.tupleof at compiletime without 
> having a instance of the given type. So fare the only way I 
> found that works is the following
>
> void iterateImpl(T, size_t i)()
> {
>   static if(i < T.tupleof.length)
>   {
>      // do something with T.tupleof[i]
>      iterateImpl!(T, i+1)();
>   }
> }
>
> void iterate(T)()
> {
>   iterateImpl!(T, 0)();
> }
>
> But this is quite ugly, as there will tons of stack frames on 
> the stack during runtime. I really wanted something like:
>
> void iterate(T)()
> {
>
>   foreach(size_t i, m; T.tupleof)
>   // error message: "Need this to access <member name here>"
>   {
>      // do something with m
>   }
> }
>
> Kind Regards
> Benjamin Thaut

Kinda like this?

import std.stdio;
void test(T...)(T args) {
	if (!__ctfe)
		writeln(args);
	foreach(arg; T) {
		pragma(msg, arg);	
	}
}

void main() {
	test("a", "b", "c", 7, 2, 9);	
}

Compilation output:
string
string
string
int
int
int

Application output:
abc729


More information about the Digitalmars-d-learn mailing list