Skip to content

Commit 9f3892d

Browse files
committed
src: fix MaybeStackBuffer char_traits deprecation warning
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 their return types reference std::basic_string<unsigned char> and std::basic_string_view<unsigned char>, even though these methods are never actually called for those types. Convert ToString() and ToStringView() into member function templates with a constrained default template parameter, so the return type is only instantiated when the function is actually called. Extract the type list into a reusable standard_char_type concept.
1 parent d9645d7 commit 9f3892d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/util.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@ 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 =
397+
std::is_same_v<T, char> || std::is_same_v<T, wchar_t> ||
398+
std::is_same_v<T, char8_t> || std::is_same_v<T, char16_t> ||
399+
std::is_same_v<T, char32_t>;
400+
393401
// Allocates an array of member type T. For up to kStackStorageSize items,
394402
// the stack is used, otherwise malloc().
395403
template <typename T, size_t kStackStorageSize = 1024>
@@ -503,8 +511,12 @@ class MaybeStackBuffer {
503511
free(buf_);
504512
}
505513

506-
inline std::basic_string<T> ToString() const { return {out(), length()}; }
507-
inline std::basic_string_view<T> ToStringView() const {
514+
template <standard_char_type U = T>
515+
inline std::basic_string<U> ToString() const {
516+
return {out(), length()};
517+
}
518+
template <standard_char_type U = T>
519+
inline std::basic_string_view<U> ToStringView() const {
508520
return {out(), length()};
509521
}
510522
// This can only be used if the buffer contains path data in UTF8

0 commit comments

Comments
 (0)