Boost logo

Boost :

From: Karl Nelson (kenelson_at_[hidden])
Date: 2000-08-22 13:42:48


> It was the statement about forgetting the endf that gave the impression of
> globals. I see that the question now is how does format handle recieving a format
> parameter? I believe that's the question you were asking above.

Yes, that is it.

 
> It seems like, if I'm understanding the code above, centering should be fairly
> simple, unfortunately it would require more options like %15cs and some people
> don't like having to remember things like that. Personally, I think that's what
> documentation is for.

C format specified there would only be one letter in any format.
Thus with regards to centering which C printf lacks, I would chose
something appropriate from the same scheme.

C printf specification.

   ' - precision = 2 for monetary
   - - left justify
   + - (ignored) should begin with a + or - sign.
  ' ' - (ignored) opposite of +
   # - (ignored) add 0x to hex and extra zeros for %f.
   0 - pad with zeros

(the three ignored ones are ones that I believe there is no
proper stream equivalent. If I find one I will use it.)
  
Thus if we want centering I would just add

   ^ - center

(%25cs would mean a char 25 wide followed by an s in printf, so we
want to avoid this.)

Not we will also be dropping the '*' command. So this

printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, prec, sec);

becomes
  cout << ("%1$d:%2$d:%3$d") << hour
       << precision(prec) << min
       << precision(prec) << sec;
 
In other words if you need variable formating use manipulators.

> Another thought with regards to the transition between formats. How would it deal
> with having the result of a format in another format? Assuming we have centering,
> something like so:
> cout << format("%d %32cs") << 8 << format("Is %s's age") << name << endl;

> Is this going to be workable? Not that I necessarily think it should be but it is
> a usage scenario that should be addressed.

Here you would need to indicate the grouping of statements as what
you have written is grouped as...

 ((((cout << format("%d %32cs") ) << 8 ) << format("Is %s's age") ) << name )
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This means the second format is incorporated in the first before it
gets the argument "name". The result would be %32cs gets ignored
as the second format will need to eat arguments.
 
Since this isn't what we mean we would write it like...

 cout << format("%d %32cs") << 8 << (format("Is %s's age") << name) << endl;
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Which means put name into the string before incorporating it in the
the first format. Thus giving the expected output.

I worked on this last night and found I could get acceptable results.

--Karl


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