mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Previously, we would get two copies, one accessing the pointee and one when we pass the pointee as a method as the implicit self argument. These copies are unsafe as they might introduce slicing. When addressable paramaters features are enabled, we no longer make these copies for the standard STL types. Custom smart pointers can replicate this by making the lifetime dependency between the implicit object parameter and the returned reference of operator* explicit via a lifetime annotation. rdar://154213694&128293252&112690482
23 lines
479 B
C
23 lines
479 B
C
#pragma once
|
|
|
|
static int copies2 = 0;
|
|
|
|
struct CountCopies2 {
|
|
CountCopies2() = default;
|
|
CountCopies2(const CountCopies2& other) { ++copies2; }
|
|
~CountCopies2() {}
|
|
|
|
int getCopies() const { return copies2; }
|
|
void method() {}
|
|
void constMethod() const {}
|
|
int field = 42;
|
|
};
|
|
|
|
struct MySmartPtr {
|
|
CountCopies2& operator*() const [[clang::lifetimebound]] { return *ptr; }
|
|
|
|
CountCopies2* ptr;
|
|
};
|
|
|
|
inline MySmartPtr getPtr() { return MySmartPtr{new CountCopies2()}; }
|