In a C++03 environment, would you use an auto_ptr or a (boost) shared_ptr to return a resource from a function? (Where in C++11 one would naturally use a unique_ptr.)
auto_ptr<T> f() {
...
return new T();
}
or
shared_ptr<T> f() {
...
return new T();
}
auto_ptr has quite some pitfalls (not the least that it's construction is awfully buggy on MSVC 2005 which is what I have to use), but shared_ptr seems like overkill ...
In C++03, I would use either a bare pointer or an auto_ptr and let the caller decide on the ownership strategy.
Note that one of the pitfalls of smart pointers is that covariance does not apply to them, meaning that you can override Base* f() by Derived* f() but you cannot override std::x_ptr<Base> f() by std::x_ptr<Derived> f(); and thus in this case you have no choice but to use a simple pointer.
I would use a shared_ptr or a raw pointer because auto_ptr is considered a bad idea.
It really depends on your ownership semantics. Who will destroy the owned resource?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With