What type should C++ object creation functions return (e.g. pointer, smart pointer, reference, etc.)? Several of our C++ API's return shared pointers. For example in the following function signature [1]:
std::shared_ptr<CacheFactory> CacheFactory::createCacheFactory(...); Here the only case I can see for shared pointer is to indicate ownership of CacheFactory. Ideally this should probably be std::unique_ptr because callee shouldn't share ownership. However, I don't see the point of using a pointer at all.. I suggest we return the bare object, like the following signature: CacheFactory CacheFactory::createCacheFactory(...); In C++03, this would have been a performance hit because we'd end up with an added call to the copy constructor. In C++11, std::unique_ptr gives std::move for free and thus avoids copy-constructor call. However, most modern C++11 compilers already perform copy-elision here. In fact, C++17 standard dictates that compilers must perform RVO here. Therefore it doesn't seem to me that std::shared_ptr or std::unique_ptr buys us much in this situation. Thoughts? Thanks, David [1] https://github.com/apache/geode-native/blob/develop/ cppcache/include/geode/CacheFactory.hpp#L54