This is a very simple question. Consider the following code:
#include
#include
typedef std::unique_ptr UniqueVo
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.
Don't delete variables of void *
。
If you want to work with something like Win32 Handles, please provide a custom deleter.
For example:
void HandleDeleter(HANDLE h)
{
if (h) CloseHandle(h);
}
using UniHandle = unique_ptr<void, function<void(HANDLE)>>;
Then:
UniHandle ptr(..., HandleDeleter);
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
The question boils down to:
void* p = new int;
delete p;
Looking at n3797 5.3.5 Delete, I believe the delete p
is undefined behavior because of mismatched types, so either compiler behavior is acceptable as the code is buggy.
Note: this differs from shared_ptr<void>
, as that uses type erasure to keep track of the original type of pointer passed in.