阅读背景:

如果存储原始引用的类超出范围,捕获成员引用是否安全?

来源:互联网 

Consider this:

考虑一下:

#include <iostream>
#include <functional>

std::function<void()> task;
int x = 42;

struct Foo
{
   int& x;

   void bar()
   {
      task = [=]() { std::cout << x << '\n'; };
   }
};

int main()
{
   {
      Foo f{x};
      f.bar();
   }

   task();
}
#include <iostream>
#i



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: