From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: David Malcolm <dmalcolm@redhat.com>
Cc: "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Re: [RFC] Using std::unique_ptr and std::make_unique in our code
Date: Fri, 8 Jul 2022 22:16:56 +0100 [thread overview]
Message-ID: <CAH6eHdSnGwtScODMveYha1S5WiDo6YsexN_pRqe9n4vq-Pmkig@mail.gmail.com> (raw)
In-Reply-To: <20220708204651.42624-1-dmalcolm@redhat.com>
On Fri, 8 Jul 2022 at 21:47, David Malcolm via Gcc <gcc@gcc.gnu.org> wrote:
>
> std::unique_ptr is C++11, and I'd like to use it in the gcc/analyzer
> subdirectory, at least. The following patch eliminates a bunch of
> "takes ownership" comments and manual "delete" invocations in favor
> of simply using std::unique_ptr.
>
> The problem is that the patch makes use of std::make_unique, but that
> was added in C++14.
>
> I've heard that it's reasonably easy to reimplement std::make_unique,
> but I'm not sure that my C++11 skills are up to it.
You know we have an implementation of std::make_unique in GCC, with a
GCC-compatible licence that you can look at, right? :-)
But it's not really necessary. There are only two reasons to prefer
make_unique over just allocating an object with new and constructing a
unique_ptr from it:
1) avoid a "naked" new in your code (some coding styles like this, but
it's not really important as long as the 'delete' is managed
automatically by unique_ptr).
2) exception-safety when allocating multiple objects as args to a
function, see https://herbsutter.com/gotw/_102/ for details.
Irrelevant for GCC, because we build without exceptions.
> Is there:
> (a) an easy way to implement a std::make_unique replacement
> (e.g. in system.h? what to call it?), or
If you don't care about using it to create unique_ptr<T[]> arrays, it's trivial:
template<typename T, typename... Args>
inline typename std::enable_if<!std::is_array<T>::value,
std::unique_ptr<T>>::type
make_unique(Args&&... args)
{ return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); }
To add the overload that works for arrays is a little trickier.
> (b) some C++11-compatible way to do the same thing?
> without accidentally bringing in C++14 features.
std::make_unique doesn't use any C++14 features.
next prev parent reply other threads:[~2022-07-08 21:17 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-08 20:46 David Malcolm
2022-07-08 21:15 ` Gabriel Ravier
2022-07-08 21:16 ` Jonathan Wakely [this message]
2022-07-11 10:56 ` Pedro Alves
2022-07-12 0:32 ` David Malcolm
2022-08-10 1:15 ` James K. Lowden
2022-07-12 10:21 ` Florian Weimer
2022-07-12 10:45 ` Jonathan Wakely
2022-07-12 11:01 ` Pedro Alves
2022-07-12 11:00 ` Pedro Alves
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=CAH6eHdSnGwtScODMveYha1S5WiDo6YsexN_pRqe9n4vq-Pmkig@mail.gmail.com \
--to=jwakely.gcc@gmail.com \
--cc=dmalcolm@redhat.com \
--cc=gcc@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).