access CTFE variables at compile time.

JS js.mdnq at gmail.com
Tue Jul 9 15:49:33 PDT 2013


It seems possible that we can print CTFE variables at compile 
time by using string mixes as the code below demonstrates.

The problem is that we can not pass a variable to a template to 
create a print routine in the first place. e.g., we can't do 
mixin a!(s) if s is a string since s can't be read, supposedly, 
at compile time.

We can't even do mixin("mixin a!(\""~s~"\");") though to get 
around this.

The following code at least proves that CTFE variables can be 
read at compile time and the limitation is with the internal 
implementation.

(this is easy to see, because on one hand we can't print s but on 
the other we can... hence the limitation with pragma is an 
artificial one.)

import std.stdio, std.cstream, std.conv;

mixin template a()
{
	template b()
	{
		static string c()
		{
			
			string s = "a"; s ~= "b";
			string x = "pragma(msg, \""~s~"\");";
			// pragma(msg, s) // fails
			return x;
		}
		enum b = c();
	}
	mixin(b!());
}

int main(string[] argv)
{
	mixin a!(); // can't pass a string to a in any way because of 
artificial  ctfe limitation
	din.getc();
	return 0;
}


More information about the Digitalmars-d mailing list