Use a lambda expression int main() { test t; std::function f = =(){ foo(t); }; f(); return 0; } Binding is exceedingly redundant due to the incredible ease etc of using lambdas. In addition, you did compile in Release mode with all optimizations on, right? You won't get only one copy constructor call, because first you have to produce a function object, and then assign that function object into the std::function.
Maybe it could be std::move d? Since you don't have lambdas and even binding produces three copies, you're just going to have to manually write your own function object in this case.
Use a lambda expression. Int main() { test t; std::function f = =(){ foo(t); }; f(); return 0; } Binding is exceedingly redundant due to the incredible ease etc of using lambdas. In addition, you did compile in Release mode with all optimizations on, right?
You won't get only one copy constructor call, because first you have to produce a function object, and then assign that function object into the std::function. Maybe it could be std::move'd? Since you don't have lambdas and even binding produces three copies, you're just going to have to manually write your own function object in this case.
Yes, Release mode and -O2. Std::move is possible in some cases. – Max Jan 29 at 14:41 In MinGW with GCC 4.4 I have error: expected primary-expression before '' token on std::function f = =(){ foo(t); }; :( – Max Jan 29 at 15:42 GCC 4.4 doesn't support lambdas, but you tagged C++0x so I expected that you had lambda support.
– DeadMG Jan 29 at 16:35 I update my MinGW and it's all Ok! Thank you! – Max Jan 29 at 17:16 No problem.
If my answer solved your problem, you should mark it as accepted. – DeadMG Jan 29 at 17:23.
Using lambdas is a good answer. If for whatever reason that doesn't work for you, another possibility is storing the result of the bind in something other than a std::function: decltype(std::bind(foo, t)) f = std::bind(foo, t); or: auto f = std::bind(foo, t); On my system (clang/libc++) this outputs: ctor copy ctor dtor dtor Though your milage may vary.
Hm, with MinGW (gcc version 4.4.0 (GCC)) it has 3 calls of copy ctor :( – Max Jan 29 at 15:00.
It does depend a bit on the lifetime of the object that you want to bind. Assuming that that object's lifetime includes the lifetime of the functor, just do int main() { test t; boost::function f( boost::bind(foo, boost::ref( t ) ) ); f(); return 0; } Yields one constructor call and one destructor call. :-) Cheers & hth.
He already said that he can't use a reference because it's multi-threaded. – DeadMG Jan 29 at 16:36 @DeadMG: I didn't notice, but, the called function takes argument by ref to const, so, no tread safety problem (well, unless one is added by modifying original object, but then just make one copy). – Alf P.
Steinbach Jan 29 at 16:38 @Alf: Except t is allocated locally, making a reference to it in a multi-threaded program a death sentence. – DeadMG Jan 29 at 16:46 @DeadMG: no, I did address the lifetime issue. Second para.
:-) – Alf P. Steinbach Jan 29 at 16:49 @Alf: But he already stated that is not the lifetime. Assumptions are great- if the OP didn't already explicitly state otherwise.
– DeadMG Jan 29 at 17:15.
I need deferred call of some function with arguments. So we can see what copy ctor called 11 times! Copy ctor called 9 times.
If change boost::function to std::function copy ctor will be called 4 times only. But it is bad behavior too. Is it possible do this with 1 call of copy ctor?
Sorry for my bad English :) Thank you.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.