The boost function
stuff takes variables by value by default, much like normal functions. If you want the function to use a reference to your CallCounter
, you have to tell it that explicitly:
callNTimes( 20, boost::ref(counter) );
See it here: http://coliru.stacked-crooked.com/a/5843b6dde685b569
There also exists a boost::cref
if you want a constant reference, but no boost::rref
(which would be nearly identical to a value, but single use. function
is multi-use, so no moves). Additionally, as of C++11, all of this is also in the std
namespace.
In addition, Jefffrey's suggestion is generally considered a better way to code this sort of thing, if possible. It can have a negative impact on compile times though.