Home | History | Annotate | Download | only in uninitialized.fill.n
      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 // <memory>
     11 
     12 // template <class ForwardIterator, class Size, class T>
     13 //   ForwardIterator
     14 //   uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
     15 
     16 #include <memory>
     17 #include <cassert>
     18 
     19 struct B
     20 {
     21     static int count_;
     22     int data_;
     23     explicit B() : data_(1) {}
     24     B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_;}
     25     ~B() {data_ = 0;}
     26 };
     27 
     28 int B::count_ = 0;
     29 
     30 struct Nasty
     31 {
     32     Nasty() : i_ ( counter_++ ) {}
     33     Nasty * operator &() const { return NULL; }
     34     int i_;
     35     static int counter_;
     36 };
     37 
     38 int Nasty::counter_ = 0;
     39 
     40 int main()
     41 {
     42     {
     43     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     {
     53         for (int i = 0; i < N; ++i)
     54             assert(bp[i].data_ == 0);
     55     }
     56     B::count_ = 0;
     57     B* r = std::uninitialized_fill_n(bp, 2, B());
     58     assert(r == bp + 2);
     59     for (int i = 0; i < 2; ++i)
     60         assert(bp[i].data_ == 1);
     61     }
     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     }
     75 }
     76