fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
20.56k stars 2.46k forks source link

Formatting for strings with custom allocators fails to compile #3938

Closed dieram3 closed 5 months ago

dieram3 commented 5 months ago

When using a std::basic_string with a custom allocator, it fails to compile in a few use cases, e.g.:

std::string f1() {
    // This compiles.
    return fmt::format("{}", std::tuple<std::string>{});
}

std::string f2() {
    // This compiles.
    return fmt::format("{}", std::pmr::string{});
}

std::string f3() {
    // This does not compile.
    return fmt::format("{}", std::tuple<std::pmr::string>{});
}

You can reproduce the error here: https://godbolt.org/z/4oa1jqr1G

I checked the code, and it seems std::basic_string is specialized only for the default allocator. I think the fix might be as simple as replacing

FMT_FORMAT_AS(std::basic_string<Char>, basic_string_view<Char>);

with

template <typename Char, typename Alloc>
class formatter<std::basic_string<Char, std::char_traits<Char>, Alloc>, Char>
    : public formatter<basic_string_view<Char>, Char> {};

It is also not clear why printing a std::pmr::string alone works. Maybe it's falling back to string_view while the tuple version is not.

vitaut commented 5 months ago

Thanks for reporting.

I checked the code, and it seems std::basic_string is specialized only for the default allocator. I think the fix might be as simple as replacing ...

That looks like the correct fix (modulo also making char_traits a template parameter). Could you submit a PR?

dieram3 commented 5 months ago

Sure thing!