From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id A5DEF3851AB7 for ; Fri, 8 Jul 2022 21:17:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A5DEF3851AB7 Received: by mail-ej1-x636.google.com with SMTP id o25so39684225ejm.3 for ; Fri, 08 Jul 2022 14:17:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=AFRLJUAidfw3uclhbBtXNZQvyM4Q5hfo8tVJ0Lo3MGA=; b=KJGryH4ZmPhlq7e2VOkG+ud5GqWkXwXfyJFDapSFo0ihrZeRCL2RAEOQfpJc6rmuMj vzzauXm4nkf946reFix6DVdvfpiuMjqDbic/QTDuKlqfr6FhYh9nXiLhQfFRcfnatKad tBFUbp0Cs59RFQaJVtBPpnvMBH1n782JLt4hpODGfKXDB4J9sACfRWuYcuIgn8lWsyn1 iKYAdJaxp2oM7U4DKC5I7aabQK3YQ92f3jS2eaJWiKsvnd3xCf0C4DfrVPLBKp6yl3m/ 52FiwuK9jF7Sk9lZvLLJxor5V5TJgxH5S05CBVUTjXHDbTUGLnQJbf/ymYcI9okA22pn aBgA== X-Gm-Message-State: AJIora88VRP82bExJbXCJK8J1iFd9d+aujW8EOdrTR67aeA6k6div/qo R5rQWIPbOFIVDKVCb4T4T889WGMCp62zL+3d/a8= X-Google-Smtp-Source: AGRyM1v36gwwlT6ckqPJcplkLtaOaXvQ1aPDyeaDqv1uHLaLP290qWw/8LNG2fXKSMYxJ45r2XYu1uU/mDBbYIGxKkQ= X-Received: by 2002:a17:907:7627:b0:72a:9098:e4d3 with SMTP id jy7-20020a170907762700b0072a9098e4d3mr5470687ejc.389.1657315027424; Fri, 08 Jul 2022 14:17:07 -0700 (PDT) MIME-Version: 1.0 References: <20220708204651.42624-1-dmalcolm@redhat.com> In-Reply-To: <20220708204651.42624-1-dmalcolm@redhat.com> From: Jonathan Wakely Date: Fri, 8 Jul 2022 22:16:56 +0100 Message-ID: Subject: Re: [RFC] Using std::unique_ptr and std::make_unique in our code To: David Malcolm Cc: "gcc@gcc.gnu.org" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Jul 2022 21:17:13 -0000 On Fri, 8 Jul 2022 at 21:47, David Malcolm via Gcc 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 arrays, it's trivial: template inline typename std::enable_if::value, std::unique_ptr>::type make_unique(Args&&... args) { return std::unique_ptr(new T(std::forward(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.