From a18e18394df62350eb9914fae7af2ea885189165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20G=C3=BCndling?= Date: Thu, 4 Apr 2024 17:31:58 +0200 Subject: [PATCH] variant: support std::visit via apply --- include/cista/containers/variant.h | 5 +++++ test/variant_test.cc | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/cista/containers/variant.h b/include/cista/containers/variant.h index 9c4c2a4b..57b13020 100755 --- a/include/cista/containers/variant.h +++ b/include/cista/containers/variant.h @@ -438,6 +438,11 @@ using cista::variant; namespace std { +template +constexpr auto visit(Visitor&& vis, cista::variant&& v) { + v.apply(vis); +} + using cista::get; } // namespace std diff --git a/test/variant_test.cc b/test/variant_test.cc index 631a54e9..570503b9 100755 --- a/test/variant_test.cc +++ b/test/variant_test.cc @@ -241,3 +241,15 @@ TEST_CASE("variant get_if") { REQUIRE(str != nullptr); CHECK(*str == "hello test test test test"); } + +TEST_CASE("std visit") { + std::visit( + [](auto&& x) { + if constexpr (std::is_same_v>) { + CHECK(true); + } else { + CHECK(false); + } + }, + cista::variant{1}); +}