Yani bir std :: unique işaretçisini parametre olarak ayrı bir iş parçacığıyla başlatılan bir işleve geçirmeye çalışıyorum ve garip bir hata alıyorum derleme zamanında okur:std :: unique_ptr parametresi olarak std :: thread
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1149): error C2280: 'std::unique_ptr<Widget,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function
hala aynı sorunu yeniden üreten bu kodun, bir basitleştirilmiş bir versiyonudur:
#include <thread>
#include <memory>
#include <iostream>
class Widget
{
public:
Widget() : m_data(0)
{
}
void prepareData(int i)
{
m_data = i;
}
int getData() const
{
return m_data;
}
private:
int m_data;
};
void processWidget(std::unique_ptr<Widget> widget)
{
std::cout << widget->getData() << std::endl;
}
int main()
{
std::unique_ptr<Widget> widget(new Widget());
widget->prepareData(42);
std::thread t(processWidget, std::move(widget));
t.join();
return 0;
}
Benim tahminim Widget nesnesinin yıkımı ile yanlış bir şey olduğudur Ancak main() 'dan, sorunu belirleyemiyorum. Bu değişkeni temizlemek için ek bir şey yapmak gerekli mi? Bu arada, VS2013 kullanıyorum.
@DevSolar 'std :: thread' her zaman işleyicisi bir Xvalue geçmez ve bu bir MSVC arıza olduğunu:
Bir referansla düzeltebilirim? –
GCC bunu kabul ediyor. std :: thread t ([& widget]() {processWidget (std :: move (widget));}); 'Ayrıca çalışır. –
, Orwel DevC++ 5.11'de iyi çalışıyor! Hiçbir şey senin kodunda –