compile-time variables?

Pragma ericanderton at yahoo.removeme.com
Tue May 22 12:45:31 PDT 2007


BCS wrote:
> Reply to Robert,
> 
>> Hi,
>>
>> D has some pretty impressive metaprogramming features, but I was
>> wondering if there was any way to do compile-time variables, or fake
>> it with recursive templates or something. In particular, I want a
>> mixin template that mixes in a  unique numeric ID as part of a
>> function every time it's used in a mixin. Thouands of these functions
>> will be used in the program, and each must have a unique ID in an
>> associative array (after they're called for the first time, lazy eval
>> is being done at runtime).
>>
>> If I can avoid code bloat, all the Better!
>>
>> Thanks,
>> Fraser
> 
> it's does stuff at runtime but this should work
> 
> int val = 0
> 
> class UID(char[] file, int line, A...)
> {
>  static const uid;
>  static this(){uid = val++}
> }
> 
> UID!(__FILE__, __LINE__).uid
> 
> if it is used in a template, add enough of the template args to make 
> each instance have different args.
> 
> 

BCS, I like that hack.

Technically, isn't this a runtime solution since the evaluation of 'val++' is done at runtime?  Then again, if all 
Robert wants is a registry of template instances or somesuch (regardless of performance), this might do the trick.

-- 
- EricAnderton at yahoo



More information about the Digitalmars-d mailing list