I have run the following code in Visual C++ 2012 express edition

#include <iostream>
#include <memory>

int main ()
{
   auto deleter=[&](int* ptr){ delete [] ptr; std::cout << "DeleterCalled " << std::endl; };
   std::unique_ptr<int[], decltype(deleter)> ptr1(new int[4], deleter);
   std::unique_ptr<int[], decltype(deleter)> ptr2(std::move(ptr1));
}

which gives the result as

Deleter Called
Deleter Called

However when I run the code after compiling using gcc 4.8 I get the following

Deleter Called

Why is the deleter called twice in VC++ where practically it seems that memory should be allocated only once, which implies that the deleter should also be called once. The next operation is std::move which I think should only transfer the ownership to the new pointer. However when I use gcc 4.8 it seems that the program is working as expected.

0

There are 0 best solutions below