Skip to content

Commit

Permalink
Made basic_string_view's const Char* constructor constexpr in C++17.
Browse files Browse the repository at this point in the history
Also added commentary for why std::strlen is directly called in certain situations.
Addresses fmtlib#2455 (comment).
  • Loading branch information
mwinterb committed Sep 30, 2021
1 parent 927dbd1 commit 3da6d3d
Showing 1 changed file with 9 additions and 3 deletions.
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
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

0 comments on commit 3da6d3d

Please sign in to comment.