Reputation: 169
I found this code in Bjarne Stroustrup's book:
The problem with this code is that variable i
does not stay at 2, it is incremented to 3. You can check it here: https://wandbox.org/permlink/p5JC1nOA4pIpsgXb
We do not have to use the std::ref()
to increment this variable. Is it a mistake in the book or something has been changed since C++11?
Upvotes: 16
Views: 1487
Reputation: 109119
The example is incorrect, bind
does make a copy of its arguments unless you wrap it in std::reference_wrapper
as the text correctly says, but that's not what the example shows. In the example, the argument i
is passed to the functor returned by bind
, not to bind
itself. If the example had instead been the following, the value of i
would've remained 2
.
auto inc = bind(incr, i); // a copy of i is made
inc(); // i stays 2; inc(i) incremented a local copy of i
In the example shown in the book, the argument i
will be forwarded to incr
, which will result in an lvalue reference to the original i
being passed to the function, and the original i
will be incremented.
For the relevant standard quotes, from 23.14.11.3 [func.bind.bind]/10
The values of the bound arguments
v1
,v2
, …,vN
and their corresponding typesV1
,V2
, …,VN
depend on the typesTDi
derived from the call tobind
and the cv-qualifiers cv of the call wrapperg
as follows:
...
— if the valuej
ofis_placeholder_v<TDi>
is not zero, the argument isstd::forward<Uj>(uj)
and its typeVi
isUj&&
;
Upvotes: 11