I have some pool of objects that I maintain in a std::vector<std::unique_ptr<MyObject>>
and I pass objects from this pool to a function void process(...)
. I'm not clear on the best way to pass one of these objects to the process()
function. If I'm understanding the different answers and articles I've read it's best to pass this as a regular reference as follows: void process(MyObject& buffer);
From my understanding this is preferable because:
- The buffer I'm passing in should be guaranteed to remain in existence throughout the lifetime of the
process()
method by virtue of the fact that it's called by the owner of the unique_ptr, i.e. the calling method - the caller can pass stack allocated objects as well so this doesn't impose allocation restrictions on clients of this method
- If the ptrs can be nullptr then it's advisable to pass a raw pointer to the
process()
method instead
Is this understanding or correct or what is the best guidance? Here are the references I've read: Passing const unique_ptr reference as parameter
https://www.chromium.org/developers/smart-pointer-guidelines
https://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/
Aucun commentaire:
Enregistrer un commentaire