-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add UDL as replacement for FMT_COMPILE #2043
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
506230b
add _cf literal as replacement for FMT_COMPILE
alexezeder 8c8b1cc
apply requested changes
alexezeder 855e488
add empty format string into CompileTest::CompileFormatStringLiteral
alexezeder 6ea2ca8
fix for empty format string
alexezeder b81bf00
update FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
alexezeder e7d035d
remove deduction guide for `fixed_string`
alexezeder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,15 @@ | |
|
||
#include "format.h" | ||
|
||
#ifndef FMT_USE_NONTYPE_TEMPLATE_PARAMETERS | ||
# if defined(__cpp_nontype_template_parameter_class) && \ | ||
(!FMT_GCC_VERSION || FMT_GCC_VERSION >= 903) | ||
# define FMT_USE_NONTYPE_TEMPLATE_PARAMETERS 1 | ||
# else | ||
# define FMT_USE_NONTYPE_TEMPLATE_PARAMETERS 0 | ||
# endif | ||
#endif | ||
|
||
FMT_BEGIN_NAMESPACE | ||
namespace detail { | ||
|
||
|
@@ -37,6 +46,24 @@ struct is_compiled_string : std::is_base_of<compiled_string, S> {}; | |
*/ | ||
#define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string) | ||
|
||
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS | ||
template <typename Char, size_t N> struct fixed_string { | ||
constexpr fixed_string(const Char (&str)[N]) { | ||
copy_str<Char, const Char*, Char*>(static_cast<const Char*>(str), str + N, | ||
data); | ||
} | ||
Char data[N]{}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's either
|
||
}; | ||
|
||
template <typename Char, size_t N, fixed_string<Char, N> Str> | ||
struct udl_compiled_string : compiled_string { | ||
using char_type = Char; | ||
constexpr operator basic_string_view<char_type>() const { | ||
return {Str.data, N - 1}; | ||
} | ||
}; | ||
#endif | ||
|
||
template <typename T, typename... Tail> | ||
const T& first(const T& value, const Tail&...) { | ||
return value; | ||
|
@@ -698,6 +725,17 @@ size_t formatted_size(const CompiledFormat& cf, const Args&... args) { | |
return format_to(detail::counting_iterator(), cf, args...).count(); | ||
} | ||
|
||
#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> | ||
operator""_cf() { | ||
return {}; | ||
} | ||
} // namespace literals | ||
#endif | ||
|
||
FMT_END_NAMESPACE | ||
|
||
#endif // FMT_COMPILE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
fixed_string
actually needed? Can we directly use an array instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like yes, it's actually needed. The raw C-style array should be wrapped in some type, and this wrapper-type should also have a constructor with
const Char (&str)[N]
as an argument. Of course, I can be wrong here, but all my attempts ended up with nothing working.But I realized that the deduction guide for
fixed_string
can be removed now because it holds a null-terminated string.