C++ 0x lambda按值捕获总是const?

Zac*_*Zac 95 c++ lambda const c++11

有没有办法按值捕获,并使捕获的值非const?我有一个库函子,我想捕获并调用一个非常量但应该是的方法.

以下不编译,但使foo :: operator()const修复它.

struct foo
{
  bool operator () ( const bool & a )
  {
    return a;
  }
};


int _tmain(int argc, _TCHAR* argv[])
{
  foo afoo;

  auto bar = [=] () -> bool
    {
      afoo(true);
    };

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Edw*_*nge 149

使用可变.


auto bar = [=] () mutable -> bool ....
Run Code Online (Sandbox Code Playgroud)

如果没有mutable,则声明lambda对象const的operator().