[Bug c++/16299] New: copy constructor not being called

r_winter at optusnet dot com dot au gcc-bugzilla@gcc.gnu.org
Wed Jun 30 15:44:00 GMT 2004


The following code: 
 
#include <iostream> 
 
class Me 
{ 
  public: 
    int a, b; 
  
    Me()  
        : a(1), b(2) 
    { 
        std::cout << "Me::Me"  << std::endl;  
    } 
     
    Me(const Me &me) 
    { 
        std::cout << "Me::Me(Me)" << std::endl; 
        a = me.b; 
        b = me.a; 
    } 
 
    ~Me() 
    {  
        std::cout << "Me::~Me" << std::endl; 
    } 
}; 
 
Me myMe() 
{ 
    Me m; 
    std::cout << "a = " << m.a << ", b = " << m.b << std::endl; 
    return m; 
} 
 
int main() 
{ 
    Me m(myMe()); 
    std::cout << "a = " << m.a << ", b = " << m.b << std::endl; 
} 
 
generates the following output with GCC (redhat 9.0 GCC 3.2.2 + gentoo GCC 
3.3.3, "gcc -lstdc++ main.cpp"): 
Me::Me 
a = 1, b = 2 
a = 1, b = 2 
Me::~Me 
 
whilst with visual studio .net (2003) it produces: 
Me::Me 
a = 1, b = 2 
Me::Me(Me) 
Me::~Me 
a = 2, b = 1 
Me::~Me 
 
I would think that Microsoft is correct (god forbid) here. GCC seems to be 
doing some weird variant of return value optimisation but getting it wrong.  
 
Optimisation flags have no effect.

-- 
           Summary: copy constructor not being called
           Product: gcc
           Version: 3.3.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: r_winter at optusnet dot com dot au
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16299



More information about the Gcc-bugs mailing list