-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add UDL as replacement for FMT_COMPILE #2043
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
Conversation
vitaut
left a comment
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.
Thanks for the PR! Mostly looks good, just a few nits inline.
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
{} seems unnecessary, please remove.
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.
It's either {} here or : data{} initialization in constructor for GCC 9.3, otherwise:
<source>:8:13: error: member 'fmt::v7::detail::fixed_string<char, 2>::data' must be initialized by mem-initializer in 'constexpr' constructor
<source>:12:8: note: declared here
12 | Char data[N];
| ^~~~
* `FMT_USE_NONTYPE_TEMPLATE_PARAMETERS` define added * `std::size_t` changed to `size_t`
df32877 to
855e488
Compare
|
I thought the plan was to just make compile-time checks the default in c++20? |
|
@yeswalrus I think you are confusing |
|
Correct, my mistake. |
include/fmt/compile.h
Outdated
| #if !defined(FMT_USE_NONTYPE_TEMPLATE_PARAMETERS) && \ | ||
| defined(__cpp_nontype_template_parameter_class) && \ | ||
| (!FMT_GCC_VERSION || FMT_GCC_VERSION >= 903) | ||
| # define FMT_USE_NONTYPE_TEMPLATE_PARAMETERS | ||
| #endif |
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.
Please make FMT_USE_NONTYPE_TEMPLATE_PARAMETERS defined to 1/0 for consistency with other similar macros and to make it possible to disable the feature. See e.g.
Lines 178 to 185 in 33f9a6d
| #ifndef FMT_USE_INLINE_NAMESPACES | |
| # if FMT_HAS_FEATURE(cxx_inline_namespaces) || FMT_GCC_VERSION >= 404 || \ | |
| (FMT_MSC_VER >= 1900 && !_MANAGED) | |
| # define FMT_USE_INLINE_NAMESPACES 1 | |
| # else | |
| # define FMT_USE_INLINE_NAMESPACES 0 | |
| # endif | |
| #endif |
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.
Done
| #define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::detail::compiled_string) | ||
|
|
||
| #ifdef FMT_USE_NONTYPE_TEMPLATE_PARAMETERS | ||
| template <typename Char, size_t N> struct fixed_string { |
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.
|
Merged, thanks. |
Right now {fmt} has a special macro to invoke compile-time API functions -
FMT_COMPILE. This macro can be replaced with UDL for modern compilers that support C++20. The chosen name in this PR for this feature is_cf.Note that only GCC 9.3+ support that. Clang (trunk) is also able to compile this code, but it doesn't have
__cpp_nontype_template_parameter_classdefined, I hope it'll be fixed in Clang 12 release.Proof on Compiler Explorer
constexpris used instead ofconsteval(even though it would make more sense) to lower GCC version.Also, I should note that this PR is based on my own ideas and the following comments: #1874 (comment), #613 (comment).