Skip to content

Commit 8ece7d4

Browse files
omghantejuanarbol
authored andcommitted
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. PR-URL: #62507 Refs: #62506 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 9e1a0bc commit 8ece7d4

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

‎src/util.h‎

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

392+
// A type that has a valid std::char_traits specialization, as required by
393+
// std::basic_string and std::basic_string_view.
394+
template <typename T>
395+
concept standard_char_type =
396+
std::is_same_v<T, char> || std::is_same_v<T, wchar_t> ||
397+
std::is_same_v<T, char8_t> || std::is_same_v<T, char16_t> ||
398+
std::is_same_v<T, char32_t>;
399+
392400
// Allocates an array of member type T. For up to kStackStorageSize items,
393401
// the stack is used, otherwise malloc().
394402
template <typename T, size_t kStackStorageSize = 1024>
@@ -502,8 +510,12 @@ class MaybeStackBuffer {
502510
free(buf_);
503511
}
504512

505-
inline std::basic_string<T> ToString() const { return {out(), length()}; }
506-
inline std::basic_string_view<T> ToStringView() const {
513+
template <standard_char_type U = T>
514+
inline std::basic_string<U> ToString() const {
515+
return {out(), length()};
516+
}
517+
template <standard_char_type U = T>
518+
inline std::basic_string_view<U> ToStringView() const {
507519
return {out(), length()};
508520
}
509521

0 commit comments

Comments
 (0)