From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate.crashing.org (gate.crashing.org [63.228.1.57]) by sourceware.org (Postfix) with ESMTP id 651973854551 for ; Fri, 18 Nov 2022 21:59:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 651973854551 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=kernel.crashing.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kernel.crashing.org Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 2AILw42U031493; Fri, 18 Nov 2022 15:58:04 -0600 Received: (from segher@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id 2AILw4m6031492; Fri, 18 Nov 2022 15:58:04 -0600 X-Authentication-Warning: gate.crashing.org: segher set sender to segher@kernel.crashing.org using -f Date: Fri, 18 Nov 2022 15:58:04 -0600 From: Segher Boessenkool To: Martin Oberzalek Cc: gcc-help@gcc.gnu.org Subject: Re: Guaranteed copy elision Message-ID: <20221118215804.GC25951@gate.crashing.org> References: <32119174b061d5bf07e3d39063709f852ff5ca5f.camel@gmx.at> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <32119174b061d5bf07e3d39063709f852ff5ca5f.camel@gmx.at> User-Agent: Mutt/1.4.2.3i X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,SPF_HELO_PASS,SPF_PASS,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Nov 18, 2022 at 10:35:20PM +0100, Martin Oberzalek via Gcc-help wrote: > Am Freitag, dem 18.11.2022 um 16:05 +0100 schrieb Stefan Ring via Gcc- > help: > > On Tue, Nov 15, 2022 at 6:48 AM Yubin Ruan via Gcc-help > > wrote: > > > 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 ? > With c++ language features this may is a solution: Nope. Try with -O0 for example. There is no way to guarantee copy elision. It isn't even clear what *exactly* you want guaranteed, what "copy elision" means *exactly*, what "guaranteeing copy elision" means, etc. If you write clear and simple (which means: not artificially made complex) and correct source code, you can trust the compiler will generate good machine code for you (if you use -O2 or such). If not, bug reports are welcome! A C compiler is not a "portable assembler", you have no direct control over generated code. This is a good thing: the compiler is much better at writing fast machine code than users are. Segher