Commit 9ffd6285 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

to_shared_ptr_aliasing

Summary: [Folly] `to_shared_ptr_aliasing`, exposing the shared-ptr aliasing constructor as a free function deducing the types of its arguments.

Reviewed By: Mizuchi

Differential Revision: D24414504

fbshipit-source-id: 290e822464860b824961986211375d87f942f483
parent 039b4056
...@@ -324,6 +324,14 @@ std::shared_ptr<T> to_shared_ptr(std::unique_ptr<T, D>&& ptr) { ...@@ -324,6 +324,14 @@ std::shared_ptr<T> to_shared_ptr(std::unique_ptr<T, D>&& ptr) {
return std::shared_ptr<T>(std::move(ptr)); return std::shared_ptr<T>(std::move(ptr));
} }
/**
* to_shared_ptr_aliasing
*/
template <typename T, typename U>
std::shared_ptr<U> to_shared_ptr_aliasing(std::shared_ptr<T> const& r, U* ptr) {
return std::shared_ptr<U>(r, ptr);
}
/** /**
* to_weak_ptr * to_weak_ptr
* *
......
...@@ -86,6 +86,12 @@ TEST(make_unique, compatible_with_std_make_unique) { ...@@ -86,6 +86,12 @@ TEST(make_unique, compatible_with_std_make_unique) {
make_unique<string>("hello, world"); make_unique<string>("hello, world");
} }
TEST(to_shared_ptr_aliasing, example) {
auto sp = folly::copy_to_shared_ptr(std::tuple{3, 4});
auto a = folly::to_shared_ptr_aliasing(sp, &std::get<1>(*sp));
EXPECT_EQ(4, *a);
}
TEST(to_weak_ptr, example) { TEST(to_weak_ptr, example) {
auto s = std::make_shared<int>(17); auto s = std::make_shared<int>(17);
EXPECT_EQ(1, s.use_count()); EXPECT_EQ(1, s.use_count());
......
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