Compile time function execution...

Russell Lewis webmaster at villagersonline.com
Fri Feb 16 10:06:35 PST 2007


Derek Parnell wrote:
> On Thu, 15 Feb 2007 10:23:43 -0800, Walter Bright wrote:
> 
>> ... is now in DMD 1.006.
> 
> I guess its time I came clean and admitted that in spite of this being a
> huge technological advancement in the language, I can't see why I'd ever be
> needing it.
> 
> I mean, when it comes down to it, it's just a fancy way of getting the
> compiler to calculate/generate literals that can be done by myself anyway,
> no? These literals are values that can be determined prior to writing one's
> code, right?
> 
> This is not a troll posting, so can anyone enlighten me on how this ability
> will reduce the cost of maintaining code? I am very open to being educated.

It (sometimes) allows you to express things using the formulae that you 
used to derive them, which makes code more readable.  It also allows you 
to express mathematically things that might depend on 
implementation-dependent parameters, or versions, or whatever.

Say, like this:
	version(4K_PAGES)
		const int page_size = 4*1024;
	else
		const int page_size = 16*1024*1024;
	const int page_shift = eval!(log_base_2(page_size));

Sure, you could integrate page_shift into the version statement...but I 
think that the above is better.

P.S. I would prefer the template
	compile_time!(value)
over
	eval!(value)
for readability reasons.



More information about the Digitalmars-d mailing list