From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x329.google.com (mail-ot1-x329.google.com [IPv6:2607:f8b0:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id BE16B3852C4E for ; Mon, 21 Nov 2022 14:17:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BE16B3852C4E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ot1-x329.google.com with SMTP id cn2-20020a056830658200b0066c74617e3dso7394481otb.2 for ; Mon, 21 Nov 2022 06:17:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=mjCt90aJp1yMY00t+7K+Mc1FHxgD8gH7KfBEwtLe7U8=; b=UFonCWcVZ616zWocnSPgOnnULWGw3eNwCuYdlrRLa7Qg6KFgZ6D8IiJ1N7I/JrSSIp RFBssk1HuKu8MznR+6GUdyRNJaH5aCmlOf7GYYHl674nEfjvoQZChDkixKbD4+Pz395K FDVSkAd5GJlNEdw7Wu2U/aXwHHt03/1por9eIjq54OSAI/qtgg/+Gqldct6/8OeCFY/T e1mqVVGzia27J0voL9jrGL5sKo72PMbK7yL6GNyrQ4cJsUddknZgw5FQ87IkQZQ+Av5b 6Mn98JFDHFrgmHXfGp/uB2q6Llh5iOtHIYN+NxEwr9BsvwtCJ6EBvmCc/e5qgHmF18JD nrIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=mjCt90aJp1yMY00t+7K+Mc1FHxgD8gH7KfBEwtLe7U8=; b=WldbEA88BNyNw1rQB0csNbiMo5gfawLAeuGNOq2R9pJJ3DrN23XngD5VaHupye4ZQl ZFx1BUhghU0/Z6Hrgn0xj71LdOnFK2susZrycIW+gZUc7GOP/u3U0apc/mV9XlHnOAyU m+Tl3cVWZ5vpUwEqHIBXrAWn5YTo6VTF8N0/dDCsxMK8IkyxI2bDL4gmZK9CmRwRzhbR 4YNIz4MVv/Wggo0RQMcSXpnmgP/XeIxOfj20phAkLxAJXdBVHVvlHJnvdDcZa0w9cWLZ BLOC+w5oMiztj9f/n49SXjoCS2sCQtOtyZyjw0sjTFMUVxPRVD2BlB5iVOhwAmbLxejA Fppw== X-Gm-Message-State: ANoB5pmAwKoAlyItkLGS9OcG6NPW6rRbcJg2MXLPe08gpUS3/bKZhVdF xLEL1WE+A1VFuuM6Cr9d5l1SVuIF27MVxjmnsA== X-Google-Smtp-Source: AA0mqf62AtuxAznnz9gP/y724eSZ9APBRLQayCgzUlwAO/JjMkl5K7NU9KUdOATyqB3pq7/B7Q0uZkIWHlfQXATLy0A= X-Received: by 2002:a9d:5f13:0:b0:66c:753a:844d with SMTP id f19-20020a9d5f13000000b0066c753a844dmr3657107oti.285.1669040276993; Mon, 21 Nov 2022 06:17:56 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Yubin Ruan Date: Mon, 21 Nov 2022 22:17:45 +0800 Message-ID: Subject: Re: Guaranteed copy elision To: Jonathan Wakely Cc: gcc-help Content-Type: multipart/alternative; boundary="000000000000d3794c05edfbb80f" X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000d3794c05edfbb80f Content-Type: text/plain; charset="UTF-8" On Sat, Nov 19, 2022 at 6:12 PM Jonathan Wakely wrote: > > > On Tue, 15 Nov 2022, 05:48 Yubin Ruan via Gcc-help, > wrote: > >> Hi, >> >> As mentioned in cppreference: >> https://en.cppreference.com/w/cpp/language/copy_elision >> >> it is guaranteed in c++17 that copy elision must be applied for some cases >> like >> >> SomeBigObject SomeBigObject::Factory(...) { >> SomeBigObject local; >> ... >> return local; >> } >> > > No, this is not guaranteed. As the cppreference page explains, it's only > guaranteed for copying temporaries. This is not a temporary. > > Elision here is allowed, but not required. It's commonly implemented > though. > > > > >> (examples taken from https://abseil.io/tips/11 ) >> >> but not for cases like >> >> SomeBigObject SomeBigObject::Factory(...) { >> SomeBigObject local1; >> SomeBigObject local2; >> ... >> >> if (cond_1) { >> return local1; >> } else { >> return local2; >> } >> } >> >> For a c++ user, it is somewhat difficult to be 100% sure that copy elision >> / NVO is applied to the functions' return value above. >> >> To be sure that a object would not be copied, we usually write something >> like >> >> SomeBigObject obj; >> func(&obj); >> >> while in most of the cases a one-liner like >> >> SomeBigObject obj = func(); >> >> would suffice. >> >> Is there any language facility to help us guarantee that at compile time >> (such as some kind of static_assert() ) so that we can be confident >> writing >> those one-liner ? >> > > Make it cheap to move, and then it will be moved instead of copied. The > move isn't always elided, but if it's cheap then the code will still be > efficient. > Thank you all. It seems that currently the only way to guarantee efficiency is to use move instead. -- Yubin --000000000000d3794c05edfbb80f--