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

Made basic_string_view's const Char* constructor constexpr in C++17. #2525

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 9 additions & 3 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,17 @@ template <typename Char> class basic_string_view {
*/
FMT_CONSTEXPR_CHAR_TRAITS
FMT_INLINE
basic_string_view(const Char* s) : data_(s) {
basic_string_view(const Char* s) : data_(s), size_(0) {
// This test is needed to ensure that this constructor is constexpr in C++17
// modes.
#ifdef __cpp_lib_is_constant_evaluated
Copy link
Contributor

Choose a reason for hiding this comment

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

__cpp_lib_is_constant_evaluated is defined only in C++20. Therefore, code generation optimizations will not work until C++20.

if (detail::const_check(std::is_same<Char, char>::value &&
!detail::is_constant_evaluated()))
!detail::is_constant_evaluated())) {
// Call strlen directly for better code generation in non-optimized
// builds.
size_ = std::strlen(reinterpret_cast<const char*>(s));
else
} else
#endif
size_ = std::char_traits<Char>::length(s);
}

Expand Down