Skip to content

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

Closed
@wkenyon

Description

@wkenyon

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions