Boost logo

Boost Users :

Subject: Re: [Boost-users] [format] Reuse of the format object
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2011-04-11 06:47:16


2011/4/11 Maxime van Noppen <maxime_at_[hidden]>

> Hi,
>
> I've got a question about the reuse of the boost::format object. Given
> this minimal testcase:
>
> -----------------------------------------------------------
> #include <iostream>
> #include <boost/format.hpp>
>
> int main()
> {
> boost::format fmt("%|=12| %|=12| %|=12|\n");
>
> // 1
> std::cout
> << boost::str(fmt % "a" % "b" % "c")
> << boost::str(fmt % 1 % 2 % 3);
>
> // 2
> std::cout
> << fmt % "a" % "b" % "c"
> << fmt % 1 % 2 % 3;
> }
> -----------------------------------------------------------
>
>
> Why is 1 working while 2 produces the runtime exception:
>
> > terminate called after throwing an instance of
> 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::io::too_many_args>
> >'
> > what(): boost::too_many_args: format-string referred to less arguments
> than were passed
>
> ?
>

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;

Regards
Kris



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