1 // GROUPS passed copy-ctors
2 /*
3 This report is for GCC 2.3.3 running on a Sun/4.  The bug is that when
4 a class instance is passed-by-value, GCC does not correctly copy the value.
5 At the end of this report is an example program that demonstrates the bug.
6 It should print:
7 
8 	construct A('x')
9 	copy A('x')
10 	destruct A('x')
11 	destruct A('x')
12 
13 and in fact does for IBM's xlC C++.  However, for GCC 2.3.3, it fails
14 to print the second line ["copy A('x')"], which indicates that it failed
15 to call the copy-constructor for class A when it should have.  Below is a
16 typescript that lists the program, shows how I compiled it, and shows the
17 incorrect output.
18 */
19 
20 extern "C" int printf (const char *, ...);
21 extern "C" void exit (int);
22 
23 int count = 0;
24 
25 void
die(int x)26 die (int x)
27 {
28   if (x != ++count)
29     {
30       printf ("FAIL\n");
31       exit (1);
32     }
33 }
34 
35 class A { // Class with explicit & instrumented copy-constructor and destructor.
36 public:
37     const char * id;
A(const char * id1)38     A( const char * id1 ) : id(id1) { die (1); }
39 
40     // Copy constructor
A(const A & a)41     A( const A& a ) : id(a.id) { die (2); }
42 
43     // Destructor
~A()44     ~A() { count++; if (count != 3 && count != 4) die (-1); }
45 };
46 
47 class X { // Class without explicit copy-constructor
48 private:
49     A a;
50 public:
X(const char * id)51     X( const char * id ) : a(id) {}
52 };
53 
Func(X x)54 void Func( X x ) {      // Function with call-by-value argument
55 }
56 
57 int
main()58 main() {
59     X x("x");           // Construct instance of x.
60 
61     // The next line should call the copy-constructor for X since x is
62     // being passed by value.  For GCC 2.3.3 on a Sun/4, it does not.
63     Func(x);
64 
65     printf ("PASS\n");
66     return 0;
67 }
68