Skip to content

Commit 7c55623

Browse files
committed
Add inspecions for literals stored in variant
1 parent b7668e9 commit 7c55623

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
@@ -813,6 +813,31 @@ auto as( std::variant<Ts...> const& x ) {
813813
throw std::bad_variant_access();
814814
}
815815

816+
template< auto value, typename... Ts >
817+
constexpr auto is_impl( std::variant<Ts...> const& v ) -> bool {
818+
if (v.valueless_by_exception()) return false;
819+
// Need to guard this with is_any otherwise the holds_alternative is illegal
820+
if constexpr (is_any<std::monostate, Ts...>) if (std::holds_alternative<std::monostate>(v)) return false;
821+
822+
return std::visit([](auto&& arg) -> bool {
823+
return cpp2::is<value>(CPP2_FORWARD(arg));
824+
}, v);
825+
}
826+
827+
template< auto value, typename... Ts >
828+
constexpr auto is( std::variant<Ts...> const& v ) -> bool {
829+
return cpp2::is_impl<value>(v);
830+
}
831+
832+
template< cstring_wrapper value, typename... Ts >
833+
constexpr auto is( std::variant<Ts...> const& v ) -> bool {
834+
return cpp2::is_impl<value>(v);
835+
}
836+
837+
template< double_wrapper value, typename... Ts >
838+
constexpr auto is( std::variant<Ts...> const& v ) -> bool {
839+
return cpp2::is_impl<value>(v);
840+
}
816841

817842
//-------------------------------------------------------------------------------------------------------------
818843
// std::any is and as

0 commit comments

Comments
 (0)