Marshall Clow | dbaf7a0 | 2015-05-10 13:14:08 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // raw_storage_iterator |
| 11 | |
| 12 | #include <memory> |
| 13 | #include <type_traits> |
| 14 | #include <cassert> |
| 15 | |
| 16 | int A_constructed = 0; |
| 17 | |
| 18 | struct A |
| 19 | { |
| 20 | int data_; |
| 21 | public: |
| 22 | explicit A(int i) : data_(i) {++A_constructed;} |
| 23 | |
| 24 | A(const A& a) : data_(a.data_) {++A_constructed;} |
| 25 | ~A() {--A_constructed; data_ = 0;} |
| 26 | |
| 27 | bool operator==(int i) const {return data_ == i;} |
| 28 | }; |
| 29 | |
| 30 | int main() |
| 31 | { |
Marshall Clow | dbaf7a0 | 2015-05-10 13:14:08 +0000 | [diff] [blame] | 32 | typedef std::aligned_storage<3*sizeof(A), std::alignment_of<A>::value>::type |
| 33 | Storage; |
| 34 | Storage buffer; |
| 35 | std::raw_storage_iterator<A*, A> it((A*)&buffer); |
| 36 | assert(A_constructed == 0); |
Marshall Clow | dbaf7a0 | 2015-05-10 13:14:08 +0000 | [diff] [blame] | 37 | for (int i = 0; i < 3; ++i) |
| 38 | { |
| 39 | *it++ = A(i+1); |
| 40 | A* ap = (A*)&buffer + i; |
| 41 | assert(*ap == i+1); |
| 42 | assert(A_constructed == i+1); |
Marshall Clow | dbaf7a0 | 2015-05-10 13:14:08 +0000 | [diff] [blame] | 43 | } |
Marshall Clow | dbaf7a0 | 2015-05-10 13:14:08 +0000 | [diff] [blame] | 44 | } |