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

Fix udl_compiled_string with non-byte chars (e.g. wchar) #2242

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -947,8 +947,9 @@ size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
inline namespace literals {
template <detail::fixed_string Str>
constexpr detail::udl_compiled_string<remove_cvref_t<decltype(Str.data[0])>,
sizeof(Str.data), Str>
constexpr detail::udl_compiled_string<
remove_cvref_t<decltype(Str.data[0])>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move remove_cvref_t<decltype(Str.data[0])> to a default template parameter to avoid repetition? i.e.

template <detail::fixed_string Str, Char = remove_cvref_t<decltype(Str.data[0])>>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's not possible because compilers expect to see certain template arguments - https://godbolt.org/z/6odY6E78h

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we at least do typename Str::char_type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about value_type as in basic_string<>::value_type ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, value_type is a more appropriate.

Copy link
Contributor Author

@alexezeder alexezeder Apr 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget that Str is not a type, it's an object since it's passed as a non-type template parameter, so we have these options here:

<remove_cvref_t<decltype(Str.data[0])>, sizeof(Str.data) / sizeof(decltype(Str.data[0])), Str>
vs.
<typename decltype(Str)::value_type, sizeof(Str.data) / sizeof(typename  decltype(Str)::value_type), Str>

and they both are not great.

But the size of fixed_string can be exposed by a static member or function, like so:

<typename decltype(Str)::value_type, decltype(Str)::size, Str>

Just let me know if this approach is preferable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine as is then. Thanks for the fix.

sizeof(Str.data) / sizeof(decltype(Str.data[0])), Str>
operator""_cf() {
return {};
}
Expand Down
1 change: 1 addition & 0 deletions test/compile-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ TEST(CompileTest, CompileFormatStringLiteral) {
using namespace fmt::literals;
EXPECT_EQ("", fmt::format(""_cf));
EXPECT_EQ("42", fmt::format("{}"_cf, 42));
EXPECT_EQ(L"42", fmt::format(L"{}"_cf, 42));
}
#endif

Expand Down