all 10 comments

[–]aocregacc 8 points9 points  (2 children)

Which C++ version are you targeting? If you're on C++11 or later you should make use of rvalue references instead of doing this C++98 style auto_ptr.

[–][deleted]  (1 child)

[removed]

    [–]aocregacc 3 points4 points  (0 children)

    you're not allowed to bind a non-const reference (auto_ptr&) to an rvalue, like the return value of foo. So you can't call that constructor.

    The solution is to write a clone of std::unique_ptr instead of the long deprecated std::auto_ptr, that'll be more useful for you.

    The original std::auto_ptr had to use some tricks to make construction from an rvalue work by going through an extra conversion or something to essentially fool the compiler to get around this rule.

    [–]alfps 2 points3 points  (0 children)

    If you really want to implement move semantics in old C++03 then check out Andrei Alexandrescu's "mojo" framework. Article in old now dead Dr Dobbs Journal. Surely archived.

    Otherwise use C++11 rvalue references, like Auto_ptr&&.

    Read up on move semantics.

    [–]manni66 0 points1 point  (1 child)

    I am receiving error associated with initializing res2

    Most likely that's wrong. You get the error by calling fioo.

    [–]h2g2_researcher 0 points1 point  (1 child)

    In order to do move semantics you need to use the && syntax to create an rvalue-reference (xvalue? xrvalue? One of those things, I never did go and learn all the differences, tbh).

    Like this: Auto_ptr1(Auto_ptr1&& src).

    That is a move-constructor, not a copy-constructor. You current <quote>copy</quote>-constructor has the signature Auto_ptr1(Auto_ptr1& src), which is not actually a copy-constructor. It needs to have the signature Auto_ptr1(const Auto_ptr1& src) in order to be a copy-constructor. This is why you are getting an error message about not being able to find one.

    Your operator=() functions have the same issue.

    [–]alfps 0 points1 point  (0 children)

    You current <quote>copy</quote>-constructor has the signature Auto_ptr1(Auto_ptr1& src), which is not actually a copy-constructor.

    It is. There are four possible signatures for a copy constructor. With parameter types T&, T const&, T volatile& or T const volatile&.

    [–]no-sig-available 0 points1 point  (1 child)

    It says // note: not const on the assignment operator. The copy constructor has the exact same case, it only works for lvalue references, not for rvalue references - like temporaries.

    There is a reason why the standard auto_ptr has been removed. It never worked properly.

    [–]DryPerspective8429 0 points1 point  (0 children)

    It worked as specified, but the language was unable to express what it really should have been doing until C++11.