3

Possible Duplicate:

Visual Studio 2013 C++ - Passing std::unique_ptr to a bound function

Invoking a function taking a unique_ptr with std::async

std::async with std::unique doesn't compile

I'm following along with the Introduction to C++ Concurrency LiveLessons videos, but have hit a snag. Of course, the instructor has a special build of Visual Studio that lets him compile the following code, but for me, the following code does not compile in MSVC v120 or v140 Preview (Windows 8.1). However, it does compile in GCC 4.8.2 and Clang 3.4 (Ubuntu Linux 14.04):

#include <thread>
#include <future>
#include <memory>
#include <iostream>
#include <utility>

struct Counter
{
   int n;

   Counter(int k)
      : n(k)
   {
   }
};

int main()
{
   Counter* pCount = new Counter(10);
   std::unique_ptr<Counter> puCount(pCount);
   std::future<void> fut = std::async([](std::unique_ptr<Counter> p)
   {
      ++(p->n);
   }, std::move(puCount));
   fut.wait();
}

The Visual Studio compiler error is:

error C2280: 'std::unique_ptr<Counter,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function

In my quick survey of other questions it seems that the issue is an existing bug with the Visual Studio compiler where it is trying to make a copy of my unique_ptr instead of moving it into my std::async call. I also looked around, but couldn't find anything that seemed on topic in the C++11/14/17 Features In VS 2015 Preview blog post.

So, my questions are:

  1. Am I correct in assuming that this bug is still a problem with MSVC v140 Preview?
  2. Does anyone have more information on if/when the compiler or library team will fix this problem?

As an aside, I know it's not the best thing ever to make a bare pointer just to go a wrap it into a unique_ptr while still leaving the bare pointer sitting around. It's an example from the video of a bad program.

Community
  • 1
  • 1
PythonJin
  • 4,034
  • 4
  • 32
  • 40

0 Answers0