### Basic Boost.Format Usage in C++ Source: https://www.boost.org/doc/libs/latest/libs/format/example/sample_formats Demonstrates fundamental string formatting capabilities of Boost.Format, including simple substitution, reordering of arguments, and the use of positional specifiers. ```C++ #include #include #include #include "boost/format.hpp" namespace MyNS_ForOutput { using std::cout; using std::cerr; using std::string; using std::endl; using std::flush; using boost::format; using boost::io::group; } namespace MyNS_Manips { using std::setfill; using std::setw; using std::hex ; using std::dec ; } int main(){ using namespace MyNS_ForOutput; using namespace MyNS_Manips; std::cout << format("%|1$1| %|2$3|") % "Hello" % 3 << std::endl; // Reordering : cout << format("%1% %2% %3% %2% %1% \n") % "o" % "oo" % "O"; // 'simple' style. // prints "o oo O oo o \n" cout << format("(x,y) = (%1$+5d,%2$+5d) \n") % -23 % 35; // Posix-Printf style // No reordering : cout << format("writing %s, x=%s : %d-th step \n") % "toto" % 40.23 % 50; // prints "writing toto, x=40.23 : 50-th step \n" cout << format("(x,y) = (%+5d,%+5d) \n") % -23 % 35; cout << format("(x,y) = (%|+5|,%|+5|) \n") % -23 % 35; cout << format("(x,y) = (%|1$+5|,%|2$+5|) \n") % -23 % 35; // all those are the same, it prints "(x,y) = ( -23, +35) \n" // Using manipulators, via 'group' : cout << format("%2% %1% %2%\n") % 1 % group(setfill('X'), hex, setw(4), 16+3) ; // prints "XX13 1 XX13\n" // printf directives's type-flag can be used to pass formatting options : cout << format("_%1$4d_ is : _%1$#4x_, _%1$#4o_, and _%1$s_ by default\n") % 18; // prints "_ 18_ is : _0x12_, _ 022_, and _18_ by default\n" // Taking the string value : std::string s; s= str( format(" %d %d ") % 11 % 22 ); assert( s == " 11 22 "); // ----------------------------------------------- // %% prints '%' cout << format("%%##%#x ") % 20 << endl; // prints "%##0x14 " // ----------------------------------------------- // Enforcing the right number of arguments // Too much arguments will throw an exception when feeding the unwanted argument : try { format(" %1% %1% ") % 101 % 102; // the format-string refers to ONE argument, twice. not 2 arguments. // thus giving 2 arguments is an error } catch (boost::io::too_many_args& exc) { cerr << exc.what() << "\n\t\t***Dont worry, that was planned\n"; } // Too few arguments when requesting the result will also throw an exception : try { cerr << format(" %|3$| ") % 101; // even if %1$ and %2$ are not used, you should have given 3 arguments } catch (boost::io::too_few_args& exc) { cerr << exc.what() << "\n\t\t***Dont worry, that was planned\n"; } cerr << "\n\nEverything went OK, exiting. \n"; return 0; } ``` -------------------------------- ### Storing and Reusing Format Objects in C++ Source: https://www.boost.org/doc/libs/latest/libs/format/example/sample_advanced Demonstrates how to create a format object, feed it arguments, and reuse it for multiple outputs. It also shows how the format object retains arguments until a new set is provided or an error occurs. ```cpp #include #include #include "boost/format.hpp" int main(){ using namespace boost::io; using std::cout; boost::format fmter("%1% %2% %3% %1% \n"); fmter % 10 % 20 % 30; cout << fmter; cout << fmter; fmter % 1001; try { cout << fmter; } catch (boost::io::too_few_args& exc) { cout << exc.what() << "***Dont worry, that was planned\n"; } cout << fmter % 1002 % 1003; cout << fmter % 10 % 1 % 2; return 0; } ``` -------------------------------- ### Handling Exceptions in C++ Format Operations Source: https://www.boost.org/doc/libs/latest/libs/format/example/sample_advanced Shows how to configure the format object to throw specific exceptions for formatting errors, such as too few or too many arguments. This enables robust error handling in C++ applications. ```cpp #include #include "boost/format.hpp" int main(){ using namespace boost::io; using std::cout; boost::format fmter("%1% %2% %3% %1% \n"); fmter.exceptions( boost::io::all_error_bits ^( boost::io::too_many_args_bit ) ); cout << fmter % 1 % 2 % 3 % 4 % 5 % 6 ; return 0; } ``` -------------------------------- ### Performance Benchmark Setup for Boost.Format Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Defines the test parameters and variables used for comparing performance of Boost.Format against POSIX printf and manual stream operations. Includes format string with reordering specifiers and multiple argument types. ```cpp string fstring="%3$0#6x %1$20.10E %2$g %3$0+5d \n"; double arg1=45.23; double arg2=12.34; int arg3=23; ``` -------------------------------- ### Unsupported Directives and String Truncations in C++ Source: https://www.boost.org/doc/libs/latest/libs/format/example/sample_advanced Demonstrates the behavior of Boost.Format with unsupported printf directives like %n and asterisk-fields, which are ignored. It also shows how to truncate strings to a specified width. ```cpp #include #include "boost/format.hpp" int main(){ using std::cout; using std::endl; cout << boost::format("|%5d| %n") % 7 << endl; cout << boost::format("|%*.*d|") % 7 << endl; cout << boost::format("%|.2s| %|8c|.\n") % "root" % "user"; return 0; } ``` -------------------------------- ### Tabulations for Column Alignment in Boost.Format Source: https://www.boost.org/doc/libs/latest/libs/format/example/sample_new_features Shows how to use tabulations (%|Nt| for N spaces, %|NTf| for N times character f) in Boost.Format for aligning columns of text. This is particularly useful when data fields have varying widths, ensuring that subsequent fields start at a consistent position. The example demonstrates aligning a phone number field. ```cpp #include "boost/format.hpp" #include #include #include #include int main() { using namespace std; using boost::format; using boost::io::group; vector names(1, "Marc-François Michel"), surname(1,"Durand"), tel(1, "+33 (0) 123 456 789"); names.push_back("Jean"); surname.push_back("de Lattre de Tassigny"); tel.push_back("+33 (0) 987 654 321"); for(unsigned int i=0; i #include #include "boost/format.hpp" int main(){ using namespace boost::io; using std::cout; using std::endl; cout << boost::format("%2s") % group(setfill('0'), setw(6), 1) << endl; cout << boost::format("%2$5s %1% %2$3s\n") % 1 % group(setfill('X'), setw(4), 2) ; cout << boost::format("%2$014x [%1%] %2$05s\n") % (boost::format("%05s / %s") % -18 % 7) % group(showbase, -100); return 0; } ``` -------------------------------- ### Modifying Format Items and Binding Arguments in C++ Source: https://www.boost.org/doc/libs/latest/libs/format/example/sample_advanced Illustrates how to modify specific format items (directives) and bind arguments to specific positions within the format string. This allows for greater control over the output formatting and argument usage. ```cpp #include #include #include "boost/format.hpp" int main(){ using namespace boost::io; using namespace boost::io::MyNS_Manips; using std::cout; boost::format fmter("%1% %2% %3% %2% %1% \n"); fmter.modify_item(4, group(setfill('_'), hex, showbase, setw(5)) ); cout << fmter % 1 % 2 % 3; fmter.bind_arg(1, 18); cout << fmter % group(hex, showbase, 20) % 30; fmter.modify_item(4, setw(0)); fmter.bind_arg(1, 77); cout << fmter % 10 % 20; try { cout << fmter % 6 % 7 % 8; } catch (boost::io::too_many_args& exc) { cout << exc.what() << "***Dont worry, that was planned\n"; } fmter.clear(); cout << fmter % 2 % 3; fmter.clear_binds(); cout << fmter % 1 % 2 % 3; return 0; } ``` -------------------------------- ### Rational Class and Stream Insertion Operator (C++) Source: https://www.boost.org/doc/libs/latest/libs/format/example/sample_userType Defines a `Rational` class to represent fractions and an overloaded `operator<<` for `std::ostream`. This operator formats the rational number as 'numerator/denominator' and handles alignment and padding using Boost.IO manipulators and `std::setw`, enabling its use with Boost.Format. ```cpp #include #include #include "boost/format.hpp" #include #include #include // Define showpos and noshowpos manipulators for custom formatting class ShowPos { public: bool showpos_; ShowPos(bool v) : showpos_(v) {} }; std::ostream& operator<<(std::ostream& os, const ShowPos& x) { if(x.showpos_) os.setf(ios_base:: showpos); else os.unsetf(ios_base:: showpos); return os; } ShowPos noshowpos(false); ShowPos showpos(true); // *** an example of UDT : a Rational class **** class Rational { public: Rational(int n, unsigned int d) : n_(n), d_(d) {} Rational(int n, int d); // convert denominator to unsigned friend std::ostream& operator<<(std::ostream&, const Rational&); private: int n_; // numerator unsigned int d_; // denominator }; Rational::Rational(int n, int d) : n_(n) { if(d < 0) { n_ = -n_; d=-d; } // make the denominator always non-negative. d_ = static_cast(d); } std::ostream& operator<<(std::ostream& os, const Rational& r) { using namespace std; streamsize n, s1, s2, s3; streamsize w = os.width(0); // width has to be zeroed before saving state. boost::io::basic_oaltstringstream oss; oss.copyfmt(os ); oss << r.n_; s1 = oss.size(); oss << "/" << noshowpos; // a rational number needs only one sign ! s2 = oss.size(); oss << r.d_ ; s3 = oss.size(); n = w - s3; if(n <= 0) { os.write(oss.begin(), oss.size()); } else if(os.flags() & std::ios_base::internal) { std::streamsize n1 = w/2, n2 = w - n1, t; t = (s3-s1) - n2; // is 2d part '/nnn' bigger than 1/2 w ? if(t > 0) { n1 = w -(s3-s1); // put all paddings on first part. n2 = 0; // minimal width (s3-s2) } else { n2 -= s2-s1; // adjust for '/', n2 is still w/2. } os << setw(n1) << r.n_ << "/" << noshowpos << setw(n2) << r.d_; } else { if(! (os.flags() & std::ios_base::left)) { // -> right align. (right bit is set, or no bit is set) os << string(boost::numeric_cast(n), ' '); } os.write(oss.begin(), s3); if( os.flags() & std::ios_base::left ) { os << string(boost::numeric_cast(n), ' '); } } return os; } ``` -------------------------------- ### Display Formatted Hexadecimal and Numeric Values Source: https://www.boost.org/doc/libs/latest/libs/format/example/sample_advanced Outputs a formatted hexadecimal number (0x0000ffffff9c) with leading zeros, followed by formatted integer values showing field width and sign handling. This demonstrates Boost Format library capabilities for precise numeric formatting with padding and alignment. ```cpp // prints "0x0000ffffff9c [-0018 / 7] -0100\n" ``` -------------------------------- ### Boost Format Usage with Rational Type (C++) Source: https://www.boost.org/doc/libs/latest/libs/format/example/sample_userType Demonstrates the usage of Boost.Format with the custom `Rational` type. It shows how standard format specifiers like `%02d`, `%+9d`, and alignment flags (`_`) can be applied to the `Rational` objects through the overloaded stream insertion operator. ```cpp #include #include "boost/format.hpp" // Assume Rational class and its operator<< are defined elsewhere // class Rational { ... }; // std::ostream& operator<<(std::ostream&, const Rational&); int main(){ using namespace std; using boost::format; using boost::io::group; using boost::io::str; string s; Rational r(16, 9); cout << "bonjour ! " << endl; // "bonjour !" cout << r << endl; // "16/9" cout << showpos << r << ", " << 5 << endl; // "+16/9, +5" cout << format("%02d : [%0+9d] \n") % 1 % r ; // "01 : [+016 / 0009]" cout << format("%02d : [%_+9d] \n") % 2 % Rational(9,160); // "02 : [+9 / 160]" cout << format("%02d : [%_+9d] \n") % 3 % r; // "03 : [+16 / 9]" cout << format("%02d : [%_9d] \n") % 4 % Rational(8,1234); // "04 : [8 / 1234]" cout << format("%02d : [%_9d] \n") % 5 % Rational(1234,8); // "05 : [1234 / 8]" cout << format("%02d : [%09d] \n") % 6 % Rational(8,1234); // "06 : [0008 / 1234]" cout << format("%02d : [%0+9d] \n") % 7 % Rational(1234,8); // "07 : [+1234 / 008]" cout << format("%02d : [%0+9d] \n") % 8 % Rational(7,12345); // "08 : [+07 / 12345]" cerr << "\n\nEverything went OK, exiting. \n"; return 0; } ``` -------------------------------- ### Boost.Format: Getting Formatted String Length Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Illustrates how to obtain the length of a formatted string using the Boost.Format library. Unlike printf's 'n' specifier, Boost.Format provides a size() member function to retrieve the character count after formatting. ```cpp format formatter("%+5d"); cout << formatter % x; unsigned int n = formatter.size(); ``` -------------------------------- ### Boost Format: Basic Output and Reordering Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Demonstrates the fundamental usage of the Boost Format library for simple string output and reordering of arguments using positional specifiers. No external dependencies beyond the Boost Format header are required. ```cpp using namespace std; using boost::format; cout << format("%1% %2% %3% %2% %1% \n") % "11" % "22" % "333"; ``` -------------------------------- ### Boost Format: Constructing and Using Format Objects Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Illustrates the core mechanism of the Boost Format library: constructing a format object from a string and then feeding arguments using the '%' operator. It also shows how to retrieve the formatted string. ```cpp using namespace std; using boost::format; // Constructing a format object format fmter("%2% %1%"); // Feeding arguments later fmter % 36; fmter % 77; // Dumping to a stream cout << fmter; // Getting the string value string s = fmter.str(); // Performing all steps at once cout << boost::format("%2% %1%") % 36 % 77; string s2 = str( format("%2% %1%") % 36 % 77 ); ``` -------------------------------- ### Boost Format: Alternative Formatting Expressions Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Presents multiple ways to express the same formatting output using Boost Format, including different specifier syntaxes and positional argument handling. This highlights the flexibility of the library. ```cpp using namespace std; using boost::format; cout << format("(x,y) = (%+5d,%+5d) \n") % -23 % 35; cout << format("(x,y) = (%|+5|,%|+5|) \n") % -23 % 35; cout << format("(x,y) = (%1$+5d,%2$+5d) \n") % -23 % 35; cout << format("(x,y) = (%|1$+5|,%|2$+5|) \n") % -23 % 35; ``` -------------------------------- ### Boost.Format vs. printf: Basic Usage Comparison Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Demonstrates the equivalent usage of Boost.Format with streams versus a standard C printf call for formatting strings with variables. It highlights that in most cases, the output will be the same, but notes potential differences due to format specification translation. ```cpp printf(s, x1, x2); // Equivalent using Boost.Format: cout << format(s) % x1 % x2; ``` -------------------------------- ### Format with Missing Arguments Using Custom Wrapper Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Demonstrates the behavior when requesting formatted output before all arguments are supplied with the custom wrapper. Missing argument placeholders are rendered as empty strings in the output. ```cpp cout << my_fmt(" _%2%_ _%1%_ \n") % 1 ; // prints " __ _1_ \n" ``` -------------------------------- ### Use Custom Format Wrapper with Extra Arguments Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Shows how to use the custom format wrapper to pass more arguments than needed. With the exception configuration from the wrapper, extra arguments are simply ignored rather than raising an exception. ```cpp cout << my_fmt(" %1% %2% \n") % 1 % 2 % 3 % 4 % 5; ``` -------------------------------- ### Outputting User-Defined Types with Boost.Format Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Demonstrates how Boost.Format handles output for user-defined types. Flags like %#x are applied recursively, while width and alignment settings apply to the final output. Special formatting options like zero-padding and space-padding can lead to less intuitive results for user-defined types. ```cpp #include #include // Assume Rational class is defined elsewhere struct Rational { int num, den; Rational(int n, int d) : num(n), den(d) {} // For demonstration, let's assume operator<< is defined for Rational friend std::ostream& operator<<(std::ostream& os, const Rational& r) { os << r.num << "/" << r.den; return os; } }; int main() { using namespace std; using namespace boost; Rational ratio(16, 9); // Example with %#x flag cerr << format("%#x \n") % ratio; // -> "0x10/0x9 \n" // Example with width and alignment cerr << format("% -8d") % ratio; // -> "16/9 " cerr << format("% =8d") % ratio; // -> " 16/9 " // Example with zero-padding and space-padding cerr << format("%+08d \n") % ratio; // -> "+00016/9" cerr << format("% 08d \n") % ratio; // -> "000 16/9" return 0; } ``` -------------------------------- ### Correct Format Argument Passing with Operator % Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Illustrates the intended and straightforward usage of operator% with Boost.Format, showing how arguments are correctly associated with the format string. The higher precedence of % ensures arguments are bound to the format object before stream insertion. ```cpp cout << format("%s %s ") % x % y << endl; ``` -------------------------------- ### Boost Format: Applying Manipulators with Arguments Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Shows how Boost format manipulators can be applied directly when passing arguments, affecting the formatting of those specific arguments. This is useful for applying formatting to individual data points. ```cpp using namespace std; using boost::format; using boost::io::group; cout << format("_%1%_ %1% \n") % group(showpos, setw(5), 101); ``` -------------------------------- ### Format object with operator% argument passing Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Demonstrates the operator% syntax used in Boost Format library for passing arguments to a format string. This approach allows chaining multiple arguments sequentially using the % operator, creating a readable and flexible alternative to function call syntax. ```cpp format(" %s at %s with %s\n") % x % y % z; ``` -------------------------------- ### Aesthetic Comparison: Operator % vs. Operator << with 'endf' Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Visually compares the readability of using operator% versus operator<< with the 'endf' manipulator for formatting multiple arguments and subsequent stream output. Operator% is shown to be more concise and visually clearer. ```cpp cout << format("%s %s %s") %x %y %z << "And avg is" << format("%s\n") %avg; ``` ```cpp cout << format("%s %s %s") << x << y << z << endf <<"And avg is" << format("%s\n") << avg; ``` -------------------------------- ### Outputting Manipulators After Format Arguments with Operator << Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Describes an intermediate solution for operator<< where overloading for manipulators allows direct output of stream manipulators (like std::flush) after format arguments, but still requires careful usage. ```cpp cout << format("%s %s \n") << x << y << flush ; ``` -------------------------------- ### Member function with() alternative syntax Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Shows the alternative member function approach using .with() for chaining arguments. While functionally equivalent to operator%, this syntax is more verbose and less visually distinctive, making it harder to quickly identify formatting operations in code. ```cpp format(fstr).with( x1 ).with( x2 ).with( x3 ); ``` -------------------------------- ### Boost Format: Modifying Format Strings with Manipulators Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Demonstrates how to dynamically modify format specifiers using Boost format manipulators like `showpos` and `setw`. This allows for runtime adjustments to the output format. ```cpp using namespace std; using boost::format; using boost::io::group; format fmter("_%1$+5d_ %1$d \n"); format fmter2("_%1%_ %1% \n"); fmter2.modify_item(1, group(showpos, setw(5)) ); cout << fmter % 101 ; cout << fmter2 % 101 ; ``` -------------------------------- ### Query and Set Format Exceptions in Boost.Format Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Defines two overloaded methods to query and configure which types of errors in Boost.Format raise exceptions. The first method allows setting new exception bits and returns the previous value, while the second only queries the current exception settings without modification. ```cpp unsigned char exceptions(unsigned char newexcept); // query and set unsigned char exceptions() const; // just query ``` -------------------------------- ### Boost Format: Precise Formatting with Posix-Printf Directives Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Illustrates how to achieve precise formatting using Posix-Printf style positional directives with Boost Format. This allows for control over sign display, field width, and alignment for numeric types. ```cpp using namespace std; using boost::format; cout << format("(x,y) = (%1$+5d,%2$+5d) \n") % -23 % 35; ``` -------------------------------- ### Argument Reordering in Boost.Format Source: https://www.boost.org/doc/libs/latest/libs/format/example/sample_new_features Demonstrates the ability to reorder arguments in a format string using positional specifiers. This allows for flexible output formatting without changing the order of variables passed to the format function. The output is controlled by the numbers following the '%' sign. ```cpp #include "boost/format.hpp" #include int main() { using namespace std; using boost::format; // Simple style of reordering cout << format("%1% %2% %3% %2% %1% \n") % "o" % "oo" % "O"; // prints "o oo O oo o \n" return 0; } ``` -------------------------------- ### Define basic_format Template Class in Boost Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Declares the basic_format template class for type-safe stream formatting. Supports construction from C-strings or std::basic_string with optional locale parameter. Provides argument passing via operator%, format parsing, and output via ostream operator<<. Includes exception control and utility functions for retrieving formatted strings. ```cpp namespace boost { template > class basic_format { public: typedef std::basic_string string_type; typedef typename string_type::size_type size_type; basic_format(const charT* str); basic_format(const charT* str, const std::locale & loc); basic_format(const string_type& s); basic_format(const string_type& s, const std::locale & loc); basic_format& operator= (const basic_format& x); void clear(); basic_format& parse(const string_type&); string_type str() const; size_type size() const; template basic_format& operator%(T& x); template basic_format& operator%(const T& x); friend std::basic_ostream& operator<< <> ( std::basic_ostream& , basic_format& ); unsigned char exceptions() const; unsigned char exceptions(unsigned char newexcept); }; typedef basic_format format; typedef basic_format wformat; } // namespace boost ``` -------------------------------- ### Template function approach for format arguments Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Illustrates the alternative template function approach that would be required if using function calls instead of operators. This method requires defining multiple template overloads for different argument counts and types, which creates limitations and combinatorial complexity when handling const/non-const parameter variations. ```cpp template string format(string s, const T1& x1, ...., const T1& xN); ``` -------------------------------- ### Boost Format: Absolute Tabulations for Consistent Alignment Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Introduces the 'absolute tabulations' feature in Boost Format, which ensures consistent field positioning on output lines regardless of previous field widths. This is particularly useful in loops for aligned table-like output. ```cpp using namespace std; using boost::format; // Assuming std::vector names, surname, tel are defined // for(unsigned int i=0; i < names.size(); ++i) // cout << format("%1%, %2%, %|40t|%3%\n") % names[i] % surname[i] % tel[i]; ``` -------------------------------- ### Boost Format: Classical printf Directive Usage Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Shows the direct application of classical printf format specifiers (like %s, %d) within the Boost Format library. This is suitable for basic type conversions and string construction. ```cpp using namespace std; using boost::format; cout << format("writing %s, x=%s : %d-th step \n") % "toto" % 40.23 % 50; ``` -------------------------------- ### Using Boost.Format Group Manipulators Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Shows how to use the 'group' function in Boost.Format to apply manipulators (like hex, showbase) to arguments. Manipulators within a group override formatting specified in the format string. The object to be printed must be the last item in the group. ```cpp #include #include #include // For std::hex, std::showbase int main() { using namespace std; using namespace boost; // Example 1: Basic group usage cout << format("%1% %2% %1%\n") % group(hex, showbase, 40) % 50; // Output: 0x28 50 0x28\n // Example 2: Manipulator overriding format string // hex and showbase from group override 'd' (decimal) in the format string cout << format("%1$d %2% %1%\n") % group(hex, showbase, 40) % 50; // Output: 0x28 50 0x28\n return 0; } ``` -------------------------------- ### Operator % Precedence and Grouping Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Explains how the higher precedence of operator% compared to stream insertion operator<< simplifies expression parsing. It shows that grouping with parentheses is sometimes necessary for arithmetic operators but not for sequential format argument passing. ```cpp cout << ( format("%s %s ") % x % y ) << endl; ``` -------------------------------- ### Using 'endf' Manipulator with Operator << for Format Completion Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Presents a solution using a special 'endf' manipulator to explicitly signal the end of format arguments when using operator<<. This allows subsequent stream operations to be correctly interpreted. ```cpp cout << format("%s %s ") << x << y << endf << endl; ``` -------------------------------- ### Optimize Boost.Format Performance with Const Format Object Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Provides a performance optimization technique for iterated formatting operations by parsing the format string once into a const format object and copying it for each formatting operation, reducing overhead from repeated string parsing. ```cpp const boost::format fmter(fstring); dest << boost::format(fmter) % arg1 % arg2 % arg3 ; ``` -------------------------------- ### Stream operator requirement for format arguments Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Demonstrates the stream insertion operator requirement for classes that can be used with the format function. Objects must define operator<< with a stream to be printable, and providing both const and non-const overloads creates exponential template combinations (2^N for N arguments). ```cpp operator<< ( stream, const T&) ``` -------------------------------- ### Incorrect Stream-like Format Usage with Operator << Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Demonstrates a common pitfall when using operator<< with a format object, where subsequent output to the stream might be misinterpreted as part of the format arguments. This highlights the non-stream-like nature of format objects. ```cpp cout << format("%s %s ") << x; cout << y ; // uh-oh, format is not really a stream manipulator ``` -------------------------------- ### Create Free Function str() for basic_format in Boost Source: https://www.boost.org/doc/libs/latest/libs/format/doc/format Provides a convenience free function template that extracts the formatted string from a basic_format object. Accepts a const reference to a basic_format instance and returns the resulting std::basic_string by calling the str() member function. ```cpp template std::basic_string str(const basic_format& f) { return f.str(); } ``` -------------------------------- ### Problematic Stream Insertion with Operator << and Multiple Arguments Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Details the ambiguity and potential issues when using operator<< with multiple arguments intended for a format string. The stream's left-to-right evaluation can lead to incorrect interpretation of arguments. ```cpp cout << format("%s %s ") << x << y << endl; // Understood as: ( ( ( cout << format("%s %s ") ) << x ) << y ) << endl; ``` -------------------------------- ### Operator% precedence behavior with arithmetic operations Source: https://www.boost.org/doc/libs/latest/libs/format/doc/choices Shows how operator% precedence works in expressions combining format operations with arithmetic. When written as format("%s") % x+y, the compiler evaluates it as (format("%s") % x) + y, where the result is a format object that cannot be added to y, causing a compile-time error that prevents precedence mistakes. ```cpp format("%s") % x+y; // Error: tmp (format object) has no operator+ with y format("%s") % (x+y); // Correct: arithmetic evaluated first ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.