@@ -2045,28 +2045,22 @@ auto as( X&& x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT_AS) -> decltype(auto)
2045
2045
2046
2046
// is Type
2047
2047
//
2048
- template <typename T, typename X>
2049
- requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
2050
- constexpr auto is ( X const & x ) -> bool
2051
- { return x.type () == Typeid<T>(); }
2052
-
2053
- template <typename T, typename X>
2054
- requires (std::is_same_v<X,std::any> && std::is_same_v<T,empty>)
2055
- constexpr auto is ( X const & x ) -> bool
2056
- { return !x.has_value (); }
2057
-
2048
+ template <typename T, std::same_as<std::any> X>
2049
+ constexpr auto is ( X const & x ) -> bool{
2050
+ if (!x.has_value ()) {
2051
+ return std::is_same_v<T,empty>;
2052
+ }
2053
+ return x.type () == Typeid<T>();
2054
+ }
2058
2055
2059
2056
// is Value
2060
2057
//
2061
2058
inline constexpr auto is ( std::any const & x, auto && value ) -> bool
2062
2059
{
2063
2060
// Predicate case
2064
- if constexpr (requires{ bool { value (x) }; } ) {
2061
+ if constexpr (valid_predicate< decltype ( value), decltype (x)> ) {
2065
2062
return value (x);
2066
2063
}
2067
- else if constexpr (std::is_function_v<decltype (value)> || requires{ &value.operator (); }) {
2068
- return false ;
2069
- }
2070
2064
2071
2065
// Value case
2072
2066
else if constexpr (requires{ bool { *std::any_cast<CPP2_TYPEOF (value)>(&x) == value }; }) {
@@ -2080,10 +2074,12 @@ inline constexpr auto is( std::any const& x, auto&& value ) -> bool
2080
2074
2081
2075
// as
2082
2076
//
2083
- template <typename T, typename X>
2084
- requires (!std::is_reference_v<T> && std::is_same_v<X,std::any> && !std::is_same_v<T,std::any>)
2085
- constexpr auto as ( X const & x ) -> T
2086
- { return std::any_cast<T>( x ); }
2077
+ template <typename T, same_type_as<std::any> X>
2078
+ constexpr auto as ( X && x ) -> decltype(auto ) {
2079
+ constness_like_t <T, X>* ptr = std::any_cast<T>( &x );
2080
+ if (!ptr) { Throw ( std::bad_any_cast (), " 'as' cast failed for 'std::any'" ); }
2081
+ return cpp2::forward_like<X>(*ptr);
2082
+ }
2087
2083
2088
2084
2089
2085
// -------------------------------------------------------------------------------------------------------------
0 commit comments