我试图通过(可变)lambda中的副本捕获一个const对象.然而,我的编译器抱怨说,捕获的对象是const.
是不是可以将对象复制为非const?
struct Foo
{
Foo(){}
void Func(){}
};
int main()
{
const Foo foo;
[foo]() mutable { foo.Func(); };
}
Run Code Online (Sandbox Code Playgroud)
用g ++ 4.7.2编译:
testcase.cpp: In lambda function:
testcase.cpp:10:29: error: no matching function for call to ‘Foo::Func() const’
testcase.cpp:10:29: note: candidate is:
testcase.cpp:4:7: note: void Foo::Func() <near match>
testcase.cpp:4:7: note: no known conversion for implicit ‘this’ parameter from ‘const Foo*’ to ‘Foo*’
Run Code Online (Sandbox Code Playgroud)
使用clang ++ 3.1进行编译:
testcase.cpp:10:20: error: member function 'Func' not viable: 'this' argument has type 'const Foo', but function …Run Code Online (Sandbox Code Playgroud)