Skip to content

Commit b459f09

Browse files
committed
Add inspecions for literals stored in variant
1 parent 921ed9f commit b459f09

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
@@ -785,6 +785,31 @@ constexpr auto as( std::variant<Ts...> const& x ) {
785785
throw std::bad_variant_access();
786786
}
787787

788+
template< auto value, typename... Ts >
789+
constexpr auto is_impl( std::variant<Ts...> const& v ) -> bool {
790+
if (v.valueless_by_exception()) return false;
791+
// Need to guard this with is_any otherwise the holds_alternative is illegal
792+
if constexpr (is_any<std::monostate, Ts...>) if (std::holds_alternative<std::monostate>(v)) return false;
793+
794+
return std::visit([](auto&& arg) -> bool {
795+
return cpp2::is<value>(CPP2_FORWARD(arg));
796+
}, v);
797+
}
798+
799+
template< auto value, typename... Ts >
800+
constexpr auto is( std::variant<Ts...> const& v ) -> bool {
801+
return cpp2::is_impl<value>(v);
802+
}
803+
804+
template< cstring_wrapper value, typename... Ts >
805+
constexpr auto is( std::variant<Ts...> const& v ) -> bool {
806+
return cpp2::is_impl<value>(v);
807+
}
808+
809+
template< double_wrapper value, typename... Ts >
810+
constexpr auto is( std::variant<Ts...> const& v ) -> bool {
811+
return cpp2::is_impl<value>(v);
812+
}
788813

789814
//-------------------------------------------------------------------------------------------------------------
790815
// std::any is and as

0 commit comments

Comments
 (0)