Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Padding and Alignment etc doesn't work for user defined types. #787

Closed
wkenyon opened this issue Jun 21, 2018 · 3 comments
Closed

Padding and Alignment etc doesn't work for user defined types. #787

wkenyon opened this issue Jun 21, 2018 · 3 comments

Comments

@wkenyon
Copy link

wkenyon commented Jun 21, 2018

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.

@mwinterb
Copy link
Contributor

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));
}

@vitaut vitaut closed this as completed Jun 22, 2018
@wkenyon
Copy link
Author

wkenyon commented Jun 22, 2018

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?

@vitaut
Copy link
Contributor

vitaut commented Jul 8, 2018

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

Good idea, added one in 60c662b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants