Boost logo

Boost :

From: Andrey Semashev (andysem_at_[hidden])
Date: 2007-06-18 15:48:41

Ion Gaztañaga wrote:
> Sebastian Redl wrote:


> -> Hard formatting: printf/scanf are much easier to use and they don't
> need several hundred of function calls to do their job. The operator<<
> overloading is also a problem if each call needs some internal locking.
> A formatting syntax that can minimize the locking needs would be a good
> choice. I would be really happy with a type-safe printf (variadic
> templates to the rescue).

I'd rather disagree with you here. Operator<< gives a great advantage of
extensibility which printf cannot offer. I can define my own classes and
overload the operator for them, and the IO system will work as expected
with my extensions. The same thing with operator>> and scanf.

I have to admit though, that indeed formatting is not a bright side of
the current IO design. I think a better set of manipulators for basic
primitives should do good. There might even be a printf-like manipulator:

int x = 300;
double y = 1.2;

// Output: "Hello: 0x0000012c; 1,200"
cout << "Hello: "
    << format("0x%08x", x) << "; "
    << fixed(y, point = ',', precision = 3) << endl;

vector< int > v;

// Output: "1, 2, 3"
cout << range(v.begin(), v.end(), separator = ", ") << endl;

And of course, widely used dump manipulator is an often requested feature.

BTW, I think this domain is well suitable to be covered by yet another
Boost library. :)

Boost list run by bdawes at, gregod at, cpdaniel at, john at