From f7404cd33f2457bd561fdcfbb024e43852c94bcf Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 28 Sep 2022 11:33:29 -0700 Subject: Allows absl::StrFormat to accept types which implement AbslStringify() PiperOrigin-RevId: 477507777 Change-Id: I5ecde3163ca560ac8774034e55654774e36ad230 --- absl/strings/internal/str_format/arg.h | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'absl/strings/internal/str_format/arg.h') diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index 2dfbf728..3d616dba 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -45,6 +45,11 @@ class FormatConversionSpec; namespace str_format_internal { +template +struct ArgConvertResult { + bool value; +}; + template struct HasUserDefinedConvert : std::false_type {}; @@ -71,6 +76,20 @@ auto FormatConvertImpl(const T& v, FormatConversionSpecImpl conv, return AbslFormatConvert(v, fcs, &fs); } +void AbslStringify(); // Stops the lexical name lookup +template +auto FormatConvertImpl(const T& v, FormatConversionSpecImpl, + FormatSinkImpl* sink) + -> std::enable_if_t(), v))>::value, + ArgConvertResult> { + using FormatSinkT = + absl::enable_if_t; + auto fs = sink->Wrap(); + AbslStringify(fs, v); + return {true}; +} + template class StreamedWrapper; @@ -95,11 +114,6 @@ struct VoidPtr { uintptr_t value; }; -template -struct ArgConvertResult { - bool value; -}; - template constexpr FormatConversionCharSet ExtractCharSet(FormatConvertResult) { return C; @@ -316,11 +330,11 @@ struct FormatArgImplFriend { template constexpr FormatConversionCharSet ArgumentToConv() { - return absl::str_format_internal::ExtractCharSet( - decltype(str_format_internal::FormatConvertImpl( - std::declval(), - std::declval(), - std::declval())){}); + using ConvResult = decltype(str_format_internal::FormatConvertImpl( + std::declval(), + std::declval(), + std::declval())); + return absl::str_format_internal::ExtractCharSet(ConvResult{}); } // A type-erased handle to a format argument. -- cgit v1.2.3