@@ -679,6 +679,9 @@ inline constexpr auto is_any = std::disjunction_v<std::is_same<T, Ts>...>;
679
679
template <typename T, typename ... Types>
680
680
inline constexpr std::ptrdiff_t count_t = ( std::ptrdiff_t {std::is_same_v<T, Types>} + ... );
681
681
682
+ template <typename ... Types>
683
+ inline constexpr bool ill_formed_variant = sizeof ...(Types) < 0 ;
684
+
682
685
template <typename T, typename ... Ts>
683
686
constexpr auto is ( std::variant<Ts...> const & x ) {
684
687
if constexpr (std::is_same_v< T, empty >) {
@@ -689,7 +692,10 @@ constexpr auto is( std::variant<Ts...> const& x ) {
689
692
}
690
693
} else {
691
694
// std::holds_alternative is ill-formed if T does not appear exactly once in Ts
692
- if constexpr (count_t <T, Ts...> == 1 ) {
695
+ if constexpr (count_t <T, Ts...> > 1 ) {
696
+ static_assert (ill_formed_variant<T,Ts...>, " Checking variant is ill-formed - T appears more then once" );
697
+ }
698
+ else if constexpr (count_t <T, Ts...> == 1 ) {
693
699
return std::holds_alternative<T>(x);
694
700
}
695
701
return false ;
@@ -699,7 +705,10 @@ constexpr auto is( std::variant<Ts...> const& x ) {
699
705
template <typename T, typename ... Ts>
700
706
constexpr auto as ( std::variant<Ts...> const & x ) {
701
707
// std::holds_alternative is ill-formed if T does not appear exactly once in Ts
702
- if constexpr (count_t <T, Ts...> == 1 ) {
708
+ if constexpr (count_t <T, Ts...> > 1 ) {
709
+ static_assert (ill_formed_variant<T,Ts...>, " Casting variant is ill-formed - T appears more then once" );
710
+ }
711
+ else if constexpr (count_t <T, Ts...> == 1 ) {
703
712
if (std::holds_alternative<T>(x))
704
713
return std::get<T>(x);
705
714
}
0 commit comments