internal/log_message: Use if constexpr instead of SFINAE for operator<<

SFINAE is harder to read and it makes mangled names longer.

PiperOrigin-RevId: 729591470
Change-Id: I8d254fdd02b993580c7a4df8f477ef4fa11b6f76
This commit is contained in:
Dino Radakovic
2025-02-21 11:10:34 -08:00
committed by Copybara-Service
parent 398e24a0b1
commit 5f4f38e15c

View File

@@ -183,16 +183,9 @@ class LogMessage {
LogMessage& operator<<(char (&buf)[SIZE]) ABSL_ATTRIBUTE_NOINLINE;
// Types that support `AbslStringify()` are serialized that way.
template <typename T,
typename std::enable_if<absl::HasAbslStringify<T>::value,
int>::type = 0>
LogMessage& operator<<(const T& v) ABSL_ATTRIBUTE_NOINLINE;
// Types that don't support `AbslStringify()` but do support streaming into a
// `std::ostream&` are serialized that way.
template <typename T,
typename std::enable_if<!absl::HasAbslStringify<T>::value,
int>::type = 0>
template <typename T>
LogMessage& operator<<(const T& v) ABSL_ATTRIBUTE_NOINLINE;
// Note: We explicitly do not support `operator<<` for non-const references
@@ -308,21 +301,16 @@ class StringifySink final {
};
// Note: the following is declared `ABSL_ATTRIBUTE_NOINLINE`
template <typename T,
typename std::enable_if<absl::HasAbslStringify<T>::value, int>::type>
template <typename T>
LogMessage& LogMessage::operator<<(const T& v) {
StringifySink sink(*this);
// Replace with public API.
AbslStringify(sink, v);
return *this;
}
// Note: the following is declared `ABSL_ATTRIBUTE_NOINLINE`
template <typename T,
typename std::enable_if<!absl::HasAbslStringify<T>::value, int>::type>
LogMessage& LogMessage::operator<<(const T& v) {
OstreamView view(*data_);
view.stream() << log_internal::NullGuard<T>().Guard(v);
if constexpr (absl::HasAbslStringify<T>::value) {
StringifySink sink(*this);
// Replace with public API.
AbslStringify(sink, v);
} else {
OstreamView view(*data_);
view.stream() << log_internal::NullGuard<T>().Guard(v);
}
return *this;
}