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 | // <memory> |
| 11 | |
| 12 | // template <class ForwardIterator, class T> |
| 13 | // void |
| 14 | // uninitialized_fill(ForwardIterator first, ForwardIterator last, |
| 15 | // const T& x); |
| 16 | |
| 17 | #include <memory> |
| 18 | #include <cassert> |
| 19 | |
| 20 | struct B |
| 21 | { |
| 22 | static int count_; |
| 23 | int data_; |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 24 | explicit B() : data_(1) {} |
| 25 | B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_;} |
| 26 | ~B() {data_ = 0;} |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | int B::count_ = 0; |
| 30 | |
Marshall Clow | 5dce73d | 2015-05-19 15:01:48 +0000 | [diff] [blame] | 31 | struct Nasty |
| 32 | { |
| 33 | Nasty() : i_ ( counter_++ ) {} |
| 34 | Nasty * operator &() const { return NULL; } |
| 35 | int i_; |
| 36 | static int counter_; |
| 37 | }; |
| 38 | |
| 39 | int Nasty::counter_ = 0; |
| 40 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 41 | int main() |
| 42 | { |
Marshall Clow | 5dce73d | 2015-05-19 15:01:48 +0000 | [diff] [blame] | 43 | { |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 44 | const int N = 5; |
| 45 | char pool[sizeof(B)*N] = {0}; |
| 46 | B* bp = (B*)pool; |
| 47 | try |
| 48 | { |
| 49 | std::uninitialized_fill(bp, bp+N, B()); |
| 50 | assert(false); |
| 51 | } |
| 52 | catch (...) |
| 53 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 54 | for (int i = 0; i < N; ++i) |
| 55 | assert(bp[i].data_ == 0); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 56 | } |
| 57 | B::count_ = 0; |
| 58 | std::uninitialized_fill(bp, bp+2, B()); |
| 59 | for (int i = 0; i < 2; ++i) |
| 60 | assert(bp[i].data_ == 1); |
Marshall Clow | 5dce73d | 2015-05-19 15:01:48 +0000 | [diff] [blame] | 61 | } |
| 62 | { |
| 63 | const int N = 5; |
| 64 | char pool[N*sizeof(Nasty)] = {0}; |
| 65 | Nasty* bp = (Nasty*)pool; |
| 66 | |
| 67 | Nasty::counter_ = 23; |
| 68 | std::uninitialized_fill(bp, bp+N, Nasty()); |
| 69 | for (int i = 0; i < N; ++i) |
| 70 | assert(bp[i].i_ == 23); |
| 71 | } |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 72 | } |