Boost logo

Boost :

From: Karl Nelson (kenelson_at_[hidden])
Date: 2001-12-19 16:09:52


> On Wed, 2001-12-19 at 19:35, Karl Nelson wrote:
>
> > Why use the operator "%" for combining items. It is not streamlike
> > and introduces precidence problems which C++ coders do not normally
> > have with streams. Previous discussions (when I submitted my formatting
> > class) here concluded operator << was a better choice.
>
> Your code used a "endf" marker, to stop the format::operato<<
> and return to stream's.

The implemention I sent of format had no problems with extra
arguments.

  cout << format("A %2$s %1$s") << s1 << s2<< endl;
became
  operator<< (cout,"A ");
  operator<< (cout, s1);
  operator<< (cout," ");
  operator<< (cout, s2);
  operator<< (cout, endl);

For good reason. All extra agruments after the format
became regular operations.

Further, you can force it off with endf
  cout << format("A %s %s") << s1 << endf << s2;

(extra argument lost)

 
> eg, the following would be a problem :
> cout << format(" %1 %2") << x << y << "\n";
> // poor example, the \n would be happier inside the format-string.
> // but well..

> Because, accepting this, eg by making the format class act as a simple
> stream-wrapper when all the arguments have been fed, means you won't
> detect arity errors if the user forgets an argument.
>
> The alternative you chose, using an "endf" marker, brings robustness vs
> users error.
> But it's ugly.

Extreme shortness in typing charactors is not a
very good reason for a design decision.

 
> On the other hand, with operator% :
> 1. the code is smaller than with '<<' and, in my opinion, nicer.

If it acts like stream make it look like a stream. The charactors
saved are not worth force the user to have more concepts. Format
should be considered a manipulator and as such should not change the
syntax.

[...]
> point 3 is an inconvenient, but I think it's not a big price.
> If you forget to print (1+2), it means :
> ( format("%1 %2") % 1 ) + 2 % <the_rest_of_the_line>
> So it tries to call operator+ on a format object, => the error is
> detected at compile time.

What is wrong with 'cout << (format("%1 %2") << 1) ;' ?

The compiler should construct the format make its contents then
put it out when it hits the stream.

Just like
  basic_format<char, trait<char> > f("%1 %2");
  f << "a" << "b";
  cout << f;

Should allow the format to be made ahead of time.
(okay I am glossing over what should it do with
  cout << hex << format("%d") << 16;
But the point is there we could implement something
much niced and more stream like.)

--Karl


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk