Skip to content

Broken interaction between std::variant and ADL format_as #4574

@Mishura4

Description

@Mishura4

When formatting a variant, fmt 12.0.0 chokes if any of the alternatives relies on format_as to be formattable.
The documentation says:

A std::variant is only formattable if every variant alternative is formattable, and requires the __cpp_lib_variant library feature.

Which does not seem to align with the behavior.

Godbolt link: https://godbolt.org/z/vrjEPsbvr

Code:

#include <fmt/format.h>
#include <fmt/std.h>

#include <iostream>

struct foo {
    float bar;

    friend float format_as(const foo& f) { return f.bar; }
};

int main() {
    foo simple_foo{};
    std::variant<foo> variant_foo{};
    std::variant<float> variant_float{};

    std::cout << fmt::format("{}", simple_foo); // ok
    std::cout << fmt::format("{}", variant_float); // ok
    std::cout << fmt::format("{}", variant_foo); // broken
}

Error log (from Godbolt): https://pastebin.com/LXiBz4mX

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