Functions returning objects do not call the copy constructors for permanent
objects. The program below works for the Solaris and Microsoft compilers, but
not for the GCC compiler.  

I’m using:
-bash-3.00$ gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)

#include "stdio.h"

struct S
{
      S()            {  printf( "Inside default constructor \n" );  }
      S( const S & ) {  printf( "Inside copy constructor \n" );  } 
};

S  f( void )
{
      S s1;
      return s1;
}

int main ( int, char ** )
{
      S  s1;
      S  s2( s1 );  // must call the copy constructor
      S  s3 = f();  // must call the copy constructor
      S  s4( f() ); // must call the copy constructor
      return 0;
}

#if 0
      //  GCC output is:
      Inside default constructor 
      Inside copy constructor 
      Inside default constructor 
      Inside default constructor 

      //  Both Solaris and Microsoft output is:
      Inside default constructor 
      Inside copy constructor 
      Inside default constructor 
      Inside copy constructor 
      Inside default constructor 
      Inside copy constructor
#endif


-- 
           Summary: The copy constructor is not being called upon return
                    from function calls.
           Product: gcc
           Version: 3.4.6
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: SSacek at appsecinc dot com
 GCC build triplet: gcc version 3.4.6
  GCC host triplet: Red Hat 3.4.6-3


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

Reply via email to