{fmt} is an open-source formatting library providing a fast and safe alternative to C stdio and C++ iostreams.

Thanks for creating this library. It’s been a hole in C++ for a long time. I’ve used both boost::format and loki::SPrintf , and neither felt like the right answer. This does.

Format API¶

The format API is similar in spirit to the C printf family of function but is safer, simpler and several times faster than common standard library implementations. The format string syntax is similar to the one used by str.format in Python:

fmt :: format ( "The answer is {}." , 42 );

The fmt::format function returns a string “The answer is 42.”. You can use fmt::memory_buffer to avoid constructing std::string :

fmt :: memory_buffer out ; format_to ( out , "For a moment, {} happened." , "nothing" ); out . data (); // returns a pointer to the formatted data

The fmt::print function performs formatting and writes the result to a stream:

fmt :: print ( stderr , "System error code = {}

" , errno );

The file argument can be omitted in which case the function prints to stdout :

fmt :: print ( "Don't {}

" , "panic" );

The Format API also supports positional arguments useful for localization:

fmt :: print ( "I'd rather be {1} than {0}." , "right" , "happy" );

Named arguments can be created with fmt::arg . This makes it easier to track what goes where when multiple arguments are being formatted:

fmt :: print ( "Hello, {name}! The answer is {number}. Goodbye, {name}." , fmt :: arg ( "name" , "World" ), fmt :: arg ( "number" , 42 ));

If your compiler supports C++11 user-defined literals, the suffix _a offers an alternative, slightly terser syntax for named arguments: