C++ Logo


Advanced search

Subject: std::make_shared and type deduction
From: Kyle Knoepfel (kyleknoepfel_at_[hidden])
Date: 2020-07-07 13:37:03

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();

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

In these circumstances, the required specification of Foo<Int> as the template argument to make_unique is cumbersome. Granted, it is easy enough to create a wrapper function that does the type deduction for you:

template <typename T>
auto as_unique_ptr(T&& t) { return std::make_unique<T>(std::forward<T>(t)); }

auto foo_ptr = as_unique_ptr(external_library::make_foo());

or something similar.

The question: when developing std::make_(shared|unique), was it ever envisaged that an extension (perhaps a differently named facility) could exist that would type-deduce T as in the above as_unique_ptr function? Looking at the proposal for std::make_shared (http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2007/n2232.html <http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2007/n2232.html>) I saw no mention of it.

STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups