Reputation: 869
I was looking at the C++ API of Apache's Arrow library, and noticed that it is littered with member functions that take arguments of type std::shared_ptr<T>*
. To me this looks unnecessarily contrived and possibly brittle, and it is frankly strange to me that a library would prescribe how I choose to solve the ownership of my instances of its classes. Thus my conclusion is that there must be some advantages to this approach that I am not aware of, which sparked my curiosity.
What are the advantages of functions that take pointers to smart pointers as arguments?
Herb Sutter does not mention this option in his article on Smart Pointer Parameters.
Upvotes: 3
Views: 419
Reputation: 8796
std::shared_ptr<…>*
is used in Arrow when a function is returning an object as a shared_ptr
while at the same time the function may fail with one of arrow::Status
codes.
Apache Arrow C++ adheres to the Google C++ style guide. One of the aspects is to not use exceptions. Furthermore, normally output will be done with a normal return
statement but in the cases where we also need to return a Status
, we use the alternative approach of returning it via a non-const
pointer.
For inputs where Arrow takes no ownership of the passed parameter, instead of std::shared_ptr<T>
, functions take const T&
. Shared pointers only appear in function signature if ownership is shared afterwards or when the parameter is an output parameter.
Upvotes: 4