blob: 3b2007b969c3fa997756692a4e4149e48f758c0f [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 InputIterator, class Size, class ForwardIterator>
13// ForwardIterator
14// uninitialized_copy_n(InputIterator first, Size n,
15// ForwardIterator result);
16
17#include <memory>
18#include <cassert>
19
20struct B
21{
22 static int count_;
23 int data_;
Dan Albert1d4a1ed2016-05-25 22:36:09 -070024 explicit B() : data_(1) {}
25 B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_;}
26 ~B() {data_ = 0;}
Howard Hinnantc52f43e2010-08-22 00:59:46 +000027};
28
29int B::count_ = 0;
30
Marshall Clow5dce73d2015-05-19 15:01:48 +000031struct Nasty
32{
33 Nasty() : i_ ( counter_++ ) {}
34 Nasty * operator &() const { return NULL; }
35 int i_;
36 static int counter_;
37};
38
39int Nasty::counter_ = 0;
40
Howard Hinnantc52f43e2010-08-22 00:59:46 +000041int main()
42{
Marshall Clow5dce73d2015-05-19 15:01:48 +000043 {
Howard Hinnantc52f43e2010-08-22 00:59:46 +000044 const int N = 5;
45 char pool[sizeof(B)*N] = {0};
46 B* bp = (B*)pool;
47 B b[N];
48 try
49 {
50 std::uninitialized_copy_n(b, 5, bp);
51 assert(false);
52 }
53 catch (...)
54 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -070055 for (int i = 0; i < N; ++i)
56 assert(bp[i].data_ == 0);
Howard Hinnantc52f43e2010-08-22 00:59:46 +000057 }
58 B::count_ = 0;
59 std::uninitialized_copy_n(b, 2, bp);
60 for (int i = 0; i < 2; ++i)
61 assert(bp[i].data_ == 1);
Marshall Clow5dce73d2015-05-19 15:01:48 +000062 }
63 {
64 const int N = 5;
65 char pool[sizeof(Nasty)*N] = {0};
66 Nasty * p = (Nasty *) pool;
67 Nasty arr[N];
68 std::uninitialized_copy_n(arr, N, p);
69 for (int i = 0; i < N; ++i) {
70 assert(arr[i].i_ == i);
71 assert( p[i].i_ == i);
72 }
73 }
Howard Hinnantc52f43e2010-08-22 00:59:46 +000074}