Unverified Commit a7e356cc authored by Victor Zverovich's avatar Victor Zverovich Committed by GitHub

Update README.rst

parent e758bfba
...@@ -108,24 +108,29 @@ Format strings can be checked at compile time: ...@@ -108,24 +108,29 @@ Format strings can be checked at compile time:
format_to(buf, "{:x}", 42); // replaces itoa(42, buffer, 16) format_to(buf, "{:x}", 42); // replaces itoa(42, buffer, 16)
// access the string using to_string(buf) or buf.data() // access the string using to_string(buf) or buf.data()
An object of any user-defined type for which there is an overloaded Formatting of user-defined types is supported via a simple
:code:`std::ostream` insertion operator (``operator<<``) can be formatted: `extension API <http://fmtlib.net/latest/api.html#formatting-user-defined-types>`_:
.. code:: c++ .. code:: c++
#include "fmt/ostream.h" #include "fmt/format.h"
class Date { struct date {
int year_, month_, day_; int year, month, day;
public: };
Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
template <>
struct fmt::formatter<date> {
template <typename ParseContext>
constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }
friend std::ostream &operator<<(std::ostream &os, const Date &d) { template <typename FormatContext>
return os << d.year_ << '-' << d.month_ << '-' << d.day_; auto format(const date &d, FormatContext &ctx) {
return format_to(ctx.begin(), "{}-{}-{}", d.year, d.month, d.day);
} }
}; };
std::string s = fmt::format("The date is {}", Date(2012, 12, 9)); std::string s = fmt::format("The date is {}", date{2012, 12, 9});
// s == "The date is 2012-12-9" // s == "The date is 2012-12-9"
You can create your own functions similar to `format You can create your own functions similar to `format
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment