| 1 | #include <memory> |
| 2 | #include <string> |
| 3 | |
| 4 | struct User { |
| 5 | int id = 30; |
| 6 | std::string name = "steph" ; |
| 7 | }; |
| 8 | |
| 9 | // libc++ stores unique_ptr data in a compressed pair, which has a specialized |
| 10 | // representation when the type of the second element is an empty class. So |
| 11 | // we need a deleter class with a dummy data member to trigger the other path. |
| 12 | struct NonEmptyIntDeleter { |
| 13 | void operator()(int* ptr) { delete ptr; } |
| 14 | |
| 15 | int dummy_ = 9999; |
| 16 | }; |
| 17 | |
| 18 | int main() { |
| 19 | std::unique_ptr<int> up_empty; |
| 20 | std::unique_ptr<int> up_int = std::make_unique<int>(args: 10); |
| 21 | std::unique_ptr<std::string> up_str = std::make_unique<std::string>(args: "hello" ); |
| 22 | std::unique_ptr<int> &up_int_ref = up_int; |
| 23 | std::unique_ptr<int> &&up_int_ref_ref = std::make_unique<int>(args: 10); |
| 24 | std::unique_ptr<User> up_user = std::make_unique<User>(); |
| 25 | auto up_non_empty_deleter = |
| 26 | std::unique_ptr<int, NonEmptyIntDeleter>(new int(1234)); |
| 27 | |
| 28 | return 0; // break here |
| 29 | } |
| 30 | |