summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2022-12-02 08:58:00 -0800
committerGravatar Copybara-Service <copybara-worker@google.com>2022-12-02 08:58:36 -0800
commitafcb7de0cfe94ad2ec95dfd0380f74a4b972401c (patch)
tree463f2a6ccbfbe3fead74c7d1f48e815e00d4501f
parent7cbdff8c13503561a166438e45bc37c5c5d0066b (diff)
Add AbslStringify support for enum types in Substitute.
PiperOrigin-RevId: 492481345 Change-Id: Ie77656ed334b54930ee852d31e2794a1fc58ce2f
-rw-r--r--absl/strings/substitute.h3
-rw-r--r--absl/strings/substitute_test.cc12
2 files changed, 14 insertions, 1 deletions
diff --git a/absl/strings/substitute.h b/absl/strings/substitute.h
index 5c3f6eff..d6a5a690 100644
--- a/absl/strings/substitute.h
+++ b/absl/strings/substitute.h
@@ -203,7 +203,8 @@ class Arg {
// This overload matches only scoped enums.
template <typename T,
typename = typename std::enable_if<
- std::is_enum<T>{} && !std::is_convertible<T, int>{}>::type>
+ std::is_enum<T>{} && !std::is_convertible<T, int>{} &&
+ !strings_internal::HasAbslStringify<T>::value>::type>
Arg(T value) // NOLINT(google-explicit-constructor)
: Arg(static_cast<typename std::underlying_type<T>::type>(value)) {}
diff --git a/absl/strings/substitute_test.cc b/absl/strings/substitute_test.cc
index 9f04545f..9cb37c39 100644
--- a/absl/strings/substitute_test.cc
+++ b/absl/strings/substitute_test.cc
@@ -253,6 +253,18 @@ TEST(SubstituteTest, Enums) {
ScopedEnumUInt16::kEnum1));
}
+enum class EnumWithStringify { Many = 0, Choices = 1 };
+
+template <typename Sink>
+void AbslStringify(Sink& sink, EnumWithStringify e) {
+ sink.Append(e == EnumWithStringify::Many ? "Many" : "Choices");
+}
+
+TEST(SubstituteTest, AbslStringifyWithEnum) {
+ const auto e = EnumWithStringify::Choices;
+ EXPECT_EQ(absl::Substitute("$0", e), "Choices");
+}
+
#ifdef GTEST_HAS_DEATH_TEST
TEST(SubstituteDeathTest, SubstituteDeath) {