Skip to content

Using FMT_COMPILE with format_to and a user defined type fails to compile #4720

@yatsukha

Description

@yatsukha

Example program:

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

#include <algorithm>
#include <array>
#include <string_view>

struct UDT {};

template<>
struct fmt::formatter<UDT> : formatter<std::string_view> {
    constexpr auto format(
        UDT const&,
        fmt::format_context& ctx
    ) const noexcept {
        return fmt::formatter<std::string_view>::format("hello", ctx);
    }
};

#include <fmt/format.h>

int main() {
    std::array<char, 256> buf = {};
    // fmt::format_to(buf.data(), "{}", UDT{}); // compiles
    fmt::format_to(buf.data(), FMT_COMPILE("{}"), UDT{}); // fails to compile

    fmt::println("{}", std::string_view(buf.begin(), std::find(buf.begin(), buf.end(), '\0')));
}

Locally I've tested with fmt version 12.1 and

Homebrew clang version 22.1.0
Target: arm64-apple-darwin24.5.0

On Compiler Explorer I've tested x86-64 clang 22.1.0 and gcc 15.2 with fmt version 12.0 and trunk. They all fail.

Metadata

Metadata

Assignees

No one assigned

    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