diff options
author | Abseil Team <absl-team@google.com> | 2024-05-24 14:10:10 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-05-24 14:11:02 -0700 |
commit | 49c1f36ec50b3f812f9031d1cf35f1f514eb2a21 (patch) | |
tree | e2b813947b2519536c50c00992b84bc282b51ae1 | |
parent | f858e740085e4ec59c319bb1a4237b628bcad4a8 (diff) |
overload: make the constructor constexpr
PiperOrigin-RevId: 637029703
Change-Id: I58b4c3a3f1aab3062b51d15c5b9fecfd3ca3193a
-rw-r--r-- | absl/functional/overload.h | 2 | ||||
-rw-r--r-- | absl/functional/overload_test.cc | 14 |
2 files changed, 15 insertions, 1 deletions
diff --git a/absl/functional/overload.h b/absl/functional/overload.h index 34ffa689..7e19e705 100644 --- a/absl/functional/overload.h +++ b/absl/functional/overload.h @@ -62,7 +62,7 @@ struct Overload final : T... { // aggregate initialization. Before then we must provide a constructor that // makes this work. // - explicit Overload(T... ts) : T(std::move(ts))... {} + constexpr explicit Overload(T... ts) : T(std::move(ts))... {} }; // Before C++20, which added support for CTAD for aggregate types, we must also diff --git a/absl/functional/overload_test.cc b/absl/functional/overload_test.cc index 92f0eb9b..fa49d298 100644 --- a/absl/functional/overload_test.cc +++ b/absl/functional/overload_test.cc @@ -194,6 +194,20 @@ TEST(OverloadTest, UseWithParentheses) { EXPECT_EQ(5, absl::visit(overloaded, v)); } +TEST(OverloadTest, HasConstexprConstructor) { + constexpr auto overloaded = absl::Overload{ + [](int v) { return absl::StrCat("int ", v); }, + [](double v) { return absl::StrCat("double ", v); }, + [](const char* v) { return absl::StrCat("const char* ", v); }, + [](auto v) { return absl::StrCat("auto ", v); }, + }; + + EXPECT_EQ("int 1", overloaded(1)); + EXPECT_EQ("double 2.5", overloaded(2.5)); + EXPECT_EQ("const char* hello", overloaded("hello")); + EXPECT_EQ("auto 1.5", overloaded(1.5f)); +} + } // namespace #endif |