From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 113723 invoked by alias); 9 Oct 2017 21:12:45 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 111557 invoked by uid 89); 9 Oct 2017 21:12:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy=earth, H*M:5722 X-HELO: simark.ca Received: from simark.ca (HELO simark.ca) (158.69.221.121) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 09 Oct 2017 21:12:43 +0000 Received: from [10.0.0.11] (cable-192.222.251.162.electronicbox.net [192.222.251.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 4E9C11E517; Mon, 9 Oct 2017 17:12:41 -0400 (EDT) Subject: Re: [RFC][13/19] Target FP: Perform Ada fixed-point scaling in target format To: Joel Brobecker , Ulrich Weigand Cc: gdb-patches@sourceware.org References: <20171009163008.5svu3sjyeubqaux7@adacore.com> <20171009165830.1607ED83320@oc3748833570.ibm.com> <20171009180952.xzg36frnnumvkrt5@adacore.com> From: Simon Marchi Message-ID: <4543871f-9a5a-5722-b868-706948b3f3ad@simark.ca> Date: Mon, 09 Oct 2017 21:12:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <20171009180952.xzg36frnnumvkrt5@adacore.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-SW-Source: 2017-10/txt/msg00237.txt.bz2 On 2017-10-09 02:09 PM, Joel Brobecker wrote: >>> This is a C++ question, really. Does it make any difference if you >>> declare the std::string first, and then only set its contents in >>> a second statement? I can't remember the details, but it has to do >>> with initialization vs assignment. I _really_ hope that the string >>> class is sufficiently well designed that the two are really equivalent >>> in practice? >> >> Huh. Indeed I see worse code when doing the assignment as a separate >> statement, at least with GCC 4.8. I'll make sure to use initialization >> instead wherever possible. Thanks for pointing this out! > > Let's wait for people who really know better about C++ to tell us > whether it makes a difference. I was amazed as how careful you have > to be when using C++ to avoid inefficiencies, but perhaps I am simply > being paranoid in this case... That's why I tried to phrase this as > a question. > Indeed, it's preferable to use std::string foo = returns_string (); than std::string foo; foo = returns_string (); The reason being that in the second case, the default constructor (the one with no params) is called to construct an empty string, and then that work is scrapped because we assign a new value. The first form constructs the string right away with the right content. While it's preferable to use the first one, the earth wouldn't stop spinning if you used the second form. In the particular case of std::string, the default constructor is basically assigning a few fields. Simon