Sign in to follow this  
Storyyeller

shared pointer temporary error

Recommended Posts

Storyyeller    215
Why does this work
            unarydbl_sptr temp2(GetCCDcallback(*otherptr, lastx, lasty, newx, newy));
            temp.swap(temp2);


but not this?
    temp.swap(GetCCDcallback(*otherptr, lastx, lasty, newx, newy));

I am really confused. The compiler gives the error no matching function for call to `boost::shared_ptr<boolFunct_dbl>::swap(unarydbl_sptr)' By the way, unarydbl_sptr is a typedef for boost::shared_ptr<boolFunct_dbl>. GetCCDcallback is a function which returns a unarydbl_sptr.

Share this post


Link to post
Share on other sites
visitor    643
It's somewhat unclear what you are after. Why do you want to swap the pointer held by temp with the return value of a function, if you could just use assignment to the same effect?

But the technical reason is that swap takes the argument by non-const reference (as it is going to modify it), and temporaries (such as return values of function calls) cannot be bound to non-const references.

However, you can call methods on temporaries returned from functions, so the following should work:


GetCCDcallback(*otherptr, lastx, lasty, newx, newy).swap(temp);

Share this post


Link to post
Share on other sites
Storyyeller    215
Well what I was originally trying to do was
temp.reset(GetCCDcallback(*otherptr, lastx, lasty, newx, newy));
but that didn't work either.

I have a habit of using reset to assign smart pointers instead of the assignment operator. Is this a bad idea? Is there any reason to choose one or the other?
(Well I suppose I just found out a reason why you shouldn't use reset)

Share this post


Link to post
Share on other sites
visitor    643
As I understand it, reset() is there to allow you to give another naked pointer to the class. When you have a smart pointer, I don't see why you should avoid assignment, as it works correctly and is the default way of assigning something to something.

Share this post


Link to post
Share on other sites
Zahlman    1682
The .swap() member function accepts a non-const reference to another instance (which makes sense; after all, the purpose is to modify the other instance, by causing it to "become" the current one, and vice-versa). You can't pass a temporary by non-const reference. It's a language feature intended to prevent certain kinds of errors. (Basically: the purpose of passing by non-const reference is to make changes to the caller's data, which the caller can then "see". But since a temporary is, well, temporary, there is no way to examine the changes to it. The conclusion is that you might be doing something wrong if you explicitly ask for changes that you can't examine.)

It appears that what you really want to do is set 'temp' to whatever is returned from the callback function (since you are swapping with 'temp2' and then apparently don't care about that variable). To do this, you can just assign (i.e., use the operator=, which has been carefully overloaded for boost::shared_ptr).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this