public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/68350] std::uninitialized_copy overly restrictive for trivially_copyable types
Date: Thu, 24 Feb 2022 12:19:53 +0000	[thread overview]
Message-ID: <bug-68350-4-PMCMCawDyo@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-68350-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68350

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|ville.voutilainen at gmail dot com |redi at gcc dot gnu.org

--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Barry Revzin from comment #8)
> Whereas the copy_b case could also use memmove.

I'd been working on the assumption that it can't, because for types that aren't
trivially default constructible we need a constructor to begin the lifetime.
But the rules for implicit-lifetime class types only require at least one
trivial constructor and a trivial destructor, so a trivial copy constructor is
enough. And memcpy/memmove implicitly create objects in the destination
storage.

I don't think that was really explicit before the implicit-lifetime rules in
C++20. The example in C++17 [basic.types] p3 implies that the destination
doesn't need to be an initialized object, but nothing said that normatively.
Quite the opposite: C++17 [basic.life] seems clear that initialisation must be
done for types with non-vacuous initialization (such as Barry's type B), and
nothing in C++17 said that memcpy/memmove do any initialization. C++20 is clear
though.

So:

- If the input and output type are the same size and both are trivially
  constructible, we can use memcpy. We should call memcpy directly from
  std::uninitialized_copy rather than via std::copy, because std::copy doesn't
  allow overlapping ranges, and has to use memmove instead.

- If the output type is trivially default constructible and is assignable from
  the input type, we can use std::copy (but it's unclear whether that gives any
  benefit if we can't turn it into memcpy/memmove).

- Otherwise, just use std::construct_at on each element.

I have a patch, but it's for stage 1 not GCC 12.

  parent reply	other threads:[~2022-02-24 12:19 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-68350-4@http.gcc.gnu.org/bugzilla/>
2020-04-09  9:31 ` redi at gcc dot gnu.org
2022-02-23 22:03 ` barry.revzin at gmail dot com
2022-02-24 12:19 ` redi at gcc dot gnu.org [this message]
2022-02-24 14:20 ` redi at gcc dot gnu.org
2022-02-24 14:45 ` redi at gcc dot gnu.org
2022-02-24 14:49 ` arthur.j.odwyer at gmail dot com
2022-02-25  9:43 ` redi at gcc dot gnu.org
2023-04-26  6:55 ` rguenth at gcc dot gnu.org
2023-07-27  9:21 ` rguenth at gcc dot gnu.org
2024-05-21  9:10 ` jakub at gcc dot gnu.org

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=bug-68350-4-PMCMCawDyo@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).