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

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

Closed
avikivity opened this issue Oct 22, 2023 · 8 comments
Closed

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

avikivity opened this issue Oct 22, 2023 · 8 comments

Comments

@avikivity
Copy link

#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.

@vitaut
Copy link
Contributor

vitaut commented May 4, 2024

Fixed in 16cec4f, thanks for reporting. I recommend opening an LWG issue since the standard map formatter introduced in P2286 likely has the same problem.

@vitaut vitaut closed this as completed May 4, 2024
@avikivity
Copy link
Author

How does one file an LWG issue?

@Dani-Hub
Copy link
Contributor

Dani-Hub commented May 5, 2024

How does one file an LWG issue?

Read How to submit an LWG issue
The email address is provided in the top part of the same document

@avikivity
Copy link
Author

Done, thanks.

@avikivity
Copy link
Author

avikivity commented May 5, 2024

Unfortunately my report was rejected. I was too lazy to port it to <format>, but when I did I couldn't reproduce as none of the trunk stdlibs on godbolt support formatting maps [1], which is a prerequisite for a reproducer.

[1] though llvm/llvm-project@b237354 mentions it's implemented

@vitaut
Copy link
Contributor

vitaut commented May 5, 2024

I don't think we need a repro, the broken behavior is clear from the spec: https://eel.is/c++draft/format#range.fmtmap-2.

@avikivity
Copy link
Author

If you want me to read standardese, then I want a raise

@vitaut
Copy link
Contributor

vitaut commented May 19, 2024

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