
On Tue, Apr 27, 2010 at 11:35:19PM +0200, Piotr Jachowicz wrote:
I've called it "unexpected", because f1() and f2() in code below behaves differently:
void echo1(const char* c) { cout << c << '\n'; }
void echo2(const string& s) { cout << s << '\n'; }
int main() { boost::function
f1 = boost::bind(echo1, "abc"); //ups! binding to address of temporary boost::function f2 = boost::bind(echo2, "abc"); //ok, bind copies temporary string("abc") f1(); f2(); }
First off, your first bind comment is incorrect, as string literals have static storage duration, and both examples behave as intended. So, according to you, no-one should ever be able to bind a function parameter without it magically being cloned, in a way I can't remotely imagine how to implement. If a developer does not understand strings, they should learn how to use strings, not horribly mutilate unrelated APIs. The bind rules are very simple. It copies whatever arguments you bind to it, and stores them internally. If you feed it a raw pointer, it copies the raw pointer value. If you pass it an object, it copies the object. It's all very simple, and if you want different lifetime semantics for say pointers, use a smart pointer like shared_ptr. -- Lars Viklund | zao@acc.umu.se