Questions tagged [boost-format]

The Boost Format Library provides a class for formatting arguments according to a format-string

The format library provides a class for formatting arguments according to a format-string, as does printf, but with two major differences :

  • format sends the arguments to an internal stream and so is entirely type-safe and naturally supports all user-defined types
  • the ellipsis (...) cannnot be used correctly in the strongly typed context of format and thus the function call with arbitrary arguments is replaced by successive calls to an argument feeding operator%
32 questions
25
votes
5 answers

C++11 Equivalent to Boost.Format

Anything like Boost.Format in the C++11 standard? I've been able to avoid using Boost with a better C++11 option for every other need I've had. For that matter, Boost.Format doesn't hold a candle to the syntax of Python format(). Something like that…
Chris Redford
  • 16,982
  • 21
  • 89
  • 109
12
votes
2 answers

Why boost::format cannot be converted directly to std::string?

Following is not possible: std::string s = boost::format("%d") % 1; // error You have to explicitely call the method str(): std::string s = (boost::format("%d") % 1).str(); // OK It would only be syntactic sugar, but why not just add the…
mr_georg
  • 3,635
  • 5
  • 35
  • 52
11
votes
3 answers

boost::format with variadic template arguments

Suppose I have a printf-like function (used for logging) utilizing perfect forwarding: template void awesome_printf(std::string const& fmt, Arguments&&... args) { boost::format f(fmt); f % /* How to specify `args`…
void.pointer
  • 24,859
  • 31
  • 132
  • 243
11
votes
3 answers

Formatting n significant digits in C++ without scientific notation

I want to format a floating point value to n significant digits but never using scientific notation (even if it would be shorter). The format specification %f doesn't deal in significant digits, and %g will sometimes give me scientific notation…
PeteC
  • 1,047
  • 9
  • 15
10
votes
4 answers

boost::format and custom printing a std containers

I have a function in my namespace ns that helps me print STL containers. For example: template std::ostream& operator<<(std::ostream& stream, const std::set& set) { stream << "{"; bool first = true; for (const T& item :…
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
7
votes
1 answer

Is it possible to use Boost.Format with a preallocated buffer?

I was wondering whether Boost.Format does support using a fixed-width / preallocated buffer as output instead of a dynamic buffer managed by the lib itself? That is, normally you'd do: boost::format myfmt("arg1: %1% / arg2: %2%"); // e.g.: cout <<…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
7
votes
2 answers

Why do Boost Format and printf behave differently on same format string

The Boost Format documentation says: 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…
ToBe
  • 921
  • 1
  • 9
  • 23
7
votes
1 answer

Using %s format specifier with boost::format and std::string

I know that using the %s format specifier and std::string like this leads to undefined behaviour: std::string myString = "test"; printf("%s", myString); But is it save to use the same specifier and a std::string with boost::format? #include…
Ronald McBean
  • 1,417
  • 2
  • 14
  • 27
5
votes
2 answers

Using boost::format in a boost::lambda

For some reason, I fail to use boost::format in a boost::lambda. Here is a (hopefully) compilable simplification of my code : #include #include #include #include #include…
icecrime
  • 74,451
  • 13
  • 99
  • 111
5
votes
2 answers

Printing bools with boost::format as symbolic values?

How do I print boolean values with boost::format as symbolic values? Can this be done without boost::io::group? It seems that flags sent to the stream beforehand get retested: #include #include #include int…
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
4
votes
2 answers

Using boost::format %s specifier with UTF-8 strings

We are adding support for UTF8 to an existing application with a large code base. This application uses boost::format(), and the output in non-ASCII characters is not aligning properly. Specifically, when using the %{width}.{length}s specifier,…
Grognard61
  • 41
  • 4
4
votes
1 answer

How to use boost::format to zeropad a number where the number of decimal places is contained in a variable?

I would like to zeropad a number such that it has 5 digits and get it as a string. This can be done with the following: unsigned int theNumber = 10; std::string theZeropaddedString = (boost::format("%05u") % theNumber).str(); However, I do not…
user3731622
  • 4,844
  • 8
  • 45
  • 84
3
votes
6 answers

Real positional string formatting?

(Note: I know of Boost.Format, I'm looking for a better way to do the following.) First a use-case example: In some countries, you name a person by calling his / her surname first and the forename last, while in other countries it's the exact…
Xeo
  • 129,499
  • 52
  • 291
  • 397
3
votes
1 answer

How to create / combine / concatenate boost::format from multiple boost::format

This is given: boost::format greeting("%s (Greeting)"); boost::format name("%s (Name)"); 'greetingwithname' should combine and reuses 'greeting' and 'name' so that it is equivalent to this: boost::format greetingwithname("%s (Greeting) %s…
ToBe
  • 921
  • 1
  • 9
  • 23
3
votes
1 answer

Odd behavior in boost::format hex

I'm trying to format a binary array: char* memblock to a hex string. When I use the following: fprintf(out, "0x%02x,", memblock[0]); I get the following output: 0x7f, When I try to use boost::format on an ofstream like so: std::ofstream outFile…
Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
1
2 3