lundi 4 février 2019

Why shared_ptr constructor does not call make_shared on non-pointer types?

I have to use make_shared most of the times I am using shared_ptr type (i.e. I rarely have to manually create a pointer to pass to the constructor):
auto p = std::make_shared<T>(...);

Assuming the above is also common for others using shared_ptr type, why isn't there a constructor that takes any non-pointer object type and attempts to create a shared_ptr from it by invoking make_shared? (as opposed to having the user manually use make_shared each time)

In case you argue the above is not much more verbose than the hypothetical:
std::shared_ptr<T> p(...);

There are cases where it would not be possible to use the auto keyword, therefore making the repetition more painful:

// declaration
class Foo
{
    static std::shared_ptr<T> p;
}
// definition
std::shared_ptr<T> Foo::p = std::make_shared<T>(...);

Aucun commentaire:

Enregistrer un commentaire