Std :: stream and transition to visual studio 2013

I have only a movable class and a function that takes an object of this class by value. The functions are called in a new thread:

void foo(MyClass a) {}

int main()
{
   MyClass a;
   std::thread t(&foo, std::move(a));
}

I get a compiler error due to a missing copy constructor of MyClass (I deleted it), and if I implement it, the copy instance is called.

Obviously, this is a bug, and it compiles without copy-constructor in gcc. Are there any workarounds?

+4
source share
1 answer

If the method needs ownership a, pass it through the heap, preferably in shared_ptr:

void foo(std::shared_ptr<MyClass> a) {}
 [...]

auto a_ptr = std::make_shared<MyClass>();
std::thread t(foo, a_ptr);

Otherwise, just pass it by reference:

void foo(MyClass& a) {}
 [...]

MyClass a;
std::thread(foo, std::ref(a));
+2
source

Source: https://habr.com/ru/post/1530203/


All Articles