Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart pointers with functions returning a pointer in an argument

This question was discussed a few times but all those discussions almost a decade old and there was no good solution. The question is the same.

We have smart pointers, like unique_ptr, that is great. We have tons of C functions returning a pointer to an object as an argument which later needs to be released.

Let's say this is the function:

int CreateSomething(OBJECT_TYPE** pObject);

So, what would we normally do without smart pointers? Apparently something like that:

OBJECT_TYPE* pObject;
if(CreateSomething(&pObject) == 0) {
    // Use the pObject pointer
    delete pObject;
}

Now, I would like to rewrite it using smart pointers, and I would like it to be as simple as this:

unique_ptr<OBJECT_TYPE> pObject;
if(CreateSomething(&pObject) == 0) {
    // Use the pObject pointer
}

Technically, that would be quite possible if unique_ptr would have T** operator&, and if it would count on this kind of workflow, but it doesn't.

In the case when we want to use a smart pointer here, we have to declare a regular pointer, use it in that function, and then reassign it to the smart pointer. But those extra steps can and should be eliminated, and I hope that maybe there is already an implementation which would allow me to do what I need in that simple manner as I showed.

Yes, I can write my own smart pointer or mediator, which would simplify the usage of unique_ptr, but first I'd like to ask if maybe there is already something implemented in the depth of standard libraries for this and I simply overlooked it?

like image 892
Alex Avatar asked Jul 23 '26 10:07

Alex


1 Answers

C++23 has very recently added std::out_ptr for exactly this use case.

like image 194
Davis Herring Avatar answered Jul 25 '26 03:07

Davis Herring