Skip to content

Instantly share code, notes, and snippets.

@rilerez

rilerez/FN.hpp

Last active Apr 8, 2021
Embed
What would you like to do?
A silly macro for a shorthand for lambdas
namespace FN_impl {
template<class T>
constexpr auto depend_id(auto x) {
return x;
}
struct fake {
template<class T>
operator T() {
static_assert(depend_id<T>(false),
"Insufficient arguments were passed to a FN lambda");
return {};
}
};
} // namespace FN_impl
#define FN(...) \
<class T0 = ::FN_impl::fake, \
class T1 = ::FN_impl::fake, \
class T2 = ::FN_impl::fake, \
class T3 = ::FN_impl::fake, \
class T4 = ::FN_impl::fake, \
class T5 = ::FN_impl::fake> /* */ \
([[maybe_unused]] T0 _0 = {}, \
[[maybe_unused]] T1 _1 = {}, \
[[maybe_unused]] T2 _2 = {}, \
[[maybe_unused]] T3 _3 = {}, \
[[maybe_unused]] T4 _4 = {}, \
[[maybe_unused]] T5 _5 = {}) { \
auto& _ = _0; \
return __VA_ARGS__; \
}
// usage:
struct point {
int x;
int y;
}
auto less_x = []FN(_0.x < _1.x);
auto less_y = []FN(_0.y < _1.y);
template<class Less = std::less>
constexpr auto compare_by(auto projection, Less less = {}){
return [=]FN(less(projection(_0), projection(_1)));
}
// now you can write these more easily:
auto less_x_better = compare_by([]FN(_.x));
auto less_y_better = compare_by([]FN(_.y));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment