blob: d2b1dfa288686217402088866c65a0f2b068699b [file] [log] [blame]
Howard Hinnantc52f43e2010-08-22 00:59:46 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc52f43e2010-08-22 00:59:46 +00007//
8//===----------------------------------------------------------------------===//
9
10// <memory>
11
12// template <class ForwardIterator, class Size, class T>
Howard Hinnant2f6a6272010-11-18 16:13:03 +000013// ForwardIterator
Howard Hinnantc52f43e2010-08-22 00:59:46 +000014// uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
15
16#include <memory>
17#include <cassert>
18
19struct B
20{
21 static int count_;
22 int data_;
Dan Albert1d4a1ed2016-05-25 22:36:09 -070023 explicit B() : data_(1) {}
24 B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_;}
25 ~B() {data_ = 0;}
Howard Hinnantc52f43e2010-08-22 00:59:46 +000026};
27
28int B::count_ = 0;
29
Marshall Clow5dce73d2015-05-19 15:01:48 +000030struct Nasty
31{
32 Nasty() : i_ ( counter_++ ) {}
33 Nasty * operator &() const { return NULL; }
34 int i_;
35 static int counter_;
36};
37
38int Nasty::counter_ = 0;
39
Howard Hinnantc52f43e2010-08-22 00:59:46 +000040int main()
41{
Marshall Clow5dce73d2015-05-19 15:01:48 +000042 {
Howard Hinnantc52f43e2010-08-22 00:59:46 +000043 const int N = 5;
44 char pool[sizeof(B)*N] = {0};
45 B* bp = (B*)pool;
46 try
47 {
48 std::uninitialized_fill_n(bp, 5, B());
49 assert(false);
50 }
51 catch (...)
52 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -070053 for (int i = 0; i < N; ++i)
54 assert(bp[i].data_ == 0);
Howard Hinnantc52f43e2010-08-22 00:59:46 +000055 }
56 B::count_ = 0;
Howard Hinnant2f6a6272010-11-18 16:13:03 +000057 B* r = std::uninitialized_fill_n(bp, 2, B());
58 assert(r == bp + 2);
Howard Hinnantc52f43e2010-08-22 00:59:46 +000059 for (int i = 0; i < 2; ++i)
60 assert(bp[i].data_ == 1);
Marshall Clow5dce73d2015-05-19 15:01:48 +000061 }
62 {
63 {
64 const int N = 5;
65 char pool[N*sizeof(Nasty)] = {0};
66 Nasty* bp = (Nasty*)pool;
67
68 Nasty::counter_ = 23;
69 std::uninitialized_fill_n(bp, N, Nasty());
70 for (int i = 0; i < N; ++i)
71 assert(bp[i].i_ == 23);
72 }
73
74 }
Howard Hinnantc52f43e2010-08-22 00:59:46 +000075}