Skip to content

Commit b42db3d

Browse files
committed
src: constrain MaybeStackBuffer::ToString and ToStringView to standard char types
On newer libc++ (shipped with macOS Xcode 16+), std::char_traits<T> for T not equal to char, wchar_t, char8_t, char16_t, or char32_t is deprecated and will be removed in a future release. When MaybeStackBuffer is instantiated with unsigned char or uint8_t (e.g. in test/cctest/test_util.cc), the ToString() and ToStringView() methods trigger this deprecation warning because they reference std::basic_string<unsigned char> and std::basic_string_view<unsigned char>, even though these methods are never actually called for those types. Add C++20 requires clauses to constrain ToString() and ToStringView() to only be available for standard character types, preventing the deprecated std::char_traits<unsigned char> from being instantiated. This is consistent with the existing use of C++20 concepts elsewhere in the same file (e.g. the is_callable concept).
1 parent d9645d7 commit b42db3d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/util.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,15 @@ constexpr size_t strsize(const T (&)[N]) {
390390
return N - 1;
391391
}
392392

393+
// A type that has a valid std::char_traits specialization, as required by
394+
// std::basic_string and std::basic_string_view.
395+
template <typename T>
396+
concept standard_char_type = std::is_same_v<T, char> ||
397+
std::is_same_v<T, wchar_t> ||
398+
std::is_same_v<T, char8_t> ||
399+
std::is_same_v<T, char16_t> ||
400+
std::is_same_v<T, char32_t>;
401+
393402
// Allocates an array of member type T. For up to kStackStorageSize items,
394403
// the stack is used, otherwise malloc().
395404
template <typename T, size_t kStackStorageSize = 1024>
@@ -503,8 +512,12 @@ class MaybeStackBuffer {
503512
free(buf_);
504513
}
505514

506-
inline std::basic_string<T> ToString() const { return {out(), length()}; }
507-
inline std::basic_string_view<T> ToStringView() const {
515+
inline std::basic_string<T> ToString() const
516+
requires standard_char_type<T> {
517+
return {out(), length()};
518+
}
519+
inline std::basic_string_view<T> ToStringView() const
520+
requires standard_char_type<T> {
508521
return {out(), length()};
509522
}
510523
// This can only be used if the buffer contains path data in UTF8

0 commit comments

Comments
 (0)