public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Antony Polukhin <antoshkka@gmail.com>
To: Ville Voutilainen <ville.voutilainen@gmail.com>
Cc: "libstdc++" <libstdc++@gcc.gnu.org>,
	gcc-patches List <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] assert that deleting by pointer to base in unique_ptr does not cause UB
Date: Wed, 22 Sep 2021 20:49:00 +0300	[thread overview]
Message-ID: <CAKqmYPZ8GaexpfyBQanECpABU=GCnm676=NjapK=85w3e1gv1w@mail.gmail.com> (raw)
In-Reply-To: <CAFk2RUZ1yzksH-n8saKzUk2LwCL3LiAWGjjFnQewbU3tFzzibA@mail.gmail.com>

ср, 22 сент. 2021 г. в 20:23, Ville Voutilainen <ville.voutilainen@gmail.com>:
>
> 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.

I used the following logic:
[unique.ptr.single.*] sections have the constraint that
"unique_­ptr<U, E>::pointer is implicitly convertible to pointer".
There's already a static assert that T in unique_ptr<T> is not void,
so U either has to be the same type T, or a type derived from T. If a
derived type adds members, then size changes and types are not similar
as the decompositions won't have the qualification-decompositions with
the same n.

-- 
Best regards,
Antony Polukhin

  reply	other threads:[~2021-09-22 17:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-22 17:08 Antony Polukhin
2021-09-22 17:22 ` Ville Voutilainen
2021-09-22 17:49   ` Antony Polukhin [this message]
2021-09-22 17:53     ` Ville Voutilainen
2021-09-22 17:44 ` Jonathan Wakely
2021-09-22 17:56   ` Antony Polukhin
2021-09-23 10:21     ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKqmYPZ8GaexpfyBQanECpABU=GCnm676=NjapK=85w3e1gv1w@mail.gmail.com' \
    --to=antoshkka@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    --cc=ville.voutilainen@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).