Typesafe printf-like formatting with boost::format





6
Date Submitted Thu. Aug. 3rd, 2006 12:10 PM
Revision 1 of 1
Helper bobbyrward
Tags boost | CPlusPlus | Format | printf
Comments 0 comments
boost::format gives you the ablility to safely use printf like formatting as well as positional format specifiers ala .NET.

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

using boost::format;
using namespace std;

int main(int argc, char* argv[]) {
    unsigned int hexNum = 0xFFEEDDCC;
    cout << format("Formatting a hex number: %08X") % hexNum << endl;

    cout << format("Positional Specifiers: %3% %1% %2% %1%")
        % 1 % 2 % 3 << endl;

    return 0;
}
 
prints:

Formatting a hex number: 0xFFEEDDCC
Positional Specifiers: 3 1 2 1

Bobby Ward

Bobby R Ward
---------------------
bobbyrward@gmail.com

Comments

There are currently no comments for this snippet.

Voting