Search code examples
c++qnxauto-ptr

auto_ptr not working as expected - Compile error



MASSIVE EDIT:

Following juanchopanza advice, I managed to get this minimal example:

#include <memory>

struct a{
    int b;
};

int main()
{

    typedef std::auto_ptr<a> ArgAutoPtr;

    ArgAutoPtr floatingArg;

    floatingArg = ArgAutoPtr( new a );

}

Which gives me the error:

no match for 'operator=' in 'm_floatingArg = std::auto_ptr<a>(((a*)operator new(4u)))'

QNX 6.4.1 with GCC 4.3.3

EDIT

I managed to compile it like this. Does this work as expected or will generate... whatever evil auto_ptr generates?

ArgAutoPtr floatingArg2 = ArgAutoPtr( new a );
floatingArg = floatingArg2;

Solution

  • The expression ArgAutoPtr( new a ) is creating a temporary auto_ptr.

    auto_ptr::operator= takes a non-const reference to its argument, unlike every other example of operator=. A non-const reference cannot be bound to a temporary.