This is the mail archive of the mailing list for the libstdc++ project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: auto_ptr member gives = no matching function for call to :(

Rodolfo Lima wrote:
> Your problem is that auto_ptr doesn't model the CopyConstructible and 
> Assignable concepts, so you have to manually create the copy constructor 
> and assignment operator and properly deal with auto_ptr semantics
> yourself.

Hello, I have a similar problem as above. Except that when I define a copy
constructor, it does not get called!

#include <stdio.h>
#include <memory>

using namespace std;

class a
  int x;
  mutable auto_ptr<int> ptr;

  a( int px=0 )
  { x = px; };

  /*a( const a& )
    printf( "Copy constructor called!\n" );

  a& operator=( const a& )
    printf( "Operator= called!\n" );

  ~a() {};

inline a funca( void )
 a avar;
 return avar;

int main( void )
  a avar = funca();
  printf( "%d\n", avar.x );


Compilation error with commented constructor:
testinit.cpp: In function 'int main()':
testinit.cpp:30: error: no matching function for call to 'a::a(a)'
testinit.cpp:15: note: candidates are: a::a(a&)
testinit.cpp:12: note:                 a::a(int)

After copy constructor and operator= are declared (uncommented):
botik@kerrigan ~/devel/temp $ ./a.out 

I tried it under debug - explicit constructor is not getting called. And the
old pointer in auto_ptr remains and memory gets destroyed leaving me with
random artifacts - if someone gets to use and write to the memory in between
copies, weird things happen.

View this message in context:
Sent from the gcc - libstdc++ mailing list archive at

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]