# Type-safe 'printf-like' format class

## Choices made

"Le pourquoi du comment" ( - "the why of the how")

### The syntax of the format-string

Format is a new library. One of its goal is to provide a replacement for printf, that means format can parse a format-string designed for printf, apply it to the given arguments, and produce the same result as printf would have.
With this constraint, there were roughly 3 possible choices for the syntax of the format-string :

1. Use the exact same syntax of printf. It's well known by many experienced users, and fits almost all needs. But with C++ streams, the type-conversion character, crucial to determine the end of a directive, is only useful to set some associated formatting options, in a C++ streams context (%x for setting hexa, etc..) It would be better to make this obligatory type-conversion character, with modified meaning, optional.
2. extend printf syntax while maintaining compatibility, by using characters and constructs not yet valid as printf syntax. e.g. : "%1%", "%[1]", "%|1$d|", .. Using begin / end marks, all sort of extension can be considered.
3. Provide a non-legacy mode, in parallel of the printf-compatible one, that can be designed to fit other objectives without constraints of compatibilty with the existing printf syntax.
   But Designing a replacement to printf's syntax, that would be clearly better, and as much powerful, is yet another task than building a format class. When such a syntax is designed, we should consider splitting Boost.format into 2 separate libraries : one working hand in hand with this new syntax, and another supporting the legacy syntax (possibly a fast version, built with safety improvement above snprintf or the like).

In the absence of a full, clever, new syntax clearly better adapted to C++ streams than printf, the second approach was chosen. Boost.format uses printf's syntax, with extensions (tabulations, centered alignements) that can be expressed using extensions to this syntax.
And alternate compatible notations are provided to address the weaknesses of printf's :

- "%N%" as a simpler positional, typeless and optionless notation.
- %|spec| as a way to encapsulate printf directive in movre visually evident structures, at the same time making printf's 'type-conversion character' optional.

### Why are arguments passed through an operator rather than a function call ?

The inconvenience of the operator approach (for some people) is that it might be confusing. It's a usual warning that too much of overloading operators gets people real confused.
Since the use of format objects will be in specific contexts ( most often right after a "cout << ") and look like a formatting string followed by arguments indeed :

>     format(" %s at %s  with %s\n") % x % y % z;

we can hope it wont confuse people that much.

An other fear about operators, is precedence problems. What if I someday write format("%s") % x+y
instead of format("%s") % (x+y) ??
It will make a mistake at compile-time, so the error will be immediately detected.
indeed, this line calls tmp = operator%( format("%s"), x)
and then operator+(tmp, y)
tmp will be a format object, for which no implicit conversion is defined, and thus the call to operator+ will fail. (except if you define such an operator, of course). So you can safely assume precedence mistakes will be noticed at compilation.

On the other hand, the function approach has a true inconvenience. It needs to define lots of template function like :

>     template <class T1, class T2,  .., class TN>
>     string format(string s,  const T1& x1, .... , const T1& xN);

and even if we define those for N up to 500, that is still a limitation, that C's printf does not have.
Also, since format somehow emulates printf in some cases, but is far from being fully equivalent to printf, it's best to use a radically different appearance, and using operator calls succeeds very well in that !

Anyhow, if we actually chose the formal function call templates system, it would only be able to print Classes T for which there is an

>     operator<< ( stream,   const T&)

Because allowing both const and non const produces a combinatorics explosion - if we go up to 10 arguments, we need 2^10 functions.
(providing overloads on T& / const T& is at the frontier of defects of the C++ standard, and thus is far from guaranteed to be supported. But right now several compilers support those overloads)
There is a lot of chances that a class which only provides the non-const equivalent is badly designed, but yet it is another unjustified restriction to the user.
Also, some manipulators are functions, and can not be passed as const references. The function call approach thus does not support manipulators well.

In conclusion, using a dedicated binary operator is the simplest, most robust, and least restrictive mechanism to pass arguments when you can't know the number of arguments at compile-time.

### Why operator% rather than a member function 'with(..)' ??

technically,

>     format(fstr) % x1 % x2 % x3;

has the same structure as

>     format(fstr).with( x1 ).with( x2 ).with( x3 );

