私はstd :: uniqueポインタを別のスレッドで起動する関数のパラメータとして渡そうとしています。読み込みコンパイル時:std :: unique_ptrが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
をまだ同じ問題を再現するこのコードの簡易版、次のとおりです。
#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;
}
私の推測では、ウィジェットオブジェクトの破壊に問題があるということですmain()からは、しかし、私は問題を特定することはできません。その変数をクリーンアップするために何か追加する必要がありますか?ところで、私はVS2013を使用しています。
@DevSolarは 'のstd :: thread'は常にそのハンドラにはxValueを渡さない、とこれはMSVCの故障でしょうか? –
GCCはそれを受け入れます。 'std :: thread t([&widget](){processWidget(std :: move(widget));});'も動作します。 –
は、Orwel DevC++ 5.11の仲間でうまくいきます!何もあなたのコードで間違っているようです –