Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // raw_storage_iterator |
| 11 | |
| 12 | #include <memory> |
| 13 | #include <type_traits> |
| 14 | #include <cassert> |
| 15 | |
Marshall Clow | 332ab91 | 2015-10-25 18:58:07 +0000 | [diff] [blame] | 16 | #include <MoveOnly.h> |
| 17 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 18 | int A_constructed = 0; |
| 19 | |
| 20 | struct A |
| 21 | { |
| 22 | int data_; |
| 23 | public: |
| 24 | explicit A(int i) : data_(i) {++A_constructed;} |
| 25 | |
| 26 | A(const A& a) : data_(a.data_) {++A_constructed;} |
| 27 | ~A() {--A_constructed; data_ = 0;} |
| 28 | |
| 29 | bool operator==(int i) const {return data_ == i;} |
| 30 | }; |
| 31 | |
| 32 | int main() |
| 33 | { |
Marshall Clow | 332ab91 | 2015-10-25 18:58:07 +0000 | [diff] [blame] | 34 | { |
| 35 | typedef A S; |
| 36 | typedef std::aligned_storage<3*sizeof(S), std::alignment_of<S>::value>::type |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 37 | Storage; |
| 38 | Storage buffer; |
Marshall Clow | 332ab91 | 2015-10-25 18:58:07 +0000 | [diff] [blame] | 39 | std::raw_storage_iterator<S*, S> it((S*)&buffer); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 40 | assert(A_constructed == 0); |
| 41 | for (int i = 0; i < 3; ++i) |
| 42 | { |
Marshall Clow | 332ab91 | 2015-10-25 18:58:07 +0000 | [diff] [blame] | 43 | *it++ = S(i+1); |
| 44 | S* ap = (S*)&buffer + i; |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 45 | assert(*ap == i+1); |
| 46 | assert(A_constructed == i+1); |
| 47 | } |
Marshall Clow | 332ab91 | 2015-10-25 18:58:07 +0000 | [diff] [blame] | 48 | } |
| 49 | #if _LIBCPP_STD_VER >= 14 |
| 50 | { |
| 51 | typedef MoveOnly S; |
| 52 | typedef std::aligned_storage<3*sizeof(S), std::alignment_of<S>::value>::type |
| 53 | Storage; |
| 54 | Storage buffer; |
| 55 | std::raw_storage_iterator<S*, S> it((S*)&buffer); |
| 56 | S m{1}; |
| 57 | *it++ = std::move(m); |
| 58 | assert(m.get() == 0); // moved from |
| 59 | S *ap = (S*) &buffer; |
| 60 | assert(ap->get() == 1); // original value |
| 61 | } |
| 62 | #endif |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 63 | } |