which does not have any precedence problem. The only drawback, is it's harder for the eye to catch what is done in this line, than when we are using operators. calling .with(..), it looks just like any other line of code. So it may be a better solution, depending on tastes. The extra characters, and overall cluttered aspect of the line of code using 'with(..)' were enough for me to opt for a true operator.

### Why operator% rather than usual formatting operator<< ??

- because passing arguments to a format object is *not* the same as sending variables, sequentially, into a stream, and because a format object is not a stream, nor a manipulator.
  We use an operator to pass arguments. format will use them as a function would, it simply takes arguments one by one.
  format objects can not provide stream-like behaviour. When you try to implement a format object that acts like a manipulator, returning a stream, you make the user beleive it is completely like a stream-manipulator. And sooner or later, the user is deceived by this point of view.
  The most obvious example of that difference in behaviour is

  >     cout << format("%s %s ") << x;
  >     cout << y ;  // uh-oh, format is not really a stream manipulator

- precedence of % is higher than that of <<. It can be viewd as a problem, because + and - thus needs to be grouped inside parentheses, while it is not necessary with '<<'. But if the user forgets, the mistake is catched at compilation, and hopefully he won't forget again.
  On the other hand, the higher precedence makes format's behaviour very straight-forward.

  >     cout << format("%s %s ") % x % y << endl;

  is treated exaclt like :

  >     cout << ( format("%s %s ") % x % y ) << endl;

  So using %, the life of a format object does not interfere with the surrounding stream context. This is the simplest possible behaviour, and thus the user is able to continue using the stream after the format object.
  With operator<<, things are much more problematic in this situation. This line :

  >     cout << format("%s %s ") <<  x  <<  y << endl;

  is understood as :

  >     ( ( ( cout << format("%s %s ") ) << x ) <<  y ) << endl;

  Several alternative implementations chose operator<<, and there is only one way to make it work :
  the first call to

  >     operator<<( ostream&, format const&)

  returns a proxy, encapsulating both the final destination (cout) and the format-string information
  Passing arguments to format, or to the final destination after completion of the format are indistinguishable. This is a problem.

  I examined several possible implementations, and none is completely satsifying.
  E.g. : In order to catch users mistake, it makes sense to raise exceptions when the user passes too many arguments. But in this context, supplementary arguments are most certainly aimed at the final destination. There are several choices here :

  - You can give-up detection of arity excess, and have the proxy's template member operator<<( const T&) simply forward all supplementary arguments to cout.
  - Require the user to close the format arguments with a special manipulator, 'endf', in this way :

    >     cout << format("%s %s ") <<  x  <<  y << endf << endl;

    You can define endf to be a function that returns the final destination stored inside the proxy. Then it's okay, after endf the user is calling << on cout again.
  - An intermediate solution, is to adress the most frequent use, where the user simply wants to output one more manipulator item to cout (a std::flush, or endl, ..)

    >     cout << format("%s %s \n") <<  x  <<  y << flush ;

    Then, the solution is to overload the operator<< for manipulators. This way You don't need endf, but outputting a non-manipulator item right after the format arguments is a mistake.
  The most complete solution is the one with the endf manipualtor. With operator%, there is no need for this end-format function, plus you instantly see which arguments are going into the format object, and which are going to the stream.
- Esthetically : '%' is the same letter as used inside the format-string. That is quite nice to have the same letter used for passing each argument. '<<' is 2 letters, '%' is one. '%' is also smaller in size. It overall improves visualisation (we see what goes with what) :

  >     cout << format("%s %s %s") %x %y %z << "And  avg is" << format("%s\n") %avg;

  compared to :

  >     cout << format("%s %s %s") << x << y << z << endf <<"And avg is" << format("%s\n") << avg;

  "<<" misleadingly puts the arguments at the same level as any object passed to the stream.
- python also uses % for formatting, so you see it's not so "unheard of" ;-)

### Why operator% rather than operator(), or operator[] ??

operator() has the merit of being the natural way to send an argument into a function. And some think that operator[] 's meaning apply well to the usage in format.
They're as good as operator% technically, but quite ugly. (that's a matter of taste)
And deepd down, using operator% for passing arguments that were referred to by "%" in the format string seems much more natural to me than using those operators.

July 07, 2001

© Copyright Samuel Krempp 2001. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.