Different results for int and long alias
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Sun Mar 4 02:34:24 PST 2007
Andy Little wrote:
> Lionello Lunesu Wrote:
>
>> Andy Little wrote:
>>> In the following example when I use an int in the marked section the code works as expected, however if I use a long the output is incorrect.
>>>
>>> Coming from C++ I don't understand the casting system. Is this the cause or is this a bug?
>> "long" in D is a 64-bit integer, therefore you should use "%ld" to print
>> it using printf. Generally, try to cut back on the use of printf since
>> it doesn't check the arguments, causing many similar problems. You
>> should use writefln/writef (from std.stdio) instead. In writefln you can
>> simply use "%s" for all types.
>
> Great it works.. thanks.
>
> Presumably this means you need to provide an overload of some "toString(T)" function for your own types?
Yes, except it's not a free function. Classes and structs (and IIRC
unions too) that you want to pass to the writef[ln]() and format()
functions should implement a "char[] toString()" member function.
(For classes this comes pre-implemented in Object to return the class
name, but that's not very informative. It's provided mainly for
implementation reasons, so it can be overridden)
More information about the Digitalmars-d-learn
mailing list