tr1::reference_wrapper 有什么用?
最近,我阅读了 Scott Meyers 的优秀 Effective C++ 书。 在最后一篇技巧中,他介绍了 TR1 的一些功能 - 我通过 Boost 了解了其中许多功能。
然而,有一个我绝对不认识:tr1::reference_wrapper。
我如何以及何时使用 tr1::reference_wrapper?
recently I've been reading through Scott Meyers's excellent Effective C++ book. In one of the last tips he covered some of the features from TR1 - I knew many of them via Boost.
However, there was one that I definitely did NOT recognize: tr1::reference_wrapper.
How and when would I use tr1::reference_wrapper?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,这就像 boost::ref 。 基本上,可以复制的参考。 当绑定到需要通过引用传递参数的函数时非常有用。
例如(使用 boost 语法):
这篇 Dobbs 博士文章有一些信息。
希望这是正确的,并且有帮助。 :)
It's like boost::ref, as far as I know. Basically, a reference which can be copied. Very useful when binding to functions where you need to pass parameters by reference.
For example (using boost syntax):
This Dr. Dobbs article has some info.
Hope this is right, and helpful. :)
reference_wrapper
是一个非常有用且简单的库。reference_wrapper
在内部存储指向 T 的指针。但它公开的接口不包含任何指针表示法。reference_wrapper
可以存储在 STL 容器中。reference_wrapper
,将指针替换为引用,将T->f()
替换为Tf()
(当然需要存储指针来删除堆分配的对象,但是对于内存管理Boost Pointer Containers非常有用)。示例:
这里我使用了引用包装器的 boost 实现,但 C++0x 标准也将拥有它。 另请参阅http://aszt.inf.elte.hu/ ~gsd/halado_cpp/ch11.html#Bind-ref
reference_wrapper<T>
is an immensely useful and simple library. Internally thereference_wrapper<T>
stores a pointer to T. But the interface it exposes does not contain any pointer notation.reference_wrapper<T>
can be stored in a STL container.reference_wrapper<T>
, pointers by references andT->f()
byT.f()
wherever possible (ofcourse pointers need to be stored for deleting a heap-allocated objects, but for memory management Boost Pointer Containers are quite useful).Example:
Here I have used the boost implementation of reference wrapper, but C++0x standard is going to have it too. See also http://aszt.inf.elte.hu/~gsd/halado_cpp/ch11.html#Bind-ref