有没有办法按值捕获,并使捕获的值非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) 我有这个简单的代码:
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_ptr了const shared_ptr?