From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24359 invoked by alias); 3 Jul 2005 13:44:39 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 24298 invoked by uid 22791); 3 Jul 2005 13:44:35 -0000 Received: from smtp13.wxs.nl (HELO smtp13.wxs.nl) (195.121.6.27) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Sun, 03 Jul 2005 13:44:35 +0000 Received: from brain.thuis (ip503d51d6.speed.planet.nl [80.61.81.214]) by smtp13.wxs.nl (iPlanet Messaging Server 5.2 Patch 2 (built Jul 14 2004)) with ESMTP id <0IJ200J8F0U755@smtp13.wxs.nl> for gcc-help@gcc.gnu.org; Sun, 03 Jul 2005 15:44:34 +0200 (CEST) Date: Sun, 03 Jul 2005 13:44:00 -0000 From: Jeroen Wijnhout Subject: Re: copy ctor not called In-reply-to: <5d0f609905070305263875cb57@mail.gmail.com> To: gcc-help@gcc.gnu.org Message-id: <200507031547.33672.Jeroen.Wijnhout@kdemail.net> MIME-version: 1.0 Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: 7BIT Content-disposition: inline User-Agent: KMail/1.8.50 References: <200507022024.10598.Jeroen.Wijnhout@kdemail.net> <5d0f609905070305263875cb57@mail.gmail.com> X-SW-Source: 2005-07/txt/msg00020.txt.bz2 On Sunday 03 July 2005 02:26 pm, Dirk Jagdmann wrote: > The behaviour of gcc is standard compliant, since an assignment in the > same statement as a variable declaration is always treated by the copy > constructor. This is what I expected, yet the copy constructor wasn't called. The copy constructor call was optimized away and -fno-elide-constructors will turn off this optimization. > The mentioned gcc switch > -fno-elide-constructors will work, but on other compilers you'll have > the same behaviour again. Hmm, I get the feeling that the first part of your message is not as you intended. The point is that if I make the copy constructor private, I get a compile error: error: `Foo::Foo(const Foo&)' is private Which suggests that the copy constructor should be called in statements like: A a = 2; So, I'm wondering what the standard actually says. If A a =2; always means A a(2); then I don't see why you would need the copy constructor to be public. I've found the following in the comp.lang.c++.moderated archive: "TITLE: direct and copy initialization UPDATE: In the last case ("T t3 = u;") the user-defined conversion and the T copy constructor must both be called. In CD2 and until the London meeting in July 1997, the compiler was permitted to elide the copy constructor call as long as the copy constructor could have been called (i.e., was accessible). Since July 1997 and in the final draft standard, the compiler may not make this optimization to elide the copy constructor call." To me this says, that I -fno-elide-constructors should be the default behavior. Which it isn't (at least for gcc 3.3.4). best, Jeroen -- Kile -- KDE Integrated LaTeX Environment http://kile.sourceforge.net