相关疑难解决方法(0)

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

有没有办法按值捕获,并使捕获的值非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)

c++ lambda const c++11

95
推荐指数
1
解决办法
2万
查看次数

lambda 捕获中的值变成常量?

我有这个简单的代码:

std::shared_ptr<std::string> s;

auto bla = [s]() {
    s.reset();
};
Run Code Online (Sandbox Code Playgroud)

我的意思是,shared_ptr 被 lambda 捕获,然后在调用 lambda 后重置。
用 VS 编译会产生以下错误:

error C2662: 'void std::shared_ptr<std::string>::reset(void) noexcept': cannot convert 'this' pointer from 'const std::shared_ptr<std::string>' to 'std::shared_ptr<std::string> &' 1>...: message : Conversion loses qualifiers

是什么赋予了?怎么轮到shared_ptrconst shared_ptr

c++ lambda capture shared-ptr

4
推荐指数
1
解决办法
1397
查看次数

标签 统计

c++ ×2

lambda ×2

c++11 ×1

capture ×1

const ×1

shared-ptr ×1