Skip to content

Custom formatter example from the documentation fails to compile #3894

Closed
@jeremy-rifkin

Description

@jeremy-rifkin

The following code using a custom formatter example from the documentation fails to compile:

#include <fmt/format.h>

enum class color { red, green, blue };

template <>
struct fmt::formatter<color> : formatter<string_view> {
    // parse is inherited from formatter<string_view>.

    auto format(color c, format_context& ctx) const -> format_parse_context::iterator;
};

auto fmt::formatter<color>::format(color c, format_context& ctx) const -> format_parse_context::iterator {
    string_view name = "unknown";
    switch (c) {
        case color::red:
            name = "red";
            break;
        case color::green:
            name = "green";
            break;
        case color::blue:
            name = "blue";
            break;
    }
    return formatter<string_view>::format(name, ctx);
}

auto foo(color c) {
    return fmt::format("{}", c);
}

https://godbolt.org/z/ojKnYf4ad

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