Skip to content

Commit 0ece9f5

Browse files
committed
simplified Optional operator&& with requires
* synchronized implementations for PackedOptional and Optional
1 parent 3071db3 commit 0ece9f5

2 files changed

Lines changed: 4 additions & 12 deletions

File tree

src/optional19.lib/optional19/Optional.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,8 @@ struct Optional
127127
constexpr explicit operator bool() const { return this->m_valid; }
128128

129129
/// @return predicate(value()) if optional is set else return false
130-
template<class F> constexpr auto operator&&(F&& f) const -> bool {
131-
if constexpr (std::is_invocable_r_v<bool, F, T>)
132-
return this->m_valid && f(value());
133-
else {
134-
return this->m_valid && f;
135-
}
130+
template<class F> requires(std::is_invocable_r_v<bool, F, T>) constexpr auto operator&&(F&& f) const -> bool {
131+
return static_cast<bool>(*this) && f(value());
136132
}
137133

138134
template<class F> constexpr auto operator||(F&& f) const -> T {

src/optional19.lib/optional19/PackedOptional.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ template<auto makeInvalid, auto isValid = notEqualBy<makeInvalid>> struct Packed
2525
constexpr explicit operator bool() const { return isValid(m_data); }
2626

2727
/// @return predicate(value()) if optional is set else return false
28-
template<class F> constexpr auto operator&&(F&& f) const -> bool {
29-
if constexpr (std::is_invocable_r_v<bool, F, T>)
30-
return *this ? f(value()) : false;
31-
else {
32-
return *this ? f : false;
33-
}
28+
template<class F> requires(std::is_invocable_r_v<bool, F, T>) constexpr auto operator&&(F&& f) const -> bool {
29+
return static_cast<bool>(*this) && f(value());
3430
}
3531

3632
template<class F> constexpr auto operator||(F&& f) const -> T {

0 commit comments

Comments
 (0)