5

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 <iomanip>
#include <iostream>
#include <boost/format.hpp>

int main()
{
  std::cout
    << std::boolalpha
    << true << " "
    << boost::format("%1% %2%\n")
         % true
         % boost::io::group(std::boolalpha, true)
    ;
}

Output

true 1 true
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137

2 Answers2

2

You can archive that like this:

#include <iomanip>
#include <iostream>
#include <boost/format.hpp>

int main()
{
  std::cout
    << std::boolalpha
    << true << " "
    << boost::format("%1$b %2%\n")
         % true
         % boost::io::group(std::boolalpha, true)
    ;
}
June
  • 41
  • 6
1

It doesn't appear to me that you can.

I looked at the Boost.Format documentation and the code, and didn't see anything.

On the other hand, the sample code shows how to write a formatter for a user-defined type. You could write one for "bool"

Marshall Clow
  • 15,972
  • 2
  • 29
  • 45
  • Since 1.66.0, `boost::format` provides the `b` conversion-specifier which may serve the purpose: https://www.boost.org/doc/libs/1_66_0/libs/format/doc/format.html – yaobin Jun 05 '20 at 18:03