Home | History | Annotate | Download | only in storage.iterator
      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 #include "test_macros.h"
     17 #include <MoveOnly.h>
     18 
     19 int A_constructed = 0;
     20 
     21 struct A
     22 {
     23     int data_;
     24 public:
     25     explicit A(int i) : data_(i) {++A_constructed;}
     26 
     27     A(const A& a) : data_(a.data_)  {++A_constructed;}
     28     ~A() {--A_constructed; data_ = 0;}
     29 
     30     bool operator==(int i) const {return data_ == i;}
     31 };
     32 
     33 int main()
     34 {
     35     {
     36     typedef A S;
     37     typedef std::aligned_storage<3*sizeof(S), std::alignment_of<S>::value>::type
     38             Storage;
     39     Storage buffer;
     40     std::raw_storage_iterator<S*, S> it((S*)&buffer);
     41     assert(A_constructed == 0);
     42     for (int i = 0; i < 3; ++i)
     43     {
     44         *it++ = S(i+1);
     45         S* ap = (S*)&buffer + i;
     46         assert(*ap == i+1);
     47         assert(A_constructed == i+1);
     48     }
     49     }
     50 #if TEST_STD_VER >= 14
     51     {
     52     typedef MoveOnly S;
     53     typedef std::aligned_storage<3*sizeof(S), std::alignment_of<S>::value>::type
     54             Storage;
     55     Storage buffer;
     56     std::raw_storage_iterator<S*, S> it((S*)&buffer);
     57     S m{1};
     58     *it++ = std::move(m);
     59     assert(m.get() == 0); // moved from
     60     S *ap = (S*) &buffer;
     61     assert(ap->get() == 1); // original value
     62     }
     63 #endif
     64 }
     65