Don't reference std::char_traits<wchar_t> when !GTEST_HAS_STD_WSTRING

If the standard library doesn't support wide character strings, `std::char_traits<Char>` won't be defined, so the definition of `PrintTo` shouldn't reference it. Use `wstrlen` directly instead.

PiperOrigin-RevId: 964168903
Change-Id: Ic628e6583679073b47bb1218dcf52b4c9eadaaf6
This commit is contained in:
Abseil Team
2026-08-13 10:39:39 -07:00
committed by Copybara-Service
parent 002d14757f
commit aa1eefe02c
2 changed files with 28 additions and 8 deletions
+4 -7
View File
@@ -1173,15 +1173,12 @@ class [[nodiscard]] UniversalTersePrinter<const wchar_t*> {
}
}
};
#endif
template <>
class [[nodiscard]] UniversalTersePrinter<wchar_t*> {
public:
static void Print(wchar_t* str, ::std::ostream* os) {
UniversalTersePrinter<const wchar_t*>::Print(str, os);
}
};
class [[nodiscard]] UniversalTersePrinter<wchar_t*>
: public UniversalTersePrinter<const wchar_t*> {};
#endif // GTEST_HAS_STD_WSTRING
template <typename T>
void UniversalTersePrint(const T& value, ::std::ostream* os) {
+24 -1
View File
@@ -50,6 +50,7 @@
#include <iomanip>
#include <ios>
#include <ostream> // NOLINT
#include <string>
#include <string_view>
#include <type_traits>
@@ -422,6 +423,28 @@ void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) {
namespace {
template <typename Char>
size_t GetLength(const Char* s) {
return std::char_traits<Char>::length(s);
}
#if !GTEST_HAS_STD_WSTRING
// If GTEST_HAS_STD_WSTRING is unset because the standard library has disabled
// wide character support, std::char_traits<wchar_t> won't be defined, which
// will cause a compile error, even if user code never actually could print a
// wide cstring. In that case, instead use `wcslen` directly.
//
// If `libc` _also_ lacks wide character support, this (and a bunch of other
// calls to wc functions) will fail to link, but only if user code actually
// uses them.
template <>
size_t GetLength<wchar_t>(const wchar_t* s) {
return wcslen(s);
}
#endif // GTEST_HAS_STD_WSTRING
// Prints a null-terminated C-style string to the ostream.
template <typename Char>
void PrintCStringTo(const Char* s, ostream* os) {
@@ -429,7 +452,7 @@ void PrintCStringTo(const Char* s, ostream* os) {
*os << "NULL";
} else {
*os << ImplicitCast_<const void*>(s) << " pointing to ";
PrintCharsAsStringTo(s, std::char_traits<Char>::length(s), os);
PrintCharsAsStringTo(s, GetLength(s), os);
}
}