tr1::reference_wrapper 有什么用?

发布于 2024-07-06 08:39:06 字数 276 浏览 5 评论 0原文

最近,我阅读了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七颜 2024-07-13 08:39:06

据我所知,这就像 boost::ref 。 基本上,可以复制的参考。 当绑定到需要通过引用传递参数的函数时非常有用。

例如(使用 boost 语法):

void Increment( int& iValue )
{
    iValue++;
}

int iVariable = 0;
boost::function< void () > fIncrementMyVariable = boost::bind( &Increment, boost::ref( iVariable ));

fIncrementMyVariable();

这篇 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):

void Increment( int& iValue )
{
    iValue++;
}

int iVariable = 0;
boost::function< void () > fIncrementMyVariable = boost::bind( &Increment, boost::ref( iVariable ));

fIncrementMyVariable();

This Dr. Dobbs article has some info.

Hope this is right, and helpful. :)

相守太难 2024-07-13 08:39:06

reference_wrapper 是一个非常有用且简单的库。 reference_wrapper 在内部存储指向 T 的指针。但它公开的接口不包含任何指针表示法。

  • 它允许引用像其他简单对象一样运行 - reference_wrapper 可以存储在 STL 容器中。
  • 它有助于避免可怕的指针表示法——这是许多分段错误的原因。 尽可能将指向 T 的指针替换为 reference_wrapper,将指针替换为引用,将 T->f() 替换为 Tf() (当然需要存储指针来删除堆分配的对象,但是对于内存管理Boost Pointer Containers非常有用)。

示例:

class A
{
    //...
};

class B
{
 public:
   void setA(A& a) 
   {
     a_ = boost::ref(a); // use boost::cref if using/storing const A&
   }
   A& getA()
   {
      return a_;
   }
   B(A& a): a_(a) {}
private:
   boost::reference_wrapper<A> a_; 
};

int main()
{
   A a1;
   B b(a1);
   A a2;
   b.setA(a2);
   return 0;
}

这里我使用了引用包装器的 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 the reference_wrapper<T> stores a pointer to T. But the interface it exposes does not contain any pointer notation.

  • It allows the reference to behave like other simple objects - a reference_wrapper<T> can be stored in a STL container.
  • It helps avoid the dreadful pointer notation - the cause of so many segmentation faults. Replace a pointer to T with a reference_wrapper<T>, pointers by references and T->f() by T.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:

class A
{
    //...
};

class B
{
 public:
   void setA(A& a) 
   {
     a_ = boost::ref(a); // use boost::cref if using/storing const A&
   }
   A& getA()
   {
      return a_;
   }
   B(A& a): a_(a) {}
private:
   boost::reference_wrapper<A> a_; 
};

int main()
{
   A a1;
   B b(a1);
   A a2;
   b.setA(a2);
   return 0;
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文