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

Formatting for strings with custom allocators fails to compile #3938

Closed
dieram3 opened this issue Apr 17, 2024 · 2 comments · Fixed by #3943
Closed

Formatting for strings with custom allocators fails to compile #3938

dieram3 opened this issue Apr 17, 2024 · 2 comments · Fixed by #3943

Comments

@dieram3
Copy link
Contributor

dieram3 commented Apr 17, 2024

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
Copy link
Contributor

vitaut commented Apr 19, 2024

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
Copy link
Contributor Author

dieram3 commented Apr 22, 2024

Sure thing!

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

Successfully merging a pull request may close this issue.

2 participants