Why does toString() exist? It seems useless.

Don nospam at nospam.com.au
Mon Aug 18 03:41:33 PDT 2008


I cannot understand the rationale for a toString() member function which 
doesn't support formatting.

I don't think I've ever made a class which only has a single format 
option. If the class contains any integers, I want to specify whether it 
should use hex or decimal, leading zeros, sign. If it contains floating 
point numbers, it's number of digits, scientific notation, hex or 
decimal, and what to do with NaNs. And of course there are locales.

Seriously, I cannot remember ever not having this requirement, so I 
cannot imagine a use for toString().

C++ got around this by giving state to the iostream classes. As far as I 
can tell, neither Phobos nor Tango provide any support at all.

Practical example: BigInt (which I'm developing in both Tango and 
Phobos). It is a struct. How can it support I/O? I can't work out how to 
do it in either library.


More information about the Digitalmars-d-learn mailing list