Commit 34ea33d2 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

copy_to_unique_ptr, copy_to_shared_ptr

Summary: [Folly] `copy_to_unique_ptr`, `copy_to_shared_ptr` - type-deducing wrappers around `make_unique` and `make_shared`.

Reviewed By: vitaut

Differential Revision: D21332222

fbshipit-source-id: f41dac0fcfa972118d3a721194c941e14511a80c
parent 4fffe6ad
......@@ -348,6 +348,30 @@ std::weak_ptr<T> to_weak_ptr(const std::shared_ptr<T>& ptr) {
return std::weak_ptr<T>(ptr);
}
/**
* copy_to_unique_ptr
*
* Move or copy the argument to the heap and return it owned by a unique_ptr.
*
* Like make_unique, but deduces the type of the owned object.
*/
template <typename T>
std::unique_ptr<remove_cvref_t<T>> copy_to_unique_ptr(T&& t) {
return make_unique<remove_cvref_t<T>>(static_cast<T&&>(t));
}
/**
* copy_to_shared_ptr
*
* Move or copy the argument to the heap and return it owned by a shared_ptr.
*
* Like make_shared, but deduces the type of the owned object.
*/
template <typename T>
std::shared_ptr<remove_cvref_t<T>> copy_to_shared_ptr(T&& t) {
return std::make_shared<remove_cvref_t<T>>(static_cast<T&&>(t));
}
namespace detail {
template <typename T>
struct lift_void_to_char {
......
......@@ -91,6 +91,16 @@ TEST(to_weak_ptr, example) {
EXPECT_EQ(3, (to_weak_ptr(decltype(s)(s)).lock(), s.use_count())) << "rvalue";
}
TEST(copy_to_unique_ptr, example) {
std::unique_ptr<int> s = copy_to_unique_ptr(17);
EXPECT_EQ(17, *s);
}
TEST(copy_to_shared_ptr, example) {
std::shared_ptr<int> s = copy_to_shared_ptr(17);
EXPECT_EQ(17, *s);
}
TEST(SysAllocator, equality) {
using Alloc = SysAllocator<float>;
Alloc const a, b;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment