Enum and CTFE function call

ixid nuaccount at gmail.com
Tue Aug 14 09:12:30 UTC 2018


This will not compile as it says n is not known at compile time:

auto fun(int n) {
	static foreach(i;0..n)
		mixin(i.to!string ~ ".writeln;");
	return;	
}

enum value = 2;


void main() {
	fun(value);
}

But making it a template parameter fun(int n)() and fun!value 
will obviously work as will replacing n in the foreach statement 
with the enum 'value'. It seems like a missed opportunity that 
the enum nature of 'value' does not propagate through the 
function call letting the compiler know that 'value'and therefore 
n are known at compile time.


More information about the Digitalmars-d-learn mailing list