Skip to content

Specializing std::pair formatter leads to compile failures #3685

@avikivity

Description

@avikivity
#include <map>
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <fmt/std.h>
#include <fmt/ostream.h>

struct x {};

template <typename K>
struct fmt::formatter<std::pair<K, x>> : fmt::formatter<std::string_view> {
    auto format(const std::pair<K, x>& p, auto& ctx) const {
        return fmt::format_to(ctx.out(), "x/x");
    }
};

int main() {
    std::map<x, x> v{{}, {}};
    fmt::print("{}\n", v);
    return 0;
}

This doesn't build, since map's formatter calls the pair formatter's set_brackets() and set_separator(), which don't exist.

set_brackets() and set_separator() are undocumented, so it's not reasonable to expect them to exist.

I think map's formatter should check to see if it's using the standard pair formatter before calling undocumented methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions