Home | History | Annotate | Download | only in uninitialized.fill
      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 T>
     13 //   void
     14 //   uninitialized_fill(ForwardIterator first, ForwardIterator last,
     15 //                      const T& x);
     16 
     17 #include <memory>
     18 #include <cassert>
     19 
     20 struct B
     21 {
     22     static int count_;
     23     int data_;
     24     explicit B() : data_(1) {}
     25     B(const B& b) {if (++count_ == 3) throw 1; data_ = b.data_;}
     26     ~B() {data_ = 0;}
     27 };
     28 
     29 int B::count_ = 0;
     30 
     31 int main()
     32 {
     33     const int N = 5;
     34     char pool[sizeof(B)*N] = {0};
     35     B* bp = (B*)pool;
     36     try
     37     {
     38         std::uninitialized_fill(bp, bp+N, B());
     39         assert(false);
     40     }
     41     catch (...)
     42     {
     43         for (int i = 0; i < N; ++i)
     44             assert(bp[i].data_ == 0);
     45     }
     46     B::count_ = 0;
     47     std::uninitialized_fill(bp, bp+2, B());
     48     for (int i = 0; i < 2; ++i)
     49         assert(bp[i].data_ == 1);
     50 }
     51