blob: 914802423ce778773daeb441e48ca786949a9748 [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// raw_storage_iterator
11
12#include <memory>
13#include <type_traits>
14#include <cassert>
15
Marshall Clow332ab912015-10-25 18:58:07 +000016#include <MoveOnly.h>
17
Howard Hinnantc52f43e2010-08-22 00:59:46 +000018int A_constructed = 0;
19
20struct A
21{
22 int data_;
23public:
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
32int main()
33{
Marshall Clow332ab912015-10-25 18:58:07 +000034 {
35 typedef A S;
36 typedef std::aligned_storage<3*sizeof(S), std::alignment_of<S>::value>::type
Howard Hinnantc52f43e2010-08-22 00:59:46 +000037 Storage;
38 Storage buffer;
Marshall Clow332ab912015-10-25 18:58:07 +000039 std::raw_storage_iterator<S*, S> it((S*)&buffer);
Howard Hinnantc52f43e2010-08-22 00:59:46 +000040 assert(A_constructed == 0);
41 for (int i = 0; i < 3; ++i)
42 {
Marshall Clow332ab912015-10-25 18:58:07 +000043 *it++ = S(i+1);
44 S* ap = (S*)&buffer + i;
Howard Hinnantc52f43e2010-08-22 00:59:46 +000045 assert(*ap == i+1);
46 assert(A_constructed == i+1);
47 }
Marshall Clow332ab912015-10-25 18:58:07 +000048 }
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 Hinnantc52f43e2010-08-22 00:59:46 +000063}