[PATCH] assert that deleting by pointer to base in unique_ptr does not cause UB

Ville Voutilainen ville.voutilainen@gmail.com
Wed Sep 22 17:22:58 GMT 2021


On Wed, 22 Sept 2021 at 20:09, Antony Polukhin via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> std::unique_ptr allows construction from std::unique_ptr of derived
> type as per [unique.ptr.single.asgn] and [unique.ptr.single.ctor]. If
> std::default_delete is used with std::unique_ptr, then after such
> construction a delete is called on a pointer to base. According to
> [expr.delete] calling a delete on a non similar object without a
> virtual destructor is an undefined behavior.
>
> This patch turns that undefined behavior into static assertions inside
> std::unique_ptr.

I don't understand the sizeof(_Tp) == sizeof(_Up) part in the
static_assert. I fail to see how
a same-size check suggests that the types are similar enough that a
delete-expression works.


More information about the Libstdc++ mailing list