Commit 49c1f36e by Abseil Team Committed by Copybara-Service

overload: make the constructor constexpr

PiperOrigin-RevId: 637029703
Change-Id: I58b4c3a3f1aab3062b51d15c5b9fecfd3ca3193a
parent f858e740
...@@ -62,7 +62,7 @@ struct Overload final : T... { ...@@ -62,7 +62,7 @@ struct Overload final : T... {
// aggregate initialization. Before then we must provide a constructor that // aggregate initialization. Before then we must provide a constructor that
// makes this work. // 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 // Before C++20, which added support for CTAD for aggregate types, we must also
......
...@@ -194,6 +194,20 @@ TEST(OverloadTest, UseWithParentheses) { ...@@ -194,6 +194,20 @@ TEST(OverloadTest, UseWithParentheses) {
EXPECT_EQ(5, absl::visit(overloaded, v)); 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 } // namespace
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment