From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by sourceware.org (Postfix) with ESMTPS id AA1BC3858D28 for ; Sat, 19 Nov 2022 10:12:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AA1BC3858D28 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-ed1-x52b.google.com with SMTP id v17so10254819edc.8 for ; Sat, 19 Nov 2022 02:12:22 -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=ajCKLs7N7NtDA3tAon8WxrzhI8XH4hIx0Ol4sp00zDA=; b=hGqAl/HAwpI2PPlOarZiqUifxPonENoG6SsmS0rVpGBRUSX3z8jz4/C6Y2XFMUQO4e wUIYOZs0zmmc7E81+hS1Rje/hzD4QsIS32SYsDdsJ/24JazFo2aeHfzqMgEW4GLLsB2q veILU9bw2yYEfmUO5H4Qo6sLnBor7T3rhvwFFAbb6vRyvFxXl/tHtuvWqGDltnOgUp96 hjchPA5P1w0shq2Z6t6EFdqMWRo69mXn1PmNIhKCpgj4t0fBThxdi4oickdv4IL5RZzz g1t/Fqu2+Fpd7PtWnjebWoxRKF6f0+MUKslLRYNZCslHVfm499V0RRssCRTROsN3OVMC mfTg== 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=ajCKLs7N7NtDA3tAon8WxrzhI8XH4hIx0Ol4sp00zDA=; b=3JUUlDHYtXCdbieV3YnC4hAEv/QcGIE82IrweJwOOzldHC7+Ew5mkDcCeya2ppDJeY n0L70O7lRqIVk4Tu9xK2HSeVD/Xvjis+rFwkA/kD4BcARvYhjqKzbiBabhnytGxrVTso N3AjzHjoIJe4KirwFjr8voyKgaYGce0N4WkGzYaTSxw+G5OgJH/1JKYgSsV8gyRONcvo rOiWL7ZET37Rp5ug9QYnFKbAtL6KfFlw06/Umyt6xc20qP+Sadi+BOF3embfRiX9+/b3 skGikGYMttwCOq7zoe4ZVp0lwK2EgodecC1W9t6exla3t2SYvipEb9chsjXRJMyvDjw0 G0pg== X-Gm-Message-State: ANoB5pmyl1uGxeOOTDhUc7EiXqzyxx79tK3dow/U9OWXhePwicJ6A6AW qDyjLZGwMWtjN7zBY501PyTMIfDDaXGYBlpI4n4= X-Google-Smtp-Source: AA0mqf63ST2472UXtskr4EOMD7w9EtoW1Z8KTkpa/xrwMFe1Wq1hWtHLk69QmRO6XchxWqElT5g49XrD8mrWxPKgmpQ= X-Received: by 2002:aa7:dbc7:0:b0:45f:b80f:1fe8 with SMTP id v7-20020aa7dbc7000000b0045fb80f1fe8mr8872197edt.118.1668852741485; Sat, 19 Nov 2022 02:12:21 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Sat, 19 Nov 2022 10:12:08 +0000 Message-ID: Subject: Re: Guaranteed copy elision To: Yubin Ruan Cc: gcc-help Content-Type: multipart/alternative; boundary="000000000000d6b8f005edd00e50" X-Spam-Status: No, score=-0.7 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: --000000000000d6b8f005edd00e50 Content-Type: text/plain; charset="UTF-8" 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. > I know that marking the copy constructor deleted would do the good, but > copy is needed in some cases. > > Thanks > Yubin > --000000000000d6b8f005edd00e50--