From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17775 invoked by alias); 18 Mar 2002 11:16:06 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 17723 invoked by uid 71); 18 Mar 2002 11:16:03 -0000 Resent-Date: 18 Mar 2002 11:16:03 -0000 Resent-Message-ID: <20020318111603.17722.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-To: nobody@gcc.gnu.org Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, martin.gerbershagen@icn.siemens.de Received:(qmail 14470 invoked by uid 61); 18 Mar 2002 11:11:12 -0000 Message-Id:<20020318111112.14467.qmail@sources.redhat.com> Date: Mon, 18 Mar 2002 03:16:00 -0000 From: martin.gerbershagen@icn.siemens.de Reply-To: martin.gerbershagen@icn.siemens.de To: gcc-gnats@gcc.gnu.org X-Send-Pr-Version:gnatsweb-2.9.3 (1.1.1.1.2.31) Subject: c++/5995: double call of copy constructor X-SW-Source: 2002-03/txt/msg00635.txt.bz2 List-Id: >Number: 5995 >Category: c++ >Synopsis: double call of copy constructor >Confidential: no >Severity: serious >Priority: medium >Responsible: unassigned >State: open >Class: wrong-code >Submitter-Id: net >Arrival-Date: Mon Mar 18 03:16:01 PST 2002 >Closed-Date: >Last-Modified: >Originator: Martin Gerbershagen >Release: g++-3.0.3 >Organization: >Environment: sparc-sun-solaris2.6 >Description: The attached test program x.C generates the following output: O::O() o1(getOcopy()) O::O(const O& s) O::O(const O& s) O::~O() o2(getOref()) O::O(const O& s) &o3(getOref()) o4 = getOref() O::O(const O& s) &o5 = getOcopy() O::O(const O& s) o6 = getOcopy() O::O(const O& s) o7 = getOcopy() O::O() O::O(const O& s) O::operator= O::~O() o7 = getOref() O::operator= (void)getOcopy() O::O(const O& s) O::~O() end O::~O() O::~O() O::~O() O::~O() O::~O() O::~O() O::~O() The output shows, that the copy constructor of O is called twice during the initialization of o1. This should not be the case and can result in performance degradation, if the class used has complex members. The test program works properly, if it is compiled with g++ 2.95.3. >How-To-Repeat: g++ -O x.C && a.out >Fix: >Release-Note: >Audit-Trail: >Unformatted: ----gnatsweb-attachment---- Content-Type: text/plain; name="x.C" Content-Disposition: inline; filename="x.C" #include class O { public: O& operator=(const O&); O(const O&); O(); ~O(); }; O& O::operator=(const O& s) { cout << "O::operator=" << endl; if (this != &s) { } return *this; } O::O(const O& s) { cout << "O::O(const O& s)" << endl; } O::O() { cout << "O::O()" << endl; } O::~O() { cout << "O::~O()" << endl; } class A { O o; public: O getOcopy() const; const O& getOref() const; A& operator=(const A&); A(const A&); A(); ~A(); }; inline O A::getOcopy() const { return o; } inline const O& A::getOref() const { return o; } A& A::operator=(const A& s) { if (this != &s) { o = s.o; } return *this; } A::A(const A& s): o(s.o) { } A::A() { } A::~A() { } int main() { A a; cout << "o1(getOcopy())" << endl; O o1(a.getOcopy()); cout << "o2(getOref())" << endl; O o2(a.getOref()); cout << "&o3(getOref())" << endl; const O &o3(a.getOref()); cout << "o4 = getOref()" << endl; O o4 = a.getOref(); cout << "&o5 = getOcopy()" << endl; const O &o5 = a.getOcopy(); cout << "o6 = getOcopy()" << endl; O o6 = a.getOcopy(); cout << "o7 = getOcopy()" << endl; O o7; o7 = a.getOcopy(); cout << "o7 = getOref()" << endl; o7 = a.getOref(); cout << "(void)getOcopy()" << endl; a.getOcopy(); cout << "end" << endl; return 0; }