public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c++/5995: double call of copy constructor
@ 2002-03-18  8:58 jason
  0 siblings, 0 replies; 3+ messages in thread
From: jason @ 2002-03-18  8:58 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, jason, martin.gerbershagen

Synopsis: double call of copy constructor

State-Changed-From-To: analyzed->closed
State-Changed-By: jason
State-Changed-When: Mon Mar 18 08:58:14 2002
State-Changed-Why:
    duplicate of 4222.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=5995


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: c++/5995: double call of copy constructor
@ 2002-03-18  6:27 jason
  0 siblings, 0 replies; 3+ messages in thread
From: jason @ 2002-03-18  6:27 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, jason, martin.gerbershagen, nobody

Synopsis: double call of copy constructor

Responsible-Changed-From-To: unassigned->jason
Responsible-Changed-By: jason
Responsible-Changed-When: Mon Mar 18 06:27:17 2002
Responsible-Changed-Why:
    x
State-Changed-From-To: open->analyzed
State-Changed-By: jason
State-Changed-When: Mon Mar 18 06:27:17 2002
State-Changed-Why:
    yep, that's a bug.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=5995


^ permalink raw reply	[flat|nested] 3+ messages in thread

* c++/5995: double call of copy constructor
@ 2002-03-18  3:16 martin.gerbershagen
  0 siblings, 0 replies; 3+ messages in thread
From: martin.gerbershagen @ 2002-03-18  3:16 UTC (permalink / raw)
  To: gcc-gnats


>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 <iostream.h>

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;
}


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-03-18 16:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-18  8:58 c++/5995: double call of copy constructor jason
  -- strict thread matches above, loose matches on Subject: below --
2002-03-18  6:27 jason
2002-03-18  3:16 martin.gerbershagen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).