@@ -568,7 +568,10 @@ class out {
568
568
569
569
// For use when returning "no such thing", such as
570
570
// when customizing is/as for std::variant
571
- static std::nullptr_t nonesuch = nullptr ;
571
+ struct nonesuch_ {
572
+ auto operator ==(auto const &) -> bool { return false ; }
573
+ };
574
+ static nonesuch_ nonesuch;
572
575
573
576
// For designating "holds no value" -- used only with is, not as
574
577
// TODO: Does this really warrant a new synonym? Perhaps "is void" is enough
@@ -619,25 +622,18 @@ auto is( X const& x ) -> bool {
619
622
// -------------------------------------------------------------------------------------------------------------
620
623
// Built-in is (values)
621
624
//
622
-
623
625
inline constexpr auto is ( auto const & x, auto const & value ) -> bool
624
626
requires requires{ x == value; }
625
627
{
626
628
return x == value;
627
629
}
628
630
629
- inline constexpr auto is ( auto const & x, auto const & value ) -> bool
630
- requires (!requires{ x == value; })
631
- {
632
- return false ;
633
- }
634
-
635
631
636
632
// -------------------------------------------------------------------------------------------------------------
637
633
// Built-in as (partial)
638
634
//
639
635
template < typename C >
640
- auto as (... ) -> auto {
636
+ auto as (auto const & ) -> auto {
641
637
return nonesuch;
642
638
}
643
639
@@ -693,6 +689,32 @@ constexpr auto operator_is( std::variant<Ts...> const& x ) {
693
689
return x.index ();
694
690
}
695
691
692
+ template <typename ... Ts>
693
+ constexpr auto is ( std::variant<Ts...> const & x, auto const & value ) -> bool
694
+ {
695
+ if constexpr (requires{ operator_as< 0 >(x) == value; }) if (x.index () == 0 ) return operator_as< 0 >(x) == value;;
696
+ if constexpr (requires{ operator_as< 1 >(x) == value; }) if (x.index () == 1 ) return operator_as< 1 >(x) == value;;
697
+ if constexpr (requires{ operator_as< 2 >(x) == value; }) if (x.index () == 2 ) return operator_as< 2 >(x) == value;;
698
+ if constexpr (requires{ operator_as< 3 >(x) == value; }) if (x.index () == 3 ) return operator_as< 3 >(x) == value;;
699
+ if constexpr (requires{ operator_as< 4 >(x) == value; }) if (x.index () == 4 ) return operator_as< 4 >(x) == value;;
700
+ if constexpr (requires{ operator_as< 5 >(x) == value; }) if (x.index () == 5 ) return operator_as< 5 >(x) == value;;
701
+ if constexpr (requires{ operator_as< 6 >(x) == value; }) if (x.index () == 6 ) return operator_as< 6 >(x) == value;;
702
+ if constexpr (requires{ operator_as< 7 >(x) == value; }) if (x.index () == 7 ) return operator_as< 7 >(x) == value;;
703
+ if constexpr (requires{ operator_as< 8 >(x) == value; }) if (x.index () == 8 ) return operator_as< 8 >(x) == value;;
704
+ if constexpr (requires{ operator_as< 9 >(x) == value; }) if (x.index () == 9 ) return operator_as< 9 >(x) == value;;
705
+ if constexpr (requires{ operator_as<10 >(x) == value; }) if (x.index () == 10 ) return operator_as<10 >(x) == value;;
706
+ if constexpr (requires{ operator_as<11 >(x) == value; }) if (x.index () == 11 ) return operator_as<11 >(x) == value;;
707
+ if constexpr (requires{ operator_as<12 >(x) == value; }) if (x.index () == 12 ) return operator_as<12 >(x) == value;;
708
+ if constexpr (requires{ operator_as<13 >(x) == value; }) if (x.index () == 13 ) return operator_as<13 >(x) == value;;
709
+ if constexpr (requires{ operator_as<14 >(x) == value; }) if (x.index () == 14 ) return operator_as<14 >(x) == value;;
710
+ if constexpr (requires{ operator_as<15 >(x) == value; }) if (x.index () == 15 ) return operator_as<15 >(x) == value;;
711
+ if constexpr (requires{ operator_as<16 >(x) == value; }) if (x.index () == 16 ) return operator_as<16 >(x) == value;;
712
+ if constexpr (requires{ operator_as<17 >(x) == value; }) if (x.index () == 17 ) return operator_as<17 >(x) == value;;
713
+ if constexpr (requires{ operator_as<18 >(x) == value; }) if (x.index () == 18 ) return operator_as<18 >(x) == value;;
714
+ if constexpr (requires{ operator_as<19 >(x) == value; }) if (x.index () == 19 ) return operator_as<19 >(x) == value;;
715
+ return false ;
716
+ }
717
+
696
718
template <size_t I, typename ... Ts>
697
719
constexpr auto operator_as ( std::variant<Ts...> const & x ) -> auto&& {
698
720
if constexpr (I < std::variant_size_v<std::variant<Ts...>>) {
@@ -709,16 +731,26 @@ inline constexpr auto is_any = std::disjunction_v<std::is_same<T, Ts>...>;
709
731
710
732
template <typename T, typename ... Ts>
711
733
auto is ( std::variant<Ts...> const & x ) {
712
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<0 >(x)), T >) if (x.index () == 0 ) return true ;
713
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<1 >(x)), T >) if (x.index () == 1 ) return true ;
714
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<2 >(x)), T >) if (x.index () == 2 ) return true ;
715
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<3 >(x)), T >) if (x.index () == 3 ) return true ;
716
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<4 >(x)), T >) if (x.index () == 4 ) return true ;
717
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<5 >(x)), T >) if (x.index () == 5 ) return true ;
718
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<6 >(x)), T >) if (x.index () == 6 ) return true ;
719
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<7 >(x)), T >) if (x.index () == 7 ) return true ;
720
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<8 >(x)), T >) if (x.index () == 8 ) return true ;
721
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<9 >(x)), T >) if (x.index () == 9 ) return true ;
734
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 0 >(x)), T >) if (x.index () == 0 ) return true ;
735
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 1 >(x)), T >) if (x.index () == 1 ) return true ;
736
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 2 >(x)), T >) if (x.index () == 2 ) return true ;
737
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 3 >(x)), T >) if (x.index () == 3 ) return true ;
738
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 4 >(x)), T >) if (x.index () == 4 ) return true ;
739
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 5 >(x)), T >) if (x.index () == 5 ) return true ;
740
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 6 >(x)), T >) if (x.index () == 6 ) return true ;
741
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 7 >(x)), T >) if (x.index () == 7 ) return true ;
742
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 8 >(x)), T >) if (x.index () == 8 ) return true ;
743
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 9 >(x)), T >) if (x.index () == 9 ) return true ;
744
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<10 >(x)), T >) if (x.index () == 10 ) return true ;
745
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<11 >(x)), T >) if (x.index () == 11 ) return true ;
746
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<12 >(x)), T >) if (x.index () == 12 ) return true ;
747
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<13 >(x)), T >) if (x.index () == 13 ) return true ;
748
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<14 >(x)), T >) if (x.index () == 14 ) return true ;
749
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<15 >(x)), T >) if (x.index () == 15 ) return true ;
750
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<16 >(x)), T >) if (x.index () == 16 ) return true ;
751
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<17 >(x)), T >) if (x.index () == 17 ) return true ;
752
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<18 >(x)), T >) if (x.index () == 18 ) return true ;
753
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<19 >(x)), T >) if (x.index () == 19 ) return true ;
722
754
if constexpr (std::is_same_v< T, empty > ) {
723
755
if (x.valueless_by_exception ()) return true ;
724
756
// Need to guard this with is_any otherwise the get_if is illegal
@@ -729,16 +761,26 @@ auto is( std::variant<Ts...> const& x ) {
729
761
730
762
template <typename T, typename ... Ts>
731
763
auto as ( std::variant<Ts...> const & x ) {
732
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<0 >(x)), T >) if (x.index () == 0 ) return operator_as<0 >(x);
733
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<1 >(x)), T >) if (x.index () == 1 ) return operator_as<1 >(x);
734
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<2 >(x)), T >) if (x.index () == 2 ) return operator_as<2 >(x);
735
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<3 >(x)), T >) if (x.index () == 3 ) return operator_as<3 >(x);
736
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<4 >(x)), T >) if (x.index () == 4 ) return operator_as<4 >(x);
737
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<5 >(x)), T >) if (x.index () == 5 ) return operator_as<5 >(x);
738
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<6 >(x)), T >) if (x.index () == 6 ) return operator_as<6 >(x);
739
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<7 >(x)), T >) if (x.index () == 7 ) return operator_as<7 >(x);
740
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<8 >(x)), T >) if (x.index () == 8 ) return operator_as<8 >(x);
741
- if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<9 >(x)), T >) if (x.index () == 9 ) return operator_as<9 >(x);
764
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 0 >(x)), T >) if (x.index () == 0 ) return operator_as<0 >(x);
765
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 1 >(x)), T >) if (x.index () == 1 ) return operator_as<1 >(x);
766
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 2 >(x)), T >) if (x.index () == 2 ) return operator_as<2 >(x);
767
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 3 >(x)), T >) if (x.index () == 3 ) return operator_as<3 >(x);
768
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 4 >(x)), T >) if (x.index () == 4 ) return operator_as<4 >(x);
769
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 5 >(x)), T >) if (x.index () == 5 ) return operator_as<5 >(x);
770
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 6 >(x)), T >) if (x.index () == 6 ) return operator_as<6 >(x);
771
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 7 >(x)), T >) if (x.index () == 7 ) return operator_as<7 >(x);
772
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 8 >(x)), T >) if (x.index () == 8 ) return operator_as<8 >(x);
773
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as< 9 >(x)), T >) if (x.index () == 9 ) return operator_as<9 >(x);
774
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<10 >(x)), T >) if (x.index () == 10 ) return operator_as<0 >(x);
775
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<11 >(x)), T >) if (x.index () == 11 ) return operator_as<1 >(x);
776
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<12 >(x)), T >) if (x.index () == 12 ) return operator_as<2 >(x);
777
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<13 >(x)), T >) if (x.index () == 13 ) return operator_as<3 >(x);
778
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<14 >(x)), T >) if (x.index () == 14 ) return operator_as<4 >(x);
779
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<15 >(x)), T >) if (x.index () == 15 ) return operator_as<5 >(x);
780
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<16 >(x)), T >) if (x.index () == 16 ) return operator_as<6 >(x);
781
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<17 >(x)), T >) if (x.index () == 17 ) return operator_as<7 >(x);
782
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<18 >(x)), T >) if (x.index () == 18 ) return operator_as<8 >(x);
783
+ if constexpr (std::is_same_v< CPP2_TYPEOF (operator_as<19 >(x)), T >) if (x.index () == 19 ) return operator_as<9 >(x);
742
784
throw std::bad_variant_access ();
743
785
}
744
786
@@ -756,6 +798,12 @@ template<typename T, typename X>
756
798
constexpr auto is ( X const & x ) -> bool
757
799
{ return !x.has_value (); }
758
800
801
+ constexpr auto is ( std::any const & x, auto const & value ) -> bool
802
+ {
803
+ auto pvalue = std::any_cast<CPP2_TYPEOF (value)>(&x);
804
+ return pvalue && *pvalue == value;
805
+ }
806
+
759
807
template <typename T, typename X>
760
808
requires (!std::is_reference_v<T> && std::is_same_v<X,std::any> && !std::is_same_v<T,std::any>)
761
809
constexpr auto as ( X const & x ) -> T
@@ -775,6 +823,20 @@ template<typename T, typename U>
775
823
constexpr auto is ( std::optional<U> const & x ) -> bool
776
824
{ return !x.has_value (); }
777
825
826
+ template <typename T>
827
+ constexpr auto is ( std::optional<T> const & x, auto const & value ) -> bool
828
+ requires requires{ x.value () == value; }
829
+ {
830
+ return x.has_value () && x.value () == value;
831
+ }
832
+
833
+ template <typename T>
834
+ constexpr auto is ( std::optional<T> const & x, auto const & value ) -> bool
835
+ requires (!requires{ x.value () == value; })
836
+ {
837
+ return false ;
838
+ }
839
+
778
840
template <typename T, typename X>
779
841
requires std::is_same_v<X,std::optional<T>>
780
842
constexpr auto as ( X const & x ) -> auto&&
@@ -839,13 +901,22 @@ inline auto to_string(...) -> std::string {
839
901
return " (customize me - no cpp2::to_string overload exists for this type)" ;
840
902
}
841
903
904
+ inline auto to_string (std::any const &) -> std::string {
905
+ return " std::any" ;
906
+ }
907
+
842
908
template <typename T>
843
909
inline auto to_string (T const & t) -> std::string
844
910
requires requires { std::to_string (t); }
845
911
{
846
912
return std::to_string (t);
847
913
}
848
914
915
+ inline auto to_string (char const * s) -> std::string
916
+ {
917
+ return std::string{s};
918
+ }
919
+
849
920
inline auto to_string (std::string const & s) -> std::string const &
850
921
{
851
922
return s;
0 commit comments