Fmt: Padding and Alignment etc doesn't work for user defined types.

Created on 21 Jun 2018  路  3Comments  路  Source: fmtlib/fmt

Hello, I'm trying to make padding and alignment work for user defined types. I'm using 5.0.0.

The following doesn't seem to work at the moment. Apologies if this has already been discussed in another ticket. However, I couldn't find it by searching.

#include <fmt/format.h>

enum class ExampleEnum : char {
    A = '0',
    B = '1',
    C = '3',
    D = '?'
};

namespace fmt {
    template<> struct formatter<ExampleEnum> {
        template <typename ParseContext> constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }
        template <typename FormatContext> auto format(const ExampleEnum& e, FormatContext& ctx)
            {
                switch (e) {
                case ExampleEnum::A:
                    return format_to(ctx.begin(), "A");
                case ExampleEnum::B:
                    return format_to(ctx.begin(), "B");
                case ExampleEnum::C:
                    return format_to(ctx.begin(), "C");
                case ExampleEnum::D:
                    return format_to(ctx.begin(), "D");
                };
                return format_to(ctx.begin(), "NOT_RECOGNISED");
            }
    };
}

int main() {
    fmt::print("{}\n", ExampleEnum::A);
    fmt::print("{:6}\n", ExampleEnum::A);
    return 0;
}

$ clang++ --std=c++14 -isystem fmt/include/  FmtBug.cpp -L build/fmt -lfmt_lib
$ ./a.out
A
terminate called after throwing an instance of 'fmt::v5::format_error'
  what():  unknown format specifier
Aborted

As you can see, fmt can format correctly if you're just formatting something very basic with only {}. However as soon as I add padding or alignment, it causes a runtime error.

Most helpful comment

The intent of the parse function is to give the formatter control of how to parse the format string, including alignment, padding, etc. To apply the defaults in a situation like this, I believe it is supported to derive your formatter from a pre-existing formatter to get the defaults for parse and similarly delegate to the base class for the format rules.

enum class ExampleEnum : char
{
    A = '0',
    B = '1',
    C = '3',
    D = '?'
};


template <>
struct fmt::formatter<ExampleEnum> : fmt::formatter<fmt::string_view>
{
    using base = fmt::formatter<fmt::string_view>;

    template <typename FormatContext>
    auto format(const ExampleEnum& e, FormatContext& ctx)
    {
        switch (e)
        {
        case ExampleEnum::A:
            return base::format("A", ctx);
        case ExampleEnum::B:
            return base::format("B", ctx);
        case ExampleEnum::C:
            return base::format("C", ctx);
        case ExampleEnum::D:
            return base::format("D", ctx);
        };
        return base::format("NOT_RECOGNISED", ctx);
    }
};

TEST(StringFormatTests, ExampleEnum)
{
    EXPECT_EQ("A", fmt::format("{}", ExampleEnum::A));
    EXPECT_EQ("A     ", fmt::format("{:6}", ExampleEnum::A));
}

All 3 comments

The intent of the parse function is to give the formatter control of how to parse the format string, including alignment, padding, etc. To apply the defaults in a situation like this, I believe it is supported to derive your formatter from a pre-existing formatter to get the defaults for parse and similarly delegate to the base class for the format rules.

enum class ExampleEnum : char
{
    A = '0',
    B = '1',
    C = '3',
    D = '?'
};


template <>
struct fmt::formatter<ExampleEnum> : fmt::formatter<fmt::string_view>
{
    using base = fmt::formatter<fmt::string_view>;

    template <typename FormatContext>
    auto format(const ExampleEnum& e, FormatContext& ctx)
    {
        switch (e)
        {
        case ExampleEnum::A:
            return base::format("A", ctx);
        case ExampleEnum::B:
            return base::format("B", ctx);
        case ExampleEnum::C:
            return base::format("C", ctx);
        case ExampleEnum::D:
            return base::format("D", ctx);
        };
        return base::format("NOT_RECOGNISED", ctx);
    }
};

TEST(StringFormatTests, ExampleEnum)
{
    EXPECT_EQ("A", fmt::format("{}", ExampleEnum::A));
    EXPECT_EQ("A     ", fmt::format("{:6}", ExampleEnum::A));
}

This is great. Thanks.

p.s. I was following the pattern described here:

http://fmtlib.net/latest/api.html#formatting-user-defined-types

Which doesn't involve inheriting from fmt::formatter<fmt::string_view>

Perhaps there could be an example of a formatter that inherits from fmt::formatter<fmt::string_view> in the docs?

Perhaps there could be an example of a formatter that inherits from fmt::formatter in the docs?

Good idea, added one in https://github.com/fmtlib/fmt/commit/60c662b3a72b4b17bcff92ec17482259150676a0.

Was this page helpful?
0 / 5 - 0 ratings