Templates considered impressive

Andy Valencia dont at spam.me
Tue Oct 1 01:00:08 UTC 2024


I had an old atoi() I wrote when I was getting started with D.  
You know, fixed type, string in, int output.  Today for fun I 
templated the type:

T atoi(T)(string s)

and used static if to add support for signed/unsigned.  Easy.  
Then added floating point so it can parse "123.456" (no 
exponents... yet).  A conversion like:

     auto d = atoi!double("123.456");

is about 4k of code.  Nice!

Andy



More information about the Digitalmars-d-learn mailing list