Skip to content

Commit

Permalink
feat: transform: Pass by-value and then move
Browse files Browse the repository at this point in the history
  • Loading branch information
rvarago committed Oct 27, 2020
1 parent 65a4976 commit 14f0df7
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/absent/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ namespace rvarago::absent {
* @return a new nullable containing the mapped value of type B, possibly empty if input was also empty.
*/
template <template <typename> typename Nullable, typename A, typename UnaryFunction>
constexpr auto transform(Nullable<A> const &input,
constexpr auto transform(Nullable<A> input,
UnaryFunction &&mapper) noexcept(noexcept(std::declval<UnaryFunction>()(std::declval<A>())))
-> Nullable<decltype(std::declval<UnaryFunction>()(std::declval<A>()))> {
using B = decltype(mapper(std::declval<A>()));
if (!input) {
return Nullable<B>{};
}
return Nullable<B>{std::forward<UnaryFunction>(mapper)(*input)};
return Nullable<B>{std::forward<UnaryFunction>(mapper)(*std::move(input))};
}

/***
* Infix version of transform.
*/
template <template <typename> typename Nullable, typename A, typename UnaryFunction>
constexpr auto operator|(Nullable<A> const &input,
constexpr auto operator|(Nullable<A> input,
UnaryFunction &&mapper) noexcept(noexcept(std::declval<UnaryFunction>()(std::declval<A>())))
-> Nullable<decltype(std::declval<UnaryFunction>()(std::declval<A>()))> {
return transform(input, std::forward<UnaryFunction>(mapper));
return transform(std::move(input), std::forward<UnaryFunction>(mapper));
}

}
Expand Down

0 comments on commit 14f0df7

Please sign in to comment.