"The C++ Programming Language [4th Edition] - Bjarne Stroustrup" 中的绑定错误
Mistake with bind in "The C++ Programming Language [4th Edition] - Bjarne Stroustrup"
我在 Bjarne Stroustrup 的书中找到了这段代码:
这段代码的问题是变量i
没有停留在2,而是增加到3。你可以在这里查看:https://wandbox.org/permlink/p5JC1nOA4pIpsgXb
我们不必使用 std::ref()
来增加这个变量。是书中的错误还是自 C++11 以来发生了一些变化?
该示例不正确,bind
会复制其参数,除非您将其包装在 std::reference_wrapper
中,如文本正确所述,但这不是该示例显示的内容。在示例中,参数 i
被传递给 bind
返回的仿函数,而不是 bind
本身。如果示例改为以下,则 i
的值将保持 2
.
auto inc = bind(incr, i); // a copy of i is made
inc(); // i stays 2; inc(i) incremented a local copy of i
在书中所示的示例中,参数 i
将被转发给 incr
,这将导致对原始 i
的左值引用被传递给函数, 原来的 i
会自增
相关标准报价,来自23.14.11.3
[func.bind.bind]/10
The values of the bound arguments v1
, v2
, …, vN
and their corresponding types V1
, V2
, …, VN
depend on the types TDi
derived from the call to bind
and the cv-qualifiers cv of the call wrapper g
as follows:
...
— if the value j
of is_placeholder_v<TDi>
is not zero, the argument is std::forward<Uj>(uj)
and its type Vi
is Uj&&
;
我在 Bjarne Stroustrup 的书中找到了这段代码:
这段代码的问题是变量i
没有停留在2,而是增加到3。你可以在这里查看:https://wandbox.org/permlink/p5JC1nOA4pIpsgXb
我们不必使用 std::ref()
来增加这个变量。是书中的错误还是自 C++11 以来发生了一些变化?
该示例不正确,bind
会复制其参数,除非您将其包装在 std::reference_wrapper
中,如文本正确所述,但这不是该示例显示的内容。在示例中,参数 i
被传递给 bind
返回的仿函数,而不是 bind
本身。如果示例改为以下,则 i
的值将保持 2
.
auto inc = bind(incr, i); // a copy of i is made
inc(); // i stays 2; inc(i) incremented a local copy of i
在书中所示的示例中,参数 i
将被转发给 incr
,这将导致对原始 i
的左值引用被传递给函数, 原来的 i
会自增
相关标准报价,来自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&&
;