为什么不可变的lambda捕获可以被移动?

liz*_*isk 10 c++ lambda move-semantics c++14

AFAIK非可变lambda捕获变量为const.这让我想知道他们为什么还能被感动?

auto p = std::make_unique<int>(0);
auto f = [p = std::move(p)](){ p->reset(); }; // Error, p is const
auto f2 = std::move(f); // OK, the pointer stored inside lambda is moved
Run Code Online (Sandbox Code Playgroud)

Nic*_*las 19

AFAIK非可变lambda捕获变量为const.

不,他们没有.他们的operator()超载是const.实际的成员变量不是.

它与以下内容没有什么不同:

class A
{
  unique_ptr<int> p
public:
  //Insert constructors here.

  void operator() const {p->reset();}
};
Run Code Online (Sandbox Code Playgroud)