1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
5 <meta http-equiv=
"Content-Language" content=
"en-us">
6 <meta http-equiv=
"Content-Type" content=
"text/html; charset=us-ascii">
8 <title>The Boost Format library
</title>
11 <body bgcolor=
"white" text=
"black">
12 <h1><img align=
"middle" alt=
"boost.png (6897 bytes)" height=
"86" src=
13 "../../../boost.png" width=
"277">The Boost Format library
</h1>
16 "../../../boost/format.hpp"><boost/format.hpp
></a></code> format
17 class provides printf-like formatting, in a type-safe manner which allows
18 output of user-defined types.
<br></p>
21 <li><a href=
"#synopsis">Synopsis
</a></li>
23 <li><a href=
"#how_it_works">How it works
</a></li>
25 <li><a href=
"#examples">Examples
</a></li>
28 <a href=
"#syntax">Syntax
</a>
31 <li><a href=
"#printf_directives">printf format-specification
34 <li><a href=
"#printf_differences">Incompatibilities with
39 <li><a href=
"#manipulators">Manipulators and the internal stream
42 <li><a href=
"#user-defined">User-defined types
</a></li>
44 <li><a href=
"#alternatives">Alternatives
</a></li>
46 <li><a href=
"#exceptions">Exceptions
</a></li>
48 <li><a href=
"#performance">Performance
</a></li>
50 <li><a href=
"#extract">Class Interface Extract
</a></li>
52 <li><a href=
"#rationale">Rationale
</a></li>
53 </ul><a name=
"synopsis" id=
"synopsis"></a>
58 <p>A format object is constructed from a format-string, and is then given
59 arguments through repeated calls to
<i>operator%
</i>.
<br>
60 Each of those arguments are then converted to strings, who are in turn
61 combined into one string, according to the format-string.
</p>
65 cout
<< boost::format(
"writing %1%, x=%2% : %3%-th try") %
"toto" %
40.23 %
50;
66 // prints
"writing toto, x=40.230 : 50-th try"
68 </blockquote><a name=
"how_it_works" id=
"how_it_works"></a>
74 <li>When you call
<i>format(s)
</i>, where s is the format-string, it
75 constructs an object, which parses the format string and look for all
76 directives in it and prepares internal structures for the next step.
</li>
78 <li>Then, either immediately, as in
82 cout
<< format(
"%2% %1%") %
36 %
77;
84 </blockquote>or later on, as in
88 format fmter(
"%2% %1%");
89 fmter %
36; fmter %
77;
91 </blockquote>you
<i>feed
</i> variables into the formatter.
<br>
92 those variables are dumped into an internal stream, which state is set
93 according to the given formatting options in the format-string -if
94 there are any-, and the format object stores the string results for the
98 <li>Once all arguments have been fed you can dump the format object to a
99 stream, or get its string value by using the
<i>str()
</i> member
100 function, or the free function
<i>str(const format
& )
</i> in
101 namespace
<i>boost
</i>. The result string stays accessible in the format
102 object until another argument is passed, at which time it is
108 // fmter was previously created and fed arguments, it can print the result :
109 cout
<< fmter ;
111 // You can take the string result :
112 string s = fmter.str();
114 // possibly several times :
117 // You can also do all steps at once :
118 cout
<< boost::format(
"%2% %1%") %
36 %
77;
120 // using the str free function :
121 string s2 = str( format(
"%2% %1%") %
36 %
77 );
127 <li>Optionnally, after step
3, you can re-use a format object and restart
128 at step2 :
<i>fmter %
18 %
39;
</i><br>
129 to format new variables with the same format-string, saving the expensive
130 processing involved at step
1.
</li>
131 </ol>All in all, the format class translates a format-string (with
132 eventually printf-like directives) into operations on an internal stream,
133 and finally returns the result of the formatting, as a string, or directly
134 into an output stream.
<a name=
"examples" id=
"examples"></a>
143 using boost::io::group;
148 <li>Simple output, with reordering :
153 cout
<< format(
"%1% %2% %3% %2% %1% \n") %
"11" %
"22" %
"333"; // 'simple' style.
156 </blockquote>It prints :
"11 22 333 22 11 \n"
159 <li>More precise formatting, with Posix-printf positional directives :
164 cout
<< format(
"(x,y) = (%1$+5d,%2$+5d) \n") % -
23 %
35; // Posix-Printf style
167 </blockquote>It prints :
"(x,y) = ( -23, +35) \n"
170 <li>classical printf directive, no reordering :
175 cout
<< format(
"writing %s, x=%s : %d-th step \n") %
"toto" %
40.23 %
50;
178 </blockquote>It prints :
"writing toto, x=40.23 : 50-th step \n"
181 <li>Several ways to express the same thing :
186 cout
<< format(
"(x,y) = (%+5d,%+5d) \n") % -
23 %
35;
187 cout
<< format(
"(x,y) = (%|+5|,%|+5|) \n") % -
23 %
35;
189 cout
<< format(
"(x,y) = (%1$+5d,%2$+5d) \n") % -
23 %
35;
190 cout
<< format(
"(x,y) = (%|1$+5|,%|2$+5|) \n") % -
23 %
35;
193 </blockquote>all those print :
"(x,y) = ( -23, +35) \n"
196 <li>Using manipulators to modify the format-string :
201 format fmter(
"_%1$+5d_ %1$d \n");
203 format fmter2(
"_%1%_ %1% \n");
204 fmter2.modify_item(
1, group(showpos, setw(
5)) );
206 cout
<< fmter %
101 ;
207 cout
<< fmter2 %
101 ;
210 </blockquote>Both print the same :
"_ +101_ 101 \n"
213 <li>Using manipulators with arguments :
218 cout
<< format(
"_%1%_ %1% \n") % group(showpos, setw(
5),
101);
221 </blockquote>The manipulators are applied at each occurrence of %
1%, and
222 thus it prints :
"_ +101_ +101 \n"
225 <li>New formatting feature : 'absolute tabulations', useful inside loops,
226 to insure a field is printed at the same position from one line to the
227 next, even if the widthes of the previous arguments can vary a lot.
232 for(unsigned int i=
0; i
< names.size(); ++i)
233 cout
<< format(
"%1%, %2%, %|40t|%3%\n") % names[i] % surname[i] % tel[i];
236 </blockquote>For some std::vector
<i>names
</i>,
<i>surnames
</i>, and
237 <i>tel
</i> (see sample_new_features.cpp) it prints :
241 Marc-Fran
çois Michel, Durand, +
33 (
0)
123 456 789
242 Jean, de Lattre de Tassigny, +
33 (
0)
987 654 321
249 <h2>Sample Files
</h2>
251 <p>The program
<a href=
252 "../example/sample_formats.cpp">sample_formats.cpp
</a> demonstrates simple
253 uses of
<b>format
</b>.
<br></p>
255 <p><a href=
"../example/sample_new_features.cpp">sample_new_features.cpp
</a>
256 illustrates the few formatting features that were added to printf's syntax
257 such as simple positional directives, centered alignment, and
258 'tabulations'.
<br></p>
260 <p><a href=
"../example/sample_advanced.cpp">sample_advanced.cpp
</a>
261 demonstrates uses of advanced features, like reusing, and modifying, format
262 objects, etc..
<br></p>
264 <p>And
<a href=
"../example/sample_userType.cpp">sample_userType.cpp
</a>
265 shows the behaviour of the
<b>format
</b> library on user-defined
266 types.
</p><a name=
"syntax" id=
"syntax"></a>
271 <p><b>boost::format(
</b> format-string
<b>) %
</b> arg1
<b>%
</b> arg2
272 <b>%
</b> ...
<b>%
</b> argN
</p>
274 <p>The
<i>format-string
</i> contains text in which special directives will
275 be replaced by strings resulting from the formatting of the given
277 The legacy syntax in the C and C++ worlds is the one used by printf, and
278 thus format can use directly printf format-strings, and produce the same
279 result (in almost all cases. see
<a href=
280 "#printf_differences">Incompatibilities with printf
</a> for details)
<br>
281 This core syntax was extended, to allow new features, but also to adapt to
282 the C++ streams context. Thus, format accepts several forms of directives
283 in format-strings :
</p>
286 <li>Legacy printf format strings :
<b>%
</b><i>spec
</i> where
<i>spec
</i>
287 is a
<a href=
"#printf_directives">printf format specification
</a><br>
288 <i>spec
</i> passes formatting options, like width, alignment, numerical
289 base used for formatting numbers, as well as other specific flags. But
290 the classical
<i>type-specification
</i> flag of printf has a weaker
291 meaning in format. It merely sets the appropriate flags on the internal
292 stream, and/or formatting parameters, but does not require the
293 corresponding argument to be of a specific type.
<br>
294 e.g. : the specification
<i>2$x
</i>, meaning
"print argument number 2,
295 which is an integral number, in hexa" for printf, merely means
"print
296 argument 2 with stream basefield flags set to <i>hex</i>" for
299 <li><b>%|
</b><i>spec
</i><b>|
</b> where
<i>spec
</i> is a printf format
301 This pipe-delimited syntax is introduced, to improve the readability of the
302 format-string, but primarily, to make the
<i>type-conversion
303 character
</i> optional in
<i>spec
</i>. This information is not necessary
304 with C++ variables, but with direct printf syntax, it is necessary to
305 always give a type-conversion character, merely because this character is
306 crucial to determine the end of a format-specification.
<br>
307 e.g. :
"%|-5|" will format the next variable with width set to
5, and
308 left-alignment just like the following printf directives :
"%-5g",
309 "%-5f",
"%-5s" ..
</li>
311 <li><b>%
</b><i>N
</i><b>%
</b><br>
312 This simple positional notation requests the formatting of the
313 <i>N
</i>-th argument - wihout any formatting option.
<br>
314 (It's merely a shortcut to Printf's positional directives (like
315 "%<i>N</i>$s"), but a major benefit is that it's much more readable, and
316 does not use a
"type-conversion" character)
</li>
317 </ul>On top of the standard printf format specifications, new features were
318 implemented, like centered alignment. See
<a href=
"#new_directives">new
319 format specification
</a> for details.
<a name=
"printf_directives" id=
320 "printf_directives"></a>
322 <h3>printf format specifications
</h3>
324 <p>The printf format specifications supported by Boost.format follows the
326 "http://www.opengroup.org/onlinepubs/7908799/xsh/fprintf.html">Open-group
327 printf
</a> precise syntax, rather than the standard C printf, which does
328 not support positional arguments. (Common flags have the same meaning in
329 both, so it should not be a headache for anybody)
<br>
330 <i>Note that it is an error to use positional format specifications
</i>
331 (e.g.
<i>%
3$+d
</i>)
<i>mixed with non-positional ones
</i> (e.g.
<i>%+d
</i>)
332 <i>in the same format string.
</i><br>
333 In the Open-group specification, referring to the same argument several
334 times (e.g.
<i>"%1$d %1$d"</i>) has undefined behaviour. Boost.format's
335 behaviour in such cases is to allow each argument to be reffered to any
336 number of times. The only constraint is that it expects exactly
<i>P
</i>
337 arguments,
<i>P
</i> being the maximum argument number used in the format
338 string. (e.g., for
"%1$d %10$d",
<i>P
</i> ==
10 ).
<br>
339 Supplying more, or less, than
<i>P
</i> arguments raises an exception.
340 (unless it was set otherwise, see
<a href=
"#exceptions">exceptions
</a>)
</p>
344 A specification
<i>spec
</i> has the form : [
<i>N
</i><b>$
</b> ] [
345 <i>flags
</i> ] [
<i>width
</i> ] [
<b>.
</b> <i>precision
</i> ]
348 Fields insided square brackets are optional. Each of those fields are
349 explained one by one in the following list :
</p>
352 <li><i>N
</i> <b>$
</b> (optional field) specifies that the format
353 specification applies to the
<i>N
</i>-th argument. (it is called a
354 <i>positional format specification
</i>)
<br>
355 If this is not present, arguments are taken one by one. (and it is then
356 an error to later supply an argument number)
</li>
359 <i>flags
</i> is a sequences of any of those :
362 <table border=
"1" cellpadding=
"5" summary=
"">
366 <td><b>Meaning
</b></td>
368 <td><b>effect on internal stream
</b></td>
374 <td>left alignment
</td>
376 <td>N/A (applied later on the string)
</td>
382 <td>centered alignment
</td>
384 <td>N/A (applied later on the string)
<br>
385 <i>- note : added feature, not in printf -
</i></td>
391 <td>internal alignment
</td>
393 <td>sets internal alignment
<br>
394 <i>- note : added feature, not in printf -
</i></td>
400 <td>show sign even for positive numbers
</td>
402 <td>sets
<i>showpos
</i></td>
408 <td>show numerical base, and decimal point
</td>
410 <td>sets
<i>showbase
</i> and
<i>showpoint
</i></td>
416 <td>pad with
0's (inserted after sign or base indicator)
</td>
418 <td>if not left-aligned, calls
<i>setfill('
0')
</i> and sets
420 Extra actions are taken after stream conversion to handle
421 <a href=
"#user-defined">user-defined output
</a>.
</td>
427 <td>if the string does not begin with
<i>+
</i> or
<i>-
</i>,
428 insert a
<i>space
</i> before the converted string
</td>
430 <td>N/A (applied later on the string)
<br>
431 Different to printf's behaviour : it is not affected by internal
438 <li><i>width
</i> specifies a minimal width for the string resulting form
439 the conversion. If necessary, the string will be padded with alignment
440 and fill characters either set on the stream via manipulators, or
441 specified by the format-string (e.g. flags '
0', '-', ..)
<br>
442 Note that width is not just set on the conversion stream. To support
443 output of
<a href=
"#user-defined">user-defined types
</a> (that might call
444 <i>operator
<<</i> many times on several members), the width is
445 handled after stream conversion of the whole argument object, in the
446 format class code.
</li>
449 <i>precision
</i> (preceded by a point), sets the stream's
453 <li>When outputting a floatting type number, it sets the maximum
457 <li>after decimal point when in fixed or scientific mode
</li>
459 <li>in total when in default mode ('
<i>general mode
</i>', like
464 <li>When used with type-char
<b>s
</b> or
<b>S
</b> it takes another
465 meaning : the conversion string is truncated to the
<i>precision
</i>
466 first chars. (Note that the eventual padding to
<i>width
</i> is done
467 after truncation.)
</li>
472 <i>type-char
</i>. it does
<b>not
</b> impose the concerned argument to
473 be of a restricted set of types, but merely sets the flags that are
474 associated with this type specification.
477 <table border=
"1" cellpadding=
"5" summary=
"">
479 <td><b>Type-Char
</b></td>
481 <td><b>Meaning
</b></td>
483 <td><b>effect on stream
</b></td>
487 <td><b>p or x
</b></td>
489 <td>hexadecimal output
</td>
491 <td>sets
<i>hex
</i></td>
497 <td>octal output
</td>
499 <td>sets
<i>oct
</i></td>
505 <td>scientific float format
</td>
507 <td>sets floatfield bits to
<i>scientific
</i></td>
513 <td>fixed float format
</td>
515 <td>sets floatfield bits to
<i>fixed
</i></td>
521 <td>general -default- float format
</td>
523 <td><b>unset
</b> all floatfield bits
</td>
527 <td><b>X, E
</b> or
<b>G
</b></td>
529 <td>same effect as their lowercase counterparts, but using
530 uppercase letters for number outputs. (exponents, hex digits,
533 <td>same effects as
<i>'x'
</i>,
<i>'e'
</i>, or
<i>'g'
</i>,
534 <b>plus
</b> <i>uppercase
</i></td>
538 <td><b>d, i
</b> or
<b>u
</b></td>
540 <td><b>decimal
</b> type output
</td>
542 <td>sets basefield bits to
<i>dec
</i></td>
546 <td><b>s
</b> or
<b>S
</b></td>
548 <td>string output
</td>
550 <td><i>precision
</i> specification is unset, and its value goes
551 to an internal field for later 'truncation'. (see
552 <i>precision
</i> explanation above)
</td>
556 <td><b>c
</b> or
<b>C
</b></td>
558 <td>1-character output
</td>
560 <td>only the first character of the conversion string is
567 <td>print the character
<i>%
</i></td>
574 <p>Note that the 'n' type specification is ignored (and so is the
575 corresponding argument), because it does not fit in this context.
<br>
576 Also, printf 'l', 'L', or 'h' modifiers (to indicate wide, long or
577 short types) are supported (and simply have no effect on the internal
580 </ul><a name=
"new_directives" id=
"new_directives"></a>
582 <h3>new format-specifications
</h3>
585 <li>as stated in the flags table, centered and internal alignment flags
586 ('
<i>=
</i> ', and '
<i>_
</i> ') were added.
</li>
588 <li><i><b>%{
</b>n
</i><b>t}
</b> , where
<i>n
</i> is a positive number,
589 inserts an
<i>absolute tabulation
</i>. It means that format will, if
590 needed, fill the string with characters, until the length of the string
591 created so far reaches
<i>n
</i> characters. (see
<a href=
592 "#examples">examples
</a> )
</li>
594 <li><b>%|
</b><i>n
</i><b>T
</b><i>X
</i><b>|
</b> inserts a tabulation in the
595 same way, but using
<i>X
</i> as fill character instead of the current
596 'fill' char of the stream (which is
<i>space
</i> for a stream in default
598 </ul><a name=
"printf_differences" id=
"printf_differences"></a>
600 <h2>Differences of behaviour vs printf
</h2>Suppose you have variables
601 <i>x1, x2
</i> (built_in types, supported by C's printf),
<br>
602 and a format string
<i>s
</i> intended for use with a printf function this
610 In almost all cases, the result will be the same as with this command :
614 cout
<< format(s) % x1 % x2;
618 <p>But because some printf format specifications don't translate well into
619 stream formatting options, there are a few notable imperfections in the way
620 Boost.format emulates printf.
<br>
621 In any case, the
<i>format
</i> class should quietly ignore the unsupported
622 options, so that printf format-strings are always accepted by format and
623 produce almost the same output as printf.
</p><br>
624 Here is the full list of such differences :
627 <li><b>'
0'
</b> and
<b>' '
</b> options : printf ignores these options for
628 non numeric conversions, but format applies them to all types of
629 variables. (so it is possible to use those options on user-defined types,
630 e.g. a Rational class, etc..)
</li>
632 <li><b>precision
</b> for integral types arguments has a special meaning
634 <i>printf(
"(%5.3d)" ,
7 ) ;
</i> prints
« (
007)
»<br>
635 While format, like streams, ignores the precision parameter for integral
636 types conversions.
</li>
638 <li>the
<b>'
</b> printf option (
<i>format with thousands grouping
639 characters)
</i>) has no effect in format.
</li>
641 <li>Width or precision set to asterisk (
<i>*
</i>) are used by printf to
642 read this field from an argument. e.g.
643 <i>printf(
"%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);
</i><br>
644 This class does not support this mechanism for now. so such precision or
645 width fields are quietly ignored by the parsing.
</li>
646 </ul>Also, note that the special
<b>'n'
</b> type-specification (used to
647 tell printf to save in a variable the number of characters output by the
648 formatting) has no effect in format.
<br>
649 Thus format strings containing this type-specification should produce the
650 same converted string by printf or format. It will not cause differences in
651 the formatted strings between printf and format.
<br>
652 To get the number of characters in the formatted string using Boost.Format,
653 you can use the
<i>size()
</i> member function :
657 format formatter(
"%+5d");
658 cout
<< formatter % x;
659 unsigned int n = formatter.size();
661 </blockquote><a name=
"user-defined" id=
"user-defined"></a>
664 <h2>User-defined types output
</h2>
666 <p>All flags which are translated into modification to the stream state act
667 recursively within user-defined types. ( the flags remain active, and so
668 does the desired format option, for each of the '
<<' operations that
669 might be called by the user-defined class)
</p>e.g., with a Rational class,
670 we would have something like :
674 Rational ratio(
16,
9);
675 cerr
<< format(
"%#x \n") % ratio; // -
> "0x10/0x9 \n"
679 <p>It's a different story for other formatting options. For example,
680 setting width applies to the final output produced by the object, not to
681 each of its internal outputs, and that's fortunate :
</p>
685 cerr
<< format(
"%-8d") % ratio; // -
> "16/9 " and not
"16 /9 "
686 cerr
<< format(
"%=8d") % ratio; // -
> " 16/9 " and not
" 16 / 9 "
691 But so does the
0 and ' ' options (contrarily to '+' which is directly
692 translated to the stream state by
<i>showpos
</i>. But no such flags exist
693 for the zero and space printf options)
<br>
694 and that is less natural :
</p>
698 cerr
<< format(
"%+08d \n") % ratio; // -
> "+00016/9"
699 cerr
<< format(
"% 08d \n") % ratio; // -
> "000 16/9"
701 </blockquote>It is possible to obtain a better behaviour by carefully
702 designing the Rational's
<i>operator
<<</i> to handle the stream's
703 width, alignment and
<i>showpos
</i> paramaters by itself. This is
704 demonstrated in
<a href=
705 "../example/sample_userType.cpp">sample_userType.cpp
</a>.
<a name=
706 "manipulators" id=
"manipulators"></a>
709 <h3>Manipulators, and internal stream state
</h3>
711 <p>The internal stream state of
<b>format
</b> is saved before and restored
712 after output of an argument; therefore, the modifiers are not sticky and
713 affect only the argument they are applied to.
<br>
714 The default state for streams, as stated by the standard, is : precision
6,
715 width
0, right alignment, and decimal flag set.
</p>
717 <p>The state of the internal
<b>format
</b> stream can be changed by
718 manipulators passed along with the argument; via the
<i>group
</i> function,
723 cout
<< format(
"%1% %2% %1%\n") % group(hex, showbase,
40) %
50; // prints
"0x28 50 0x28\n"
728 When passing N items inside a 'group' Boost.format needs to process
729 manipulators diferently from regular argument, and thus using group is
730 subject to the following constraints :
</p>
733 <li>the object to be printed must be passed as the last item in the
736 <li>the first N-
1 items are treated as manipulators, and if they do
737 produce output, it is discarded
</li>
740 <p>Such manipulators are passed to the streams right before the following
741 argument, at every occurrence. Note that formatting options specified within
742 the format string are overridden by stream state modifiers passed this way.
743 For instance in the following code, the
<i>hex
</i> manipulator has priority
744 over the
<i>d
</i> type-specification in the format-string which would set
749 cout
<< format(
"%1$d %2% %1%\n") % group(hex, showbase,
40) %
50;
750 // prints
"0x28 50 0x28\n"
752 </blockquote><a name=
"alternatives" id=
"alternatives"></a>
754 <h2>Alternatives
</h2>
757 <li><b>printf
</b> is the classical alternative, that is not type safe and
758 not extendable to user-defined types.
</li>
760 <li>ofrstream.cc by Karl Nelson's design was a big source of inspiration
761 to this format class.
</li>
763 <li>James Kanze's library has a format class (in
764 <i>srcode/Extended/format
</i> ) which looks very well polished. Its
765 design has in common with this class the use of internal stream for the
766 actual conversions, as well as using operators to pass arguments. (but
767 his class, as ofrstream, uses
<i>operator
<<</i> rather
<i>than
770 <li><a href=
"http://groups.yahoo.com/group/boost/files/format3/">Karl
771 Nelson's library
</a> was intented as demonstration of alternative
772 solutions in discussions on Boost's list for the design of
774 </ul><a name=
"exceptions" id=
"exceptions"></a>
779 <p>Boost.format enforces a number of rules on the usage of format objects.
780 The format-string must obeys the syntax described above, the user must
781 supply exactly the right number of arguments before outputting to the final
782 destination, and if using modify_item or bind_arg, items and arguments
783 index must not be out of range.
<br>
784 When format detects that one of these rules is not satisfied, it raises a
785 corresponding exception, so that the mistakes don't go unnoticed and
787 But the user can change this behaviour to fit his needs, and select which
788 types of errors may raise exceptions using the following functions :
</p>
793 unsigned char exceptions(unsigned char newexcept); // query and set
794 unsigned char exceptions() const; // just query
799 <p>The user can compute the argument
<i>newexcept
</i> by combining the
800 following atoms using binary arithmetic :
</p>
803 <li><b>boost::io::bad_format_string_bit
</b> selects errors due to
804 ill-formed format-strings.
</li>
806 <li><b>boost::io::too_few_args_bit
</b> selects errors due to asking for
807 the srting result before all arguments are passed.
</li>
809 <li><b>boost::io::too_many_args_bit
</b> selects errors due to passing too
812 <li><b>boost::io::out_of_range_bit
</b> select errors due to out of range
813 index supplied by the user when calling
<i>modify_item
</i> or other
814 functions taking an item index (or an argument index)
</li>
816 <li><b>boost::io::all_error_bits
</b> selects all errors
</li>
818 <li><b>boost::io::no_error_bits
</b> selects no error.
</li>
821 <p>For instance, if you don't want Boost.format to detect bad number of
822 arguments, you can define a specific wrapper function for building format
823 objects with the right exceptions settings :
</p>
828 boost::format my_fmt(const std::string
& f_string) {
829 using namespace boost::io;
830 format fmter(f_string);
831 fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit ) );
836 </blockquote>It is then allowed to give more arguments than needed (they
837 are simply ignored) :
842 cout
<< my_fmt(
" %1% %2% \n") %
1 %
2 %
3 %
4 %
5;
845 </blockquote>And if we ask for the result before all arguments are
846 supplied, the corresponding part of the result is simply empty
851 cout
<< my_fmt(
" _%2%_ _%1%_ \n") %
1 ;
852 // prints
" __ _1_ \n"
855 </blockquote><a name=
"performance" id=
"performance"></a>
858 <h2>A Note about performance
</h2>
860 <p>The performance of boost::format for formatting a few builtin type
861 arguments with reordering can be compared to that of Posix-printf, and of
862 the equivalent stream manual operations to give a measure of the overhead
863 incurred. The result may greatly depend on the compiler, standard library
864 implementation, and the precise choice of format-string and arguments.
</p>
866 <p>Since common stream implementations eventually call functions of the
867 printf family for the actual formatting of numbers, in general printf will
868 be noticeably faster than the direct stream operations And due to to the
869 reordering overhead (allocations to store the pieces of string, stream
870 initialisation at each item formatting, ..) the direct stream operations
871 would be faster than boost::format, (one cas expect a ratio ranging from
2
874 <p>When iterated formattings are a performance bottleneck, performance can
875 be slightly increased by parsing the format string into a format object,
876 and copying it at each formatting, in the following way.
</p>
880 const boost::format fmter(fstring);
881 dest
<< boost::format(fmter) % arg1 % arg2 % arg3 ;
885 <p>As an example of performance results, the author measured the time of
886 execution of iterated formattings with
4 different methods
</p>
889 <li>posix printf
</li>
891 <li>manual stream output (to a dummy
<i>nullStream
</i> stream sending the
892 bytes into oblivion)
</li>
894 <li>boost::format copied from a const object as shown above
</li>
896 <li>the straight boost::format usage
</li>
899 <p>the test was compiled with g++-
3.3.3 and the following timings were
900 measured (in seconds, and ratios) :
</p>
904 string
fstring=
"%3$0#6x %1$20.10E %2$g %3$0+5d \n";
911 nullStream :
3.43, =
1.61033 * printf
912 boost::format copied :
6.77, =
3.1784 * printf , =
1.97376 * nullStream
913 boost::format straight :
10.67, =
5.00939 * printf , =
3.11079 * nullStream
917 nullStream :
3.69, =
1.74057 * printf
918 boost::format copied :
10.02, =
4.72642 * printf , =
2.71545 * nullStream
919 boost::format straight :
17.03, =
8.03302 * printf , =
4.61518 * nullStream
921 </blockquote><a name=
"extract" id=
"extract"></a>
924 <h2>Class Interface Extract
</h2>
930 template
<class charT, class Traits=std::char_traits
<charT
> >
934 typedef std::basic_string
<charT, Traits
> string_t;
935 typedef typename string_t::size_type size_type;
936 basic_format(const charT* str);
937 basic_format(const charT* str, const std::locale
& loc);
938 basic_format(const string_t
& s);
939 basic_format(const string_t
& s, const std::locale
& loc);
940 basic_format
& operator= (const basic_format
& x);
942 void clear(); // reset buffers
943 basic_format
& parse(const string_t
&); // clears and parse a new format string
945 string_t str() const;
946 size_type size() const;
948 // pass arguments through those operators :
949 template
<class T
> basic_format
& operator%(T
& x);
950 template
<class T
> basic_format
& operator%(const T
& x);
952 // dump buffers to ostream :
953 friend std::basic_ostream
<charT, Traits
>&
954 operator
<< <> ( std::basic_ostream
<charT, Traits
>& , basic_format
& );
956 // Choosing which errors will throw exceptions :
957 unsigned char exceptions() const;
958 unsigned char exceptions(unsigned char newexcept);
960 // ............ this is just an extract .......
963 typedef basic_format
<char
> format;
964 typedef basic_format
<wchar_t
> wformat;
967 // free function for ease of use :
968 template
<class charT, class Traits
>
969 std::basic_string
<charT,Traits
> str(const basic_format
<charT,Traits
>& f) {
978 <a name=
"rationale" id=
"rationale"></a>
982 <p>This class's goal is to bring a better, C++, type-safe and
983 type-extendable
<i>printf
</i> equivalent to be used with
984 streams.
</p>Precisely,
<b>format
</b> was designed to provide the following
988 <li>support positional arguments (required for internationalisation)
</li>
990 <li>accept an unlimited number of arguments.
</li>
992 <li>make formatting commands visually natural.
</li>
994 <li>support the use of manipulators to modify the display of an argument.
995 in addition to the format-string syntax.
</li>
997 <li>accept any types of variables, by relying on streams for the actual
998 conversion to string. This specifically concerns user-defined types, for
999 which the formatting options effects should be intuitively natural.
</li>
1001 <li>provide printf-compatibility, as much as it makes sense in a
1002 type-safe and type-extendable context.
</li>
1005 <p>In the process of the design, many issues were faced, and some choices
1006 were made, that might not be intuitively right. But in each case they were
1007 taken for
<a href=
"choices.html">some reasons
</a>.
</p>
1012 <p>The author of Boost format is Samuel Krempp.
He used ideas from
1013 R
üdiger Loos' format.hpp and Karl Nelson's formatting classes.
</p>
1016 <p><a href=
"http://validator.w3.org/check?uri=referer"><img border=
"0" src=
1017 "../../../doc/images/valid-html401.png" alt=
"Valid HTML 4.01 Transitional"
1018 height=
"31" width=
"88"></a></p>
1021 <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->02 December,
2006<!--webbot bot="Timestamp" endspan i-checksum="38510" --></p>
1023 <p><i>Copyright
© 2002 Samuel Krempp
</i></p>
1025 <p><i>Distributed under the Boost Software License, Version
1.0. (See
1026 accompanying file
<a href=
"../../../LICENSE_1_0.txt">LICENSE_1_0.txt
</a> or
1028 "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt
</a>)
</i></p>