Topic: use bind1st to adapt member function
Author: "X. Liu" <liu@erdw.ethz.ch>
Date: 1998/08/06 Raw View
Hi, Any one would like to give some comments or examples to use the
binder1st to adapter the member function? I tried in the following way, but
failed to make it compiled.
// code begins from here--------------------------
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
class test{
public:
bool run(int a)
{
cout<<"test"<<a<<"\n";
return true;
}
};
main(){
typedef mem_fun1_t<bool, test, int> MF;
MF &mf=mem_fun1(test::run);
test t;
mf(&t, 10); // this work OK, and print test10
typedef binder1st<MF> BD;
BD & bd=bind1st(mf, &t); // can not compile
bd(10); // I expect to print "test10" too
return 0;
}
// code ends ----------------------------------
I compiled the above code using VC++ 5.0 (SP3), and got the error:
cannot convert 'this' pointer from 'const class std::mem_fun1_t<bool,class
test,int>' to 'class std::mem_fun1_t<bool,class test,int> &'
So I tried to transform the template implementation to non-template
implementation (that is, I rewrite the mem_fun1_t and binder1st without the
template according the STL source code in VC++5.0) and recompile the code.
More problems were found.
1. binder1st constructor error:
template<MF> binder1st::binder1st(const Pred& pr, const
Pred::first_argument_type x) requires (const test *) type as the second
parameter and store it to type (test *). So a conversion error is generated.
This maybe a problem with implementation.
2. the error in calling the operator function of binder1st
the operator function is declared according the standard as: result_type
operator()(const argument_type& _X) const. It is a function with const
modifer. So calling this function assumes the binder1st object as const
binder1st type, as a sequence the members of this object also as const. Now
the problem is the operator of mem_fun1_t is not a function with const
modifer, so another conversion error occurs.
Here I copy the code for binder1st and mem_fun1_t from <functional> of
VC++5.0 just to facilitate to read this mail.
// the following from <functional>, VC++5.0
template<class _Bfn>
class binder1st
: public unary_function<_Bfn::second_argument_type,
_Bfn::result_type> {
public:
binder1st(const _Bfn& _X,
const _Bfn::first_argument_type& _Y)
: op(_X), value(_Y) {}
result_type operator()(const argument_type& _X) const
{return (op(value, _X)); }
protected:
_Bfn op;
_Bfn::first_argument_type value;
};
template<class _R, class _Ty, class _A>
class mem_fun1_t : public binary_function<_Ty *, _A, _R> {
public:
explicit mem_fun1_t(_R (_Ty::*_Pm)(_A))
: _Ptr(_Pm) {}
_R operator()(_Ty *_P, _A _Arg)
{return ((_P->*_Ptr)(_Arg)); }
private:
_R (_Ty::*_Ptr)(_A);
};
// TEMPLATE FUNCTION mem_fun1
template<class _R, class _Ty, class _A> inline
mem_fun1_t<_R, _Ty, _A> mem_fun1(_R (_Ty::*_Pm)(_A))
{return (mem_fun1_t<_R, _Ty, _A>(_Pm)); }
// TEMPLATE CLASS mem_fun_ref_t
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]