|
Boost : |
From: Paul A. Bristow (boost_at_[hidden])
Date: 2001-12-13 16:32:45
Although this is harder work (more typing) I agree that it is better
than all the formatters that have been proposed. In my view they all suffer
from being write-only. Once one is familar with the << way of doing things,
and are willing to accept that they mean more typing, they are much easier
to read than the compact but cryptic format strings.
Paul
Dr Paul A Bristow, hetp Chromatography
Prizet Farmhouse
Kendal, Cumbria
LA8 8AB UK
+44 1539 561830
Mobile +44 7714 33 02 04
mailto:pbristow_at_[hidden]
> -----Original Message-----
> From: nbecker_at_[hidden] [mailto:nbecker_at_[hidden]]On Behalf Of
> nbecker_at_[hidden]
> Sent: Thursday, December 13, 2001 1:43 PM
> To: boost_at_[hidden]
> Subject: Re: [boost] formatter
>
>
> An approach I have used to a limited degree is to convert objects to
> be printed into a special print object that encapsulates all the
> information necessary to print into the desired format.
>
> For example, I want to print a number in hex with some special
> format:
>
> struct HexObj {
> HexObj (int _x, int _bits) : x (_x), bits (_bits) {}
> int x;
> int bits;
> };
>
> inline ostream& operator << (ostream& os, const HexObj& h) {
> return PrintHex (os, h.bits, h.x, 0);
> }
>
> cout << hexobj (2, <parameters>);
>
> In case you're wondering:
>
> inline ostream& PrintHex (ostream& os, int width, int x, char ret
> = '\n') {
> const int mask = ~(unsigned(-1) << width);
> os << std::hex << std::setw ((width+3)/4) << std::setfill ('0')
> << (x & mask);
> if (ret)
> os << ret;
> return os;
> }
>
> Info: http://www.boost.org Send unsubscribe requests to:
> <mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk