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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…