egcs-1.1b (possibly later) calls wrong copy constructor.

Carlo Wood carlo@runaway.xs4all.nl
Fri Nov 20 08:39:00 GMT 1998


Hi again,

I believe the following code snippet reveals a bug in egcs-1.1b (didn't test it
with a later version).

The problem:

A provided copy constructor, which is a template in itself, is ignored by the
compiler - and the compiler generated copy constructor is used instead.

I believe this to be wrong based mainly on the fact that this is how
class auto_ptr (versions I could find) are implemented.

~/c++/tests>g++ copycon.cc
~/c++/tests>a.out
Defining auto_ptr<B> x:
  Called auto_ptr(X*)
Defining auto_ptr<B> y(x):
Reached end of main()
  Called ~auto_ptr()
  Called ~auto_ptr()

As you can see, two "auto_ptr" objects are destructed, but only one
constructor that printed out that it was called was called.
My conclusion is that the wrong(?) copy constructor was called: A compiler
generated one.

The copycon.cc program:
==================================================================================
#include <iostream>

template<class X>
class auto_ptr {
  template<class Y> friend class auto_ptr;

public:
  explicit auto_ptr(X*) {
    cerr << "  Called auto_ptr(X*)" << endl;
  }

  // This should be called as copy constructor, right???
  // It is not called...
  template<class Y>
  auto_ptr(const auto_ptr<Y> &) {
    cerr << "  Called template<class Y> auto_ptr(const auto_ptr<Y> &)" << endl;
  }

  ~auto_ptr() {
    cerr << "  Called ~auto_ptr()" << endl;
  }
};

class B {
};

int main(void)
{
  B b;
  cerr << "Defining auto_ptr<B> x:" << endl;
  auto_ptr<B> x(&b);
  cerr << "Defining auto_ptr<B> y(x):" << endl;
  auto_ptr<B> y(x); 
  cerr << "Reached end of main()" << endl;
}
==================================================================================

-- 
 Carlo Wood  <carlo@runaway.xs4all.nl>



More information about the Gcc-bugs mailing list