This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Max | |
{ | |
template <class T1, class T2> | |
constexpr decltype(auto) operator()(T1&& lhs, T2&& rhs) | |
{ | |
return lhs > rhs ? std::forward<T1>(lhs) : std::forward<T2>(rhs); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Comp | |
{ | |
template <class F1, class F2> | |
auto operator()(F1 lhs, F2 rhs) | |
{ | |
return [=](auto... args) | |
{ | |
return lhs(rhs(args...)); | |
// ^^^^^^^^^^^^^^^^^ | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() | |
{ | |
auto f1 = [](auto a) { return 2 * a; }; | |
auto f2 = [](auto a) { return 3 * a; }; | |
auto f3 = [](auto a, auto b) { return b * a; }; | |
auto cfs = compose(f1, f2, f3); | |
std::cout << cfs(2, 3) << std::endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T> | |
using perfect_capture_t = | |
std::conditional_t<std::is_lvalue_reference<T>::value, | |
std::reference_wrapper<std::remove_reference_t<T>>, T>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Comp | |
{ | |
template <class F1, class F2> | |
auto operator()(F1&& lhs, F2&& rhs) | |
{ | |
return [ | |
lhs = perfect_capture_t<F1>(std::forward<F1>(lhs)), | |
rhs = perfect_capture_t<F2>(std::forward<F2>(rhs)) | |
](auto&&... args) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Curry | |
{ | |
template <class F1, class F2> | |
auto operator()(F1&& lhs, F2&& rhs) | |
{ | |
return [ | |
lhs = perfect_capture_t<F1>(std::forward<F1>(lhs)), | |
rhs = perfect_capture_t<F2>(std::forward<F2>(rhs)) | |
](auto&&... args) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() | |
{ | |
auto f1 = [](auto a) { return 2 * a; }; | |
auto f2 = [](auto a) { return 3 * a; }; | |
auto f4 = [](auto a, auto b, auto c) { return c * b * a; }; | |
std::cout << curry(f4, 2, 1)(1) << std::endl; | |
auto ccfs = compose(f1, f2, curry(f4, 2, 1)); | |
std::cout << ccfs(1) << std::endl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class... Ts> | |
void call_me(Ts&&... args) { /* implementation */ } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::apply(call_me, a); // a is array<int, 4> | |
std::apply(call_me, p); // p is pair<double, int> | |
std::apply(call_me, t); // t is tuple<char, int, double> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace detail | |
{ | |
template <std::size_t Ofst, class Tuple, std::size_t... I> | |
constexpr auto slice_impl(Tuple&& t, std::index_sequence<I...>) | |
{ | |
return std::forward_as_tuple( | |
std::get<I + Ofst>(std::forward<Tuple>(t))...); | |
} | |
} |
OlderNewer