Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing the address of dereferenced smart pointers to functions that expect raw pointers

(assuming that I am working with a library or framework that expects usage of raw pointers,)

Is it valid practice to use a smart pointer which owns some data, and then pass the address of the derefenced smart pointer to a function that expects a raw pointer?

like image 290
Max-Florian Luchterhand Avatar asked Nov 17 '20 10:11

Max-Florian Luchterhand


2 Answers

Yes, that is valid practice. The std smart pointers have a get() member function exactly for that purpose.

In general, when you manage an object through smart pointers, you should only pass the whole smart-pointer-object as is to other functions when these functions imply ownership semantics: if a function will copy a std::shared_ptr, it should accept it by value. Similar for std::unique_ptr. More often than that, a function doesn't have anything to do with ownership, it just wants to act on data and/or behavior passed into it. Then, your first choice should be to take a (const-qualified) reference, because it doesn't have the additional nullptr-state of pointers. Otherwise, a pointer is just fine.

Long story short: if you deal with an API that accepts raw pointers and doesn't do any ownership-related actions on it (delete it, copy the pointee), then it's fine to pass .get() to it.

like image 173
lubgr Avatar answered Sep 21 '22 14:09

lubgr


As long as the function doesn't expect to take ownership of the data, definitely.

In fact, that is also how you should design your own functions: use a smart pointer in an interface if, and only if, it should participate in the ownership of the pointee.

like image 45
Quentin Avatar answered Sep 20 '22 14:09

Quentin