[cfe-dev] Parser Stmt/Expr Owning Pointer

Sebastian Redl sebastian.redl at getdesigned.at
Tue Dec 9 08:16:04 PST 2008


Howard Hinnant wrote:
> On Dec 9, 2008, at 5:05 AM, Sebastian Redl wrote:
>
>   
>> Also, can I trick your unique_ptr to give up its raw pointer after  
>> it has been moved from? For convenience in the transition phase,  
>> that would be invaluable. In my current implementation you can do:
>>
>> void takesOwnershipButHasNotBeenConvertedToBeSmart(ExprTy*);
>>
>> ExprOwner ptr(getExprFromSomewhere());
>> takesOwnershipButHasNotBeenConvertedToBeSmart(ptr.move());
>>
>> The important part is that it's always move(), even after converting  
>> the function to take an ExprOwner.
>>     
>
> I'm not positive I understand your question 100%, but I'll try a  
> shotgun answer.  If I don't hit the target, just ask again. :-)
>   
In terms of C++0x rvalue references, I've basically implemented an 
implicit conversion from "unique_ptr<void, ASTDeleter>&&" to void* that 
is equivalent to release(). Obviously, this is not possible in C++0x, 
but since my rvalue reference is a special object, it's very easy.

The advantage of this is that I can pass my pointer's mover to a 
function that is updated to take my pointer as easily as I can pass it 
to a function that still takes a raw pointer, with no change in the 
caller's syntax. Since the update is the business of the callee, I don't 
see this as unsafe, I see it as convenient.
We're not redefining ownership here, we're just trying to enforce the 
existing rules through the type system. So the caller moves the pointer 
to the callee, no matter how the callee decides to handle it further.

> I'll try to get it in soon.  I'm not yet positive what "in" means.
For starters, you could send me a link. :-) I'm more interested in the 
technique, though. Using unique_ptr directly is difficult because we 
need to store the success flag alongside the pointer (and if I wrap 
unique_ptr, I need to write all the emulation stuff again anyway).

> The design is pretty much taken from auto_ptr.  It is a painful but  
> effective means of installing move semantics into a class.
But auto_ptr doesn't work with temporaries, does it? I've never used it, 
really. I'm quite wary of something that has such an incredibly broken 
MS implementation.

Hm, a small example shows that it does work. Tricky business, that.

Sebastian



More information about the cfe-dev mailing list