This is a very simple question. Consider the following code:
#include <iostream>
#include <memory>
typedef std::unique_ptr<void> UniqueVoidPtr;
int main() {
UniqueVoidPtr p(new int);
return 0;
}
Compiling with cygwin (g++ 4.5.3) with the following command g++ -std=c++0x -o prog file.cpp
works just fine. However, compiling with the microsoft compiler (either VS 2010 or 2013) I get this error:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\memory(2067) : error C2070: 'void': illegal sizeof operand
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\memory(2066) : while compiling class template member function 'void std::default_delete<_Ty>::operator ()(_Ty *) const'
with
[
_Ty=void
]
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\type_traits(650) : see reference to class template instantiation 'std::default_delete<_Ty>' being compiled
with
[
_Ty=void
]
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\memory(2193) : see reference to class template instantiation 'std::tr1::is_empty<_Ty>' being compiled
with
[
_Ty=std::default_delete<void>
]
foo1.cpp(7) : see reference to class template instantiation 'std::unique_ptr<_Ty>' being compiled
with
[
_Ty=void
]
Is this expected? I'm writing a class where I wanted to have a unique pointer in the in the class. While trying to work out the semantics of a move constructor for the class, I ran into this (I assume because I finally got my move constructor coded correctly: i.e. the other errors were fixed).
Use unique_ptr when you want to have single ownership(Exclusive) of the resource. Only one unique_ptr can point to one resource. Since there can be one unique_ptr for single resource its not possible to copy one unique_ptr to another. A shared_ptr is a container for raw pointers.
std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.
It can be assigned: class owner { std::unique_ptr<someObject> owned; public: owner() { owned=std::unique_ptr<someObject>(new someObject()); } };
unique_ptr objects automatically delete the object they manage (using a deleter) as soon as they themselves are destroyed, or as soon as their value changes either by an assignment operation or by an explicit call to unique_ptr::reset.
MSVC is right while GCC is wrong:
Standard(3.9/5):
Incompletely-defined object types and the void types are incomplete types
Standard(20.7.1.1.2/4):
If T is an incomplete type, the program is ill-formed
GCC actually has code to prevent it, but it didn't work until recently.
GCC's unique_ptr
has a static assertion in default_deleter::operator()
that should reject incomplete types:
static_assert(sizeof(_Tp)>0,
"can't delete pointer to incomplete type");
However, as an extension GCC supports sizeof(void)
, so the assertion doesn't fail, and because it appears in a system header doesn't even give a warning (unless you use -Wsystem-headers
).
I discovered this problem myself recently so to fix it I added this 10 days ago:
static_assert(!is_void<_Tp>::value,
"can't delete pointer to incomplete type");
So using the latest code on trunk your example fails to compile, as required by the standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With