[Boost][bind] problem using bind with non-copyable class members?
Hi, I'm trying to use bood::bind and I'm having a problem with compilation when I'm passing an object that holds a std::stringstream. The errors are to do with std::ios_base and std::streambuf's copy constructors being private, but as far as I knew (and understand from the docs), boost::bind passes by reference, so nothing should be copied. I'm sure I'm missing something fundamental here, so any enlightening would be very welcome. My setup is gcc 4.1.2 on Ubuntu 6.10. Here's an example program that fails: #include <sstream>
#include
#include using namespace boost; // sorry using namespace std; // oops
struct holder { std::ostringstream oss; };
void sub_main( holder& h ) { return; }
class event_handler { public: void operator() ( holder& h ) { thread t( bind( &sub_main, h ) ); t.join(); } };
int main( int, char** ) { event_handler e_h; holder h;
e_h(h);
return 0; }
Regards, Darren
Darren Garvey wrote:
Hi,
I'm trying to use bood::bind and I'm having a problem with compilation when I'm passing an object that holds a std::stringstream. The errors are to do with std::ios_base and std::streambuf's copy constructors being private, but as far as I knew (and understand from the docs), boost::bind passes by reference, so nothing should be copied.
No, boost::bind( f, x, y... ) stores copies of f, x and y. Use ref(h) to make it store a reference. See the last paragraph of http://boost.org/libs/bind/bind.html#with_functions You might also consider using shared_ptr<holder> unless you have the lifetime issues already figured out.
Hi Peter,
On 20/02/07, Peter Dimov
<snip> No, boost::bind( f, x, y... ) stores copies of f, x and y. Use ref(h) to make it store a reference. See the last paragraph of
http://boost.org/libs/bind/bind.html#with_functions
You might also consider using shared_ptr<holder> unless you have the lifetime issues already figured out.
I remember seeing ref used before. Now I know why. Thanks for the doc pointer. And you're right about the shared_ptr, which I'm using (in a roundabout way) in the real code. Thanks for the reply. Darren
participants (2)
-
Darren Garvey
-
Peter Dimov