Boost logo

Boost Users :

Subject: Re: [Boost-users] [format] Reuse of the format object
From: Maxime van Noppen (maxime_at_[hidden])
Date: 2011-04-11 07:07:30


On 04/11/2011 12:47 PM, Krzysztof Czainski wrote:
> I believe a possible reason is that order of evaluation of the
> operations is unspecified.
>
> A simple example of this mechanism would be:
> int i = 0;
> cout << i++ << ++i; // what will this print? It's unspecified and
> compiler dependant
>
> To make your example work, try:
> cout << fmt % "a" % "b" % "c";
> cout << fmt % 1 % 2 % 3;

Oh well, I just rewrote the operator<< calls as function calls and it
becomes clear:

operator<<(
    operator<<(std::cout, fmt % "a" % "b" % "c"), // 1
    fmt % 1 % 2 % 3); // 2

The compiler may very well evaluate 2, which binds arguments into fmt,
then evaluate 1 to perform the nested operator<< call but then fmt is
already bound to arguments. But then, in the source code of the example
I quoted previously there is this statement:

> // once you call operator% again, arguments are cleared inside the object

Does there have to be at least one evaluation of the formatter before
this statement becomes true? It seems so given that this example throws
the same exception as above:

  fmt % 1 % 2 % 3;
  fmt % 4 % 5 % 6;
  std::cout << fmt;

So *after being evaluated*, the call to operator% clears the formatting
object, am I right?

-- 
Maxime

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net