Skip to content

Commit

Permalink
variant: support std::visit via apply
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling committed Apr 4, 2024
1 parent 251a73b commit a18e183
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/cista/containers/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ using cista::variant;

namespace std {

template <typename Visitor, typename... T>
constexpr auto visit(Visitor&& vis, cista::variant<T...>&& v) {
v.apply(vis);
}

using cista::get;

} // namespace std
12 changes: 12 additions & 0 deletions test/variant_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, std::decay_t<decltype(x)>>) {
CHECK(true);
} else {
CHECK(false);
}
},
cista::variant<int, float>{1});
}

0 comments on commit a18e183

Please sign in to comment.