Skip to content

Commit 30be721

Browse files
committed
Refactor of is() - part 4 (std::optional)
1 parent 272c3bc commit 30be721

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

include/cpp2util.h

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,40 +1901,33 @@ constexpr auto as( X const& x ) -> T
19011901
// std::optional is and as
19021902
//
19031903

1904-
// is Type
1904+
// std::optional variable is Type
19051905
//
1906-
template<typename T, typename X>
1907-
requires std::is_same_v<X,std::optional<T>>
1908-
constexpr auto is( X const& x ) -> bool
1909-
{ return x.has_value(); }
1910-
1911-
template<typename T, typename U>
1912-
requires std::is_same_v<T,empty>
1913-
constexpr auto is( std::optional<U> const& x ) -> bool
1914-
{ return !x.has_value(); }
1906+
template<not_same_as<empty> T, specialization_of_template<std::optional> U>
1907+
requires not_same_as<T, U> && not_same_as<T, pointee_t<U>>
1908+
constexpr auto is( U&& ) -> std::false_type {
1909+
return {};
1910+
}
19151911

1912+
template<not_same_as<empty> T, specialization_of_template<std::optional> U>
1913+
requires not_same_as<T, U>
1914+
constexpr auto is( U&& x ) {
1915+
return x.has_value();
1916+
}
19161917

1917-
// is Value
1918+
//-------------------------------------------------------------------------------------------------------------
1919+
// pointer_like variable is value
19181920
//
1919-
template<typename T>
1920-
constexpr auto is( std::optional<T> const& x, auto&& value ) -> bool
1921-
{
1922-
// Predicate case
1923-
if constexpr (requires{ bool{ value(x) }; }) {
1924-
return value(x);
1925-
}
1926-
else if constexpr (std::is_function_v<decltype(value)> || requires{ &value.operator(); }) {
1927-
return false;
1928-
}
19291921

1930-
// Value case
1931-
else if constexpr (requires{ bool{ x.value() == value }; }) {
1932-
return x.has_value() && x.value() == value;
1922+
template <pointer_like X, typename V>
1923+
constexpr auto is( X const& x, V && value) -> decltype(auto) {
1924+
if constexpr (pointer<X> && pointer<V>) {
1925+
return x == value;
1926+
} else {
1927+
return !is<empty>(x) && is(*x, CPP2_FORWARD(value));
19331928
}
1934-
return false;
19351929
}
19361930

1937-
19381931
// as
19391932
//
19401933
template<typename T, typename X>

0 commit comments

Comments
 (0)