Template question

Thomas Kuehne thomas-dloop at kuehne.cn
Wed Oct 4 17:31:41 PDT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chad J schrieb am 2006-10-04:
> Sean Kelly wrote:
>> Chad J > wrote:
>> 
>>> I'm trying to write a template that, when instantiated, gives a value 
>>> that is the same as the last time it was instantiated plus one.
>> 
>> 
>> I don't think this is possible.  Once a template is evaluated that's it. 
>>  Future references to a template do not cause a re-evaluation.
>> 
>> 
>> Sean
>
> Oh, bummer.  Is there another way to do what I want to do at 
> compile-time, besides just manually writing the numbers in?

Here is a kludge(limitation: only UID instantiation per line and file allowed):

#
# template UID(char[] file, int line){
#     const uint UID = line * Hash!(file);
# }
# 
# template Hash(char[] seed){
#     static if(seed.length == 1){
#         const uint Hash = seed[0];
#     }else{
#         const uint Hash = Hash!(seed[1 .. $]) * 9 + seed[0];
#     }
# }
# 
# const x = UID!(__FILE__, __LINE__);
# const y = UID!(__FILE__, __LINE__);
#

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFFJF8MLK5blCcjpWoRArtDAJ42K2nEhKcEWNvpp3q+LjHVdBzAfwCfa8dT
rS6sQyT7s1XUic1TLz3Y2+A=
=+5nW
-----END PGP SIGNATURE-----



More information about the Digitalmars-d-learn mailing list