@@ -661,6 +661,9 @@ inline constexpr auto is_any = std::disjunction_v<std::is_same<T, Ts>...>;
661
661
template <typename T, typename ... Types>
662
662
inline constexpr std::ptrdiff_t count_t = ( std::ptrdiff_t {std::is_same_v<T, Types>} + ... );
663
663
664
+ template <typename ... Types>
665
+ inline constexpr bool ill_formed_variant = sizeof ...(Types) < 0 ;
666
+
664
667
template <typename T, typename ... Ts>
665
668
constexpr auto is ( std::variant<Ts...> const & x ) {
666
669
if constexpr (std::is_same_v< T, empty >) {
@@ -671,7 +674,10 @@ constexpr auto is( std::variant<Ts...> const& x ) {
671
674
}
672
675
} else {
673
676
// std::holds_alternative is ill-formed if T does not appear exactly once in Ts
674
- if constexpr (count_t <T, Ts...> == 1 ) {
677
+ if constexpr (count_t <T, Ts...> > 1 ) {
678
+ static_assert (ill_formed_variant<T,Ts...>, " Checking variant is ill-formed - T appears more then once" );
679
+ }
680
+ else if constexpr (count_t <T, Ts...> == 1 ) {
675
681
return std::holds_alternative<T>(x);
676
682
}
677
683
return false ;
@@ -681,7 +687,10 @@ constexpr auto is( std::variant<Ts...> const& x ) {
681
687
template <typename T, typename ... Ts>
682
688
constexpr auto as ( std::variant<Ts...> const & x ) {
683
689
// std::holds_alternative is ill-formed if T does not appear exactly once in Ts
684
- if constexpr (count_t <T, Ts...> == 1 ) {
690
+ if constexpr (count_t <T, Ts...> > 1 ) {
691
+ static_assert (ill_formed_variant<T,Ts...>, " Casting variant is ill-formed - T appears more then once" );
692
+ }
693
+ else if constexpr (count_t <T, Ts...> == 1 ) {
685
694
if (std::holds_alternative<T>(x))
686
695
return std::get<T>(x);
687
696
}
0 commit comments