Yerel bir değişkeni lambda ele geçirmeye çalışıyorum. Ben derlerkenC++ yakalama hareketi başlatıldı const?
#include <thread>
#include <iostream>
// Moveable but not copyable object.
class WorkUnit
{
public:
WorkUnit(int) {}
WorkUnit(WorkUnit&&) noexcept {}
WorkUnit& operator=(WorkUnit&&) noexcept {return *this;}
WorkUnit(WorkUnit const&) = delete;
WorkUnit& operator=(WorkUnit const&) = delete;
// Non const function.
void doWork()
{
std::cerr << "Work\n";
}
};
int main()
{
WorkUnit data(4);
// Use C++14 generalized lambda capture.
std::thread test([data{std::move(data)}]()
{
// here it is complaining the `data` is a const value.
// Is there a way to capture this as a non const?
data.doWork();
}
);
test.join();
}
Ben bu olsun.
> g++ -std=c++14 WU.cpp
Test.cpp:26:13: error: member function 'doWork' not viable: 'this' argument has type 'const WorkUnit',
but function is not marked const
data.doWork();
^~~~
Yakalanan değerin const olmaması bekleniyordu.
koyun 'mutable' sonra' [verileri = std :: hareket (veri)]()/* burada */{ ' –
ya da referans ile bu yakalama '[& veri]() { veri .işi yapmak(); } '. – skypjack
@PiotrSkotnicki Neden başlatıcıya burada izin verilmiyor? – songyuanyao