Skip to content

Commit 3da6d3d

Browse files
committed
Made basic_string_view's const Char* constructor constexpr in C++17.
Also added commentary for why std::strlen is directly called in certain situations. Addresses fmtlib#2455 (comment).
1 parent 927dbd1 commit 3da6d3d

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

include/fmt/core.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,17 @@ template <typename Char> class basic_string_view {
422422
*/
423423
FMT_CONSTEXPR_CHAR_TRAITS
424424
FMT_INLINE
425-
basic_string_view(const Char* s) : data_(s) {
425+
basic_string_view(const Char* s) : data_(s), size_(0) {
426+
// This test is needed to ensure that this constructor is constexpr in C++17
427+
// modes.
428+
#ifdef __cpp_lib_is_constant_evaluated
426429
if (detail::const_check(std::is_same<Char, char>::value &&
427-
!detail::is_constant_evaluated()))
430+
!detail::is_constant_evaluated())) {
431+
// Call strlen directly for better code generation in non-optimized
432+
// builds.
428433
size_ = std::strlen(reinterpret_cast<const char*>(s));
429-
else
434+
} else
435+
#endif
430436
size_ = std::char_traits<Char>::length(s);
431437
}
432438

0 commit comments

Comments
 (0)