2

I was surprised to find in Visual Studio 2012 Update 1 the following does not compile anymore:

[](unique_ptr<int>){};

Compiler error states it can't access the private copy constructor of unique_ptr. The above worked just fine in Visual Studio 2010.

It also compiles just fine in gcc 4.7.2

Is this a known bug, or is this actually expected behavior? I could not find anything on Microsoft Connect.

EDIT: I have just updated to Visual Studio 2012 Update 2, the issue still occurs.

EDIT2: I have filed a bug report on Microsoft Connect, you are welcome to upvote it if it affects you too.

Ed Rowlett-Barbu
  • 1,611
  • 10
  • 27
  • Does the error occur on declaration of the lambda? – sehe Apr 21 '13 at 21:05
  • @sehe `[](unique_ptr){};` this expression alone does not compile. You can leave out `auto lambda = `. Apparently, it's the instantiation of the temporary lambda object that fails. – Ed Rowlett-Barbu Apr 21 '13 at 21:08
  • Does that happen with regular functions or is it just lambdas? – Nicol Bolas Apr 21 '13 at 22:38
  • Sounds like the cause is a result of different library files being used for `unique_ptr` before and after the update, and the different library files from after the update have the copy constructor declared as private while the previous ones did not. This sounds intentional because I believe the copy constructor for `unique_ptr` would necessarily destroy the `unique_ptr` object passed to it, and I doubt users generally want to destroy their `unique_ptr` by calling a function with it as the argument. – user1167662 Apr 22 '13 at 03:01
  • @NicolBolas heh that's the first thing I tried before posting this. Yep, works just fine with regular functions. – Ed Rowlett-Barbu Apr 22 '13 at 03:37
  • @user1167662 I doubt that is the case. unique_ptr was non-copyable in 2010 too (its copy constructor is private in 2010) too. Or have I misunderstood your statement? – Ed Rowlett-Barbu Apr 22 '13 at 03:42
  • then what is the purpose of creating this function? it sounds like the change should have no impact since this function could never be used in the first place? – user1167662 Apr 22 '13 at 03:47
  • 1
    `unique_ptr` can be constructed by move semantics also. You can read about them here: http://stackoverflow.com/questions/3106110/what-are-move-semantics To answer your (rethorical :P) question, you can call a function taking a unique_ptr like this: `YourFunction(std::move(yourUniquePtr));` – Ed Rowlett-Barbu Apr 22 '13 at 03:51

2 Answers2

1

Try this:

int a;
[a](unique_ptr<int>){};

It doesn't matter what it is, just capture something explicitly.

user2345215
  • 637
  • 5
  • 9
-1

I am still not sure whether this is a bug, it certainly looks like it.

In the meantime, a logical equivalent of

  [](unique_ptr<int> aArg)
  {

  };

can be written as

  [](unique_ptr<int> && aArg)
  {
    unique_ptr<int> arg = std::move(aArg);
  };

It's not a very nice approach for me as I am writing library code. The lambda's caller is the library and the lambda is user provided. I do not wish to impose on users that they manually std::move the argument.

So, while this is not practical for me, it might come in handy to someone else.

Ed Rowlett-Barbu
  • 1,611
  • 10
  • 27