I do some experiences about boost::bind
And I find out that boost::bind would incur heavy copy if you don't pass by referene
#include<iostream>
#include<boost/bind.hpp>
struct test_for_bind_00
{
test_for_bind_00() {}
test_for_bind_00(test_for_bind_00 const &Obj) { std::cout<<"this is copy constructor :: tfb00"<<std::endl; }
};
struct test_for_bind
{
void tamaya() { std::cout<<"tamaya"<<std::endl; }
void tamaya(test_for_bind_00 const&) { std::cout<<"this is copy :: tamaya"<<std::endl; }
test_for_bind() {}
test_for_bind(test_for_bind const &Obj)
{
std::cout<<"this is copy constructor :: tamaya"<<std::endl;
}
};
struct F2
{
typedef void result_type;
void operator()( ) { std::cout<<"F2, F2, F2"<<std::endl; }
F2() {}
F2(F2 const&) { std::cout<<"this is copy :: F2"<<std::endl; }
};
int main()
{
test_for_bind tb_0;
boost::bind( &test_for_bind::tamaya, test_for_bind() )();
test_for_bind_00 tfb00_0;
boost::bind( &test_for_bind::tamaya, &tb_0, boost::cref(tfb00_0) )();
F2 f;
boost::bind(f)
return 0;
}
boost::bind( &test_for_bind::tamaya, test_for_bind() )();
would produce
this is copy constructor :: tamaya
this is copy constructor :: tamaya
this is copy constructor :: tamaya
this is copy constructor :: tamaya
boost::bind( &test_for_bind::tamaya, &tb_0, boost::cref(tfb00_0) )();
would produce
this is copy constructor :: tfb00
this is copy constructor :: tfb00
this is copy constructor :: tfb00
this is copy constructor :: tfb00
this is copy constructor :: tfb00
this is copy :: tamaya
boost::bind(f)();
would produce
this is copy :: F2
this is copy :: F2
this is copy :: F2
F2, F2, F2
I don't know why boost::bind would need to make so many copies
besides,
boost::bind( &test_for_bind::tamaya, &tb_0, boost::cref(tfb00_0) )();
would produce one more copy of "tb_0" no matter what.
How could I save the copy of "tb_0"?
Thanks a lot