The basic Stannum.Printf interface is available through the following header:
#include <stannum/io/printf.h>
The library provides two different (classes of) formatting functions:
template<class Sink, class... Args> auto printf(Sink &&x, const char *fmt, const Args &...a)
-> declspec(printf_core(std::forward<Sink>(x), *(detail::format_core*)0));
— writes to an arbitrary sink. Accepts std::ostream
, std::streambuf*
, stannum::detail::iter_type
and can be extended to accept pretty much anything. For std::ostream
behaves as a formatted output function (see ISO/IEC 14882:2011 [ostream.formatted.reqmts]).
template<class... Args> std::string sprintf(const char *fmt, const Args &...a);
— returns the result as an std::string
.
For example:
#include <iostream>
#include <stannum/io/printf.h>
namespace Sn { using namespace stannum::io; }
int main() {
char buf[256];
Sn::printf(buf, "Hello world! %d %g\n", 123, 3.14159);
std::cout << buf;
Sn::printf(std::cout, "Hello world! %d %g\n", 123, 3.14159);
Sn::printf(std::cout.rdbuf(), "Hello world! %d %g\n", 123, 3.14159);
std::cout << Sn::sprintf("Hello world! %d %g\n", 123, 3.14159);
}
All the above statements print, as expected:
Hello world! 123 3.14159
The functions listed here are merely wrappers of the library core class stannum::io::detail::format_core
. The user may build her own wrappers instead.