bodihex
bodihex

Reputation: 89

Custom format specifier with {fmt} for custom class

How would I go about allowing for custom padding, etc.. when formatting my own custom types?

struct S
{
   int x;
};

template<> struct fmt::formatter<S> {
    template <typename ParseContext>
        constexpr auto parse(ParseContext& ctx) { return ctx.begin(); }

    template <typename FormatContext>
        auto format(const S& s, FormatContext& ctx)
        {
            return format_to(ctx.out(), "{}", s.x);
        }
};

S s; s.x = 1;
fmt::format("{:<10}", s); // error

Upvotes: 3

Views: 3070

Answers (2)

N Cheadle
N Cheadle

Reputation: 113

This is similar to the answer provided by cpplearner, but with the latest release of fmt you can use nested_formatter. Be aware that this is still labeled as experimental.

The documentation for fmt has also recently been updated to include a better review of formatting user defined types [link].

I changed the format specifier to make it obvious that the formatting is getting forwarded.

#include <fmt/format.h>

struct S
{
   int x;
};

template <>
struct fmt::formatter<S> : fmt::nested_formatter<int> {
  auto format(const S& s, format_context& ctx) const {
    return write_padded(ctx, [&](auto out) {
      return format_to(out, "(S.x: {})", nested(s.x));
    });
  }
};

int main() {
    S s; 
    s.x = 123456789;
    fmt::print("s = {:>20#X}", s);
}

Outputs:

s =     (S.x: 0X75BCD15)

To view in Godbolt: https://godbolt.org/z/z7WxzWhvv

Upvotes: 0

cpplearner
cpplearner

Reputation: 15908

Since you seem to only reuse existing format specifiers, you can just forward operations to formatter<int>:

template<> struct fmt::formatter<S> {
    formatter<int> int_formatter;

    template <typename ParseContext>
        constexpr auto parse(ParseContext& ctx)
        {
            return int_formatter.parse(ctx);
        }

    template <typename FormatContext>
        auto format(const S& s, FormatContext& ctx) const
        {
            return int_formatter.format(s.x, ctx);
        }
};

If you want to use different syntax, or if you don't want to depend on formatter<int>, you can also manually parse the format string in formatter::parse.

template<> struct fmt::formatter<S> {
    enum { left, right } align = right;
    int width = 0;

    template <typename ParseContext>
        constexpr auto parse(ParseContext& ctx)
        {
            auto it = ctx.begin();

            // parse /align/
            if (*it == '<') {
                align = left;
                ++it;
            }

            // parse /width/
            const char* width_str_begin = std::to_address(it);
            const char* width_str_bound = std::to_address(ctx.end());
            auto [ptr, ec] = std::from_chars(width_str_begin, width_str_bound, width);
            auto length = ptr - width_str_begin;
            it += length;

            // error handling omitted

            return it;
        }

    template <typename FormatContext>
        auto format(const S& s, FormatContext& ctx) const
        {
            return format_to(ctx.out(), align == left ? "{:<{}}" : "{:>{}}", s.x, width);
        }
};

Upvotes: 4

Related Questions