From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-10-30 08:11:00
From: "Gustavo Guerra" <gustavobt_at_[hidden]>
> I get a lot of errors when I try to bind a free function with an auto_ptr
> Simple illustrative code, tested in gcc 3.2 (cygwin) with both 1.29 and
> current CVS:
> void foo(auto_ptr<int> i_aptr);
> int f()
> auto_ptr<int> i_aptr(new int(0));
> function0<void> f = bind(&foo, i_aptr);
> What am I doing wrong? It seems to be some problem with const correctness.
There are several problems with the above, that can be summarized as
"auto_ptr is evil".
bind(foo, i_aptr) attempts to create a function object that contains a copy
of i_aptr. It cannot do that since auto_ptr doesn't have an ordinary copy
constructor. Even if it could create the function object, it would not have
an usable copy constructor, rendering it pretty much useless.
Even if the function object could somehow be copied, it wouldn't be
callable, as calling foo(p) modifies p, and operator() is const.
Auto_ptr is evil. Use shared_ptr. Or wrap i_aptr in boost::ref, if you have
control over its lifetime and can ensure that it won't outlive f.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk