July 09, 2008
"BCS" <ao@pathlink.com> wrote in message news:55391cb32ef558caafbcbd0e7af2@news.digitalmars.com...
> Reply to Jarrett,
>
>> "BCS" <ao@pathlink.com> wrote in message news:55391cb32eed78caaf3194222520@news.digitalmars.com...
>>
>>> you can even force the format string to be built at compile time.
>>>
>>> char[] toHex(T)(T arg){ return format("%."~itoa!(T.sizeof * 2)~"x",
>>> arg); }
>>>
>> No function necessary.
>>
>> return format("%." ~ (T.sizeof * 2).stringof ~ "x", arg);
>>
>> Untested, but you can use .stringof to do number to string at compile time. Not sure if it'll give "T.sizeof * 2" instead here :P  might have to put it in a const.
>>
>
> I think that won't work. The only place I know that .stringof works that way is __LINE__.stringof but that's special cased in the lexer IIRC.

Just try it, please?

template Itoa(int x)
{
    const Itoa = x.stringof;
}

pragma(msg, (5).stringof); // "5"
pragma(msg, (3 + 4).stringof); // "3 + 4"
pragma(msg, Itoa!(3 + 4)); // "7"


July 09, 2008
Reply to Jarrett,

> "BCS" <ao@pathlink.com> wrote in message
> news:55391cb32ef558caafbcbd0e7af2@news.digitalmars.com...
> 
>> Reply to Jarrett,
>> 
>>> "BCS" <ao@pathlink.com> wrote in message
>>> news:55391cb32eed78caaf3194222520@news.digitalmars.com...
>>>> you can even force the format string to be built at compile time.
>>>> 
>>>> char[] toHex(T)(T arg){ return format("%."~itoa!(T.sizeof * 2)~"x",
>>>> arg); }
>>>> 
>>> No function necessary.
>>> 
>>> return format("%." ~ (T.sizeof * 2).stringof ~ "x", arg);
>>> 
>>> Untested, but you can use .stringof to do number to string at
>>> compile time. Not sure if it'll give "T.sizeof * 2" instead here :P
>>> might have to put it in a const.
>>> 
>> I think that won't work. The only place I know that .stringof works
>> that way is __LINE__.stringof but that's special cased in the lexer
>> IIRC.
>> 
> Just try it, please?
> 
> template Itoa(int x)
> {
> const Itoa = x.stringof;
> }
> pragma(msg, (5).stringof); // "5"
> pragma(msg, (3 + 4).stringof); // "3 + 4"
> pragma(msg, Itoa!(3 + 4)); // "7"

I stand corrected:

It didn't work last time I tried it. (I think I only managed to test case 2 & 1but with consts)


1 2
Next ›   Last »