mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
[c10] Add default constructor to Maybeowned (#55128)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/55128 Test Plan: CI Reviewed By: swolchok Differential Revision: D27495079 fbshipit-source-id: 3bd01956a8b65170d6b38096dbd15c4809904f88
This commit is contained in:
committed by
Facebook GitHub Bot
parent
ec609e7420
commit
93d0f636bb
@ -35,6 +35,17 @@ TEST(MaybeOwnedTest, SimpleDereferencingString) {
|
||||
EXPECT_EQ(owned2->size(), x.size());
|
||||
}
|
||||
|
||||
TEST(MaybeOwnedTest, DefaultCtorInt) {
|
||||
int x = 123;
|
||||
MaybeOwned<int> borrowed, owned;
|
||||
borrowed = MaybeOwned<int>::borrowed(x);
|
||||
owned = MaybeOwned<int>::owned(c10::in_place, x);
|
||||
EXPECT_EQ(*borrowed, x);
|
||||
EXPECT_EQ(*owned, x);
|
||||
EXPECT_EQ(&*borrowed, &x);
|
||||
EXPECT_NE(&*owned, &x);
|
||||
}
|
||||
|
||||
TEST(MaybeOwnedTest, MoveConstructor) {
|
||||
std::string x = "hello";
|
||||
auto borrowed = MaybeOwned<std::string>::borrowed(x);
|
||||
|
@ -37,7 +37,7 @@ class MaybeOwned final {
|
||||
, own_(std::forward<Args>(args)...) {}
|
||||
|
||||
public:
|
||||
|
||||
explicit MaybeOwned(): isBorrowed_(true), borrow_(nullptr) {}
|
||||
MaybeOwned(const MaybeOwned&) = delete;
|
||||
MaybeOwned& operator=(const MaybeOwned&) = delete;
|
||||
|
||||
@ -91,10 +91,16 @@ class MaybeOwned final {
|
||||
}
|
||||
|
||||
const T& operator*() const {
|
||||
if (isBorrowed_) {
|
||||
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(borrow_ != nullptr);
|
||||
}
|
||||
return isBorrowed_ ? *borrow_ : own_;
|
||||
}
|
||||
|
||||
const T* operator->() const {
|
||||
if (isBorrowed_) {
|
||||
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(borrow_ != nullptr);
|
||||
}
|
||||
return isBorrowed_ ? borrow_ : &own_;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user