C++ Logo


Advanced search

Re: std::make_shared and type deduction

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 08 Jul 2020 11:51:34 -0700
On Tuesday, 7 July 2020 11:37:03 PDT Kyle Knoepfel via Std-Discussion wrote:
> I routinely encounter situations where objects must be owned by (smart)
> pointer, but the means in which the pointee is constructed is not under my
> control. For example:
> namespace external_library {
> template <typename T>
> Foo<T> make_foo();
> }

This is not a good example because make_foo did not return a pointer. It
returned an object by value. Therefore, when you do:

> auto foo_ptr = std::make_unique<Foo<int>>(external_library::make_foo());

The pointee is constructed fully in your control. You're creating a copy of
Foo<int> and saving that in a unique_ptr.

But note how Foo<int> can be copied. So are you sure you need unique_ptr in
the first place?

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel System Software Products

Received on 2020-07-08 13:54:52