小编z33*_*3ky的帖子

lambda闭包中复制的const对象不可变

我试图通过(可变)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)

c++ lambda c++11

10
推荐指数
1
解决办法
1936
查看次数

标签 统计

c++ ×1

c++11 ×1

lambda ×1