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

Custom formatter example from the documentation fails to compile #3894

Closed
jeremy-rifkin opened this issue Mar 15, 2024 · 2 comments
Closed

Comments

@jeremy-rifkin
Copy link

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

@vitaut
Copy link
Contributor

vitaut commented Mar 15, 2024

Good catch, thanks. Fixed in #3895.

@vitaut vitaut closed this as completed Mar 15, 2024
@jeremy-rifkin
Copy link
Author

Thanks for the lightning-fast fix!

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

2 participants