Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing std::move() and std::thread won't compile

Tags:

c++

c++11

Having code as follows:

#include <memory>
#include <thread>

class A
{
  void foo(int&& arg) const {}

  void boo() const 
  {
    int value(0);
    std::thread t(&A::foo, this, std::move(value));
    t.join();
  }

};

int main()
{
  A a;
  return 0;
}

MS Visual Studio 2012 (toolset v110) gives next error:

error C2664: '_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,>::operator ()(const _Wrapper &,_V0_t) const' : cannot convert parameter 2 from 'int' to 'int &&'

What's that? Can't we use move semantics through threads?

like image 637
Andrey Lyubimov Avatar asked Apr 23 '15 10:04

Andrey Lyubimov


1 Answers

This is bug in VS. You can see this:

https://connect.microsoft.com/VisualStudio/feedback/details/737812

// Opened: 4/19/2012 8:58:36 PM, hmm :)

And workaround from theirs page:

You can use std::ref, but it's not the same.

Closed as fixed, so probably you need to use never tools or use 'workaround'.

like image 149
senfen Avatar answered Oct 19 '22 12:10

senfen