language: C++11 (gcc-4.7.2)
date: 806 days 12 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <type_traits>
#include <stdexcept>
#include <memory>
#include <iostream>
 
struct T {
   int val;
   T() = delete;
   T(int i) : val(i) {}
   void* operator new[](std::size_t, std::size_t cnt, const T& t)
   {
       typedef std::aligned_storage<sizeof(t),
                    std::alignment_of<T>::value>::type buf;
       T* ptr = reinterpret_cast<T*>(new buf[cnt]);
       std::uninitialized_fill_n(ptr, cnt, t);
       return ptr;
    }
};
 
int main()
{
    T* a = new(100, T(7)) T[0]; // using zero is legal per 5.3.4/7
 
    std::cout << "a[0] = " << a[0].val << '\n'
              << "a[1] = " << a[1].val << '\n'
              << "a[98] = " << a[98].val << '\n'
              << "a[99] = " << a[99].val << '\n';
    delete[] a; // I know this won't call the 100 destructors, but it will free the 100 aligned_storages at least
}