मैं स्थानीय चर को लैम्ब्डा के कब्जे में ले जाने की कोशिश कर रहा हूं।सी ++ कैप्चर लेवल प्रारंभिक है?
#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();
}
जब मैं संकलित करता हूं तो मुझे यह मिलता है।
> 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();
^~~~
मैं पर कब्जा कर लिया मूल्य कोई स्थिरांक होने की उम्मीद कर रहा था।
रखें 'mutable' के बाद' [डेटा = std :: चाल (डेटा)]()/* यहाँ */{ ' –
या संदर्भ से यह कब्जा:' [और डेटा]() { डेटा ।काम करो(); } '। – skypjack
@PiotrSkotnicki क्यों शुरुआतकर्ता को अनुमति नहीं है? – songyuanyao