identical boost expression used in different parts of the code - looking for an alias
hi for example, i use boost::bind( &A::f, _1 ) in different parts of my code. how can i define an alias for this expression? i'm thinking of using something like some_type* fo = new boost::bind(&A::f,_1); and use *fo in place of boost::bind(...). is this crazy? what should some_type be?
e r wrote:
i'm thinking of using something like
some_type* fo = new boost::bind(&A::f,_1);
and use *fo in place of boost::bind(...). is this crazy? what should some_type be?
You can use boost::function to store the result of a boost::bind expression: http://boost.org/libs/bind/bind.html#with_boost_function For instance, you might write: boost::function<void(const A&)> bound = boost::bind(&A::f, _1); depending on the signature of A::f. Note, don't use 'new', just assign the value of boost::bind.
Nat Goodspeed wrote:
e r wrote:
i'm thinking of using something like
some_type* fo = new boost::bind(&A::f,_1);
and use *fo in place of boost::bind(...). is this crazy? what should some_type be?
You can use boost::function to store the result of a boost::bind expression:
http://boost.org/libs/bind/bind.html#with_boost_function
For instance, you might write:
boost::function<void(const A&)> bound = boost::bind(&A::f, _1);
depending on the signature of A::f.
Note, don't use 'new', just assign the value of boost::bind.
thanks! i still need a little help, if possible. boost::function<void(const boost::shared_ptr<A>&)> deref = boost::bind(&boost::shared_ptr<A>::operator*, _1); Functor ftor; std::vector<boost::shared_ptr<A> > vec = ...; for_each( vec.begin(), vec.end(), boost::bind( boost::ref(ftor), boost::bind(&boost::shared_ptr<A>::operator*, _1) ) );//fine for_each( vec.begin(), vec.end(), boost::bind( boost::ref(ftor), deref ) );//not fine. see error below //no match for call to ‘(Functor) (boost::function<void ()(const //boost::shared_ptr<A>&), std::allocator<void> >&)
e r wrote:
hi boost::bind( &A::f, _1 )
in different parts of my code. how can i define an alias for this expression?
i'm thinking of using something like
some_type* fo = new boost::bind(&A::f,_1);
and use *fo in place of boost::bind(...). is this crazy? what should some_type be?
Normally the easiest thing to do is to put it into a boost::function object. The correct type depends on the parameter to &A::f and the return type. Assuming that it is this: R A::f( B ) then something like this should work: boost::function< R ( B ) > do_f( boost::bind( &A::f, _1 ) ); R my_r = do_f( a_B ); K
participants (3)
-
e r
-
Kirit Sælensminde
-
Nat Goodspeed