Skip to content

Instantly share code, notes, and snippets.

@hikarin522
Created March 3, 2020 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hikarin522/60ecf4b028f353d594e06f93f52ad3f8 to your computer and use it in GitHub Desktop.
Save hikarin522/60ecf4b028f353d594e06f93f52ad3f8 to your computer and use it in GitHub Desktop.
#pragma once
// C++20 bind_front
// https://cpprefjp.github.io/reference/functional/bind_front.html
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0356r5.html
#ifndef __cpp_lib_bind_front
#include <utility>
#include <functional>
namespace std {
template <class ...Arg, size_t ...N>
auto _bind_front(std::index_sequence<N...> seq, Arg &&...arg) {
return std::bind(std::forward<Arg>(arg)..., std::_Placeholder<N + 1>{}...);
}
template <class Class, class Ret, class ...Arg, class ...Bind>
auto bind_front(Ret(Class::*func)(Arg...) const, Bind &&...bind) {
auto seq = std::make_index_sequence<sizeof...(Arg) - sizeof...(Bind) + 1>();
return _bind_front(seq, func, std::forward<Bind>(bind)...);
}
template <class Class, class Ret, class ...Arg, class ...Bind>
auto bind_front(Ret(Class::*func)(Arg...), Bind &&...bind) {
auto seq = std::make_index_sequence<sizeof...(Arg) - sizeof...(Bind) + 1>();
return _bind_front(seq, func, std::forward<Bind>(bind)...);
}
template <class Ret, class ...Arg, class ...Bind>
auto bind_front(Ret(*func)(Arg...), Bind &&...bind) {
auto seq = std::make_index_sequence<sizeof...(Arg) - sizeof...(Bind)>();
return _bind_front(seq, func, std::forward<Bind>(bind)...);
}
template <class Ret, class ...Arg, class ...Bind>
auto bind_front(std::function<Ret(Arg...)> &func, Bind &&...bind) {
auto seq = std::make_index_sequence<sizeof...(Arg) - sizeof...(Bind)>();
return _bind_front(seq, func, std::forward<Bind>(bind)...);
}
template <class Ret, class ...Arg, class ...Bind>
auto bind_front(std::function<Ret(Arg...)> &&func, Bind &&...bind) {
auto seq = std::make_index_sequence<sizeof...(Arg) - sizeof...(Bind)>();
return _bind_front(seq, std::move(func), std::forward<Bind>(bind)...);
}
} // namespace std
#endif // __cpp_lib_bind_front
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment