int sum( int lhs, int rhs ) { return lhs + rhs; } auto f_sum = std::bind( sum, 3, std::placeholders::_2 ); f_sum( 5, 7 ); // f_sum 10
struct A { void Print() const { std::cout << "A::Print()" << std::endl; } }; A a; auto f = std::bind(&A::Print, a); f(); // "" A::Print()
namespace naive { template<typename Func, typename... BinderArgs> binder<Func, BinderArgs...> bind( Func const & func, BinderArgs &&... args ) { return binder<Func, BinderArgs...>( func, std::forward<BinderArgs>( args )... ); } }
// ... template<typename Func, typename... BinderArgs> struct binder { binder( Func const & func, BinderArgs &&... binderArgs ) : m_func{ func} , m_args{ std::forward<BinderArgs>(binderArgs)... } {} template<typename... Args> void operator()( Args &&... args ) const { // ... } // ... private: invoker_t m_invoker; Func m_func; args_list<BinderArgs...> m_args; }; // ...
// arg args_list. // std::size_t , // template<std::size_t, typename T> struct arg { explicit arg( T val ) : value( val ) {} T const value; }; template<typename,typename...> struct args_list_impl; template<std::size_t... Indices, typename... Args> struct args_list_impl<indices<Indices...>, Args...> : arg<Indices, Args>... { template<typename... OtherArgs> args_list_impl( OtherArgs &&... args ) : arg<Indices, Args>( std::forward<OtherArgs>(args) )... {} }; template<typename... Args> struct args_list : args_list_impl< typename make_indices< sizeof...( Args )>::type, Args... > { using base_t = args_list_impl< typename make_indices< sizeof...( Args ) >::type, Args... >; template<typename... OtherArgs> args_list( OtherArgs &&... args ) : base_t( std::forward<OtherArgs>(args)... ) {} };
template<typename...Args> void invoke( Func const & func, Args &&... args ) const { return func( std::forward<Args>(args)... ); }
template<typename ObjType, typename...Args> void invoke( Func const & func, ObjType && obj, Args &&... args ) const { return (obj.*func)( std::forward<Args>(args)... ); }
using invoker_t = conditional_t< std::is_member_function_pointer<Func>::value, member_function_invoker, free_function_invoker >;
template<typename Func, typename... BinderArgs> struct binder { // ... template<typename... Args> void operator()( Args &&... args ) const { // need check: sizeof...(Args) should not be less than max placeholder value call_function( make_indices< sizeof...(BinderArgs) >{}, std::forward<Args>(args)... ); } private: template< std::size_t... Indices, typename... Args > void call_function( indices<Indices...> const &, Args &&... args ) const { struct empty_list { empty_list( Args &&... args ) {} }; using args_t = conditional_t< sizeof...(Args) == 0, empty_list, args_list<Args...> >; args_t const argsList{ std::forward<Args>(args)... }; m_invoker.invoke( m_func, take_argument( get_arg<Indices,BinderArgs...>( m_args ), argsList )... ); } // ... };
template<std::size_t I, typename Head, typename... Tail> struct type_at_index { using type = typename type_at_index<I-1, Tail...>::type; }; template<typename Head, typename... Tail> struct type_at_index<0, Head, Tail...> { using type = Head; }; template<std::size_t I, typename... Args> using type_at_index_t = typename type_at_index<I, Args...>::type; template<std::size_t I, typename... Args> type_at_index_t<I, Args...> get_arg( args_list<Args...> const & args ) { arg< I, type_at_index_t<I, Args...> > const & argument = args; return argument.value; };
template<typename T, typename S> T take_argument( T const & arg, S const & args ) { return arg; } template<typename T, typename... Args, std::size_t I = std::is_placeholder<T>::value, typename = typename std::enable_if< I != 0 >::type > type_at_index_t<I-1, Args...> take_argument( T const & ph, args_list<Args...> const & args ) { return get_arg< I-1, Args... >( args ); }
#include "binder.hpp" #include <iostream> #include <string> void Print( std::string const & msg ) { std::cout << "Print(): " << msg << std::endl; } struct A { void Print( std::string const & msg ) { std::cout << "A::Print(): " << msg << std::endl; } }; int main() { std::string const hello {"hello"}; auto f = naive::bind( &Print, hello ); auto f2 = naive::bind( &Print, std::placeholders::_1 ); f(); f2( hello ); A a; auto f3 = naive::bind( &A::Print, std::placeholders::_2, std::placeholders::_1 ); auto f4 = naive::bind( &A::Print, std::placeholders::_1, hello ); auto f5 = naive::bind( &A::Print, a, std::placeholders::_1 ); auto f6 = naive::bind( &A::Print, a, hello ); f3( hello, a ); f4( a ); f5( hello ); f6(); return 0; }
Source: https://habr.com/ru/post/310270/
All Articles