Skip to content

Commit 466d5db

Browse files
committed
Add inspecions for literals stored in variant
1 parent 04e7701 commit 466d5db

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
@@ -746,6 +746,31 @@ auto as( std::variant<Ts...> const& x ) {
746746
throw std::bad_variant_access();
747747
}
748748

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

750775
//-------------------------------------------------------------------------------------------------------------
751776
// std::any is and as

0 commit comments

Comments
 (0)