The choice of smart pointer depends on caller code instead of callee code.
Created by: wangkuiyi
I noticed that at https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/net.h#L73, there defines
class PlainNet : public Net {
void Run(const ScopePtr& scope,
const platform::DeviceContext& dev_ctx) const override {
and at https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/net.h#L60, there defines
class PlainNet : public Net {
void InferShape(const ScopePtr& scope) const override {
where ScopePtr
is defined at https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/framework/scope.h#L27 as
using ScopePtr = std::shared_ptr<Scope>;
However the choice of smart pointer, e.g., shared_ptr v.s. unique_ptr, depends on the application environment of class Scope
. In some cases, we want to keep a singleton/default object and we use unique_ptr<Scope>
; and in some other cases, we want auto garbage-collection and we use shared_ptr<Scope>
.
In above example, all what PlainNet::InferShape
and PlainNet::Run
want is const Scope&
.
I'd suggest that we remove the assumption about what application code would want a smart pointer and keep the source code in its first-principle form.