Skip to content

Commit 43d1e72

Browse files
committed
Add inspecions for literals stored in variant
1 parent 50b7932 commit 43d1e72

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

include/cpp2util.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,31 @@ auto as( std::variant<Ts...> const& x ) {
750750
throw std::bad_variant_access();
751751
}
752752

753+
template< auto value, typename... Ts >
754+
constexpr auto is_impl( std::variant<Ts...> const& v ) -> bool {
755+
if (v.valueless_by_exception()) return false;
756+
// Need to guard this with is_any otherwise the holds_alternative is illegal
757+
if constexpr (is_any<std::monostate, Ts...>) if (std::holds_alternative<std::monostate>(v)) return false;
758+
759+
return std::visit([](auto&& arg) -> bool {
760+
return cpp2::is<value>(CPP2_FORWARD(arg));
761+
}, v);
762+
}
763+
764+
template< auto value, typename... Ts >
765+
constexpr auto is( std::variant<Ts...> const& v ) -> bool {
766+
return cpp2::is_impl<value>(v);
767+
}
768+
769+
template< cstring_wrapper value, typename... Ts >
770+
constexpr auto is( std::variant<Ts...> const& v ) -> bool {
771+
return cpp2::is_impl<value>(v);
772+
}
773+
774+
template< double_wrapper value, typename... Ts >
775+
constexpr auto is( std::variant<Ts...> const& v ) -> bool {
776+
return cpp2::is_impl<value>(v);
777+
}
753778

754779
//-------------------------------------------------------------------------------------------------------------
755780
// std::any is and as

0 commit comments

Comments
 (0)