Home | History | Annotate | Download | only in allocator.traits.members
      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 Alloc>
     13 // struct allocator_traits
     14 // {
     15 //     template <class Ptr, class... Args>
     16 //         static void construct(allocator_type& a, Ptr p, Args&&... args);
     17 //     ...
     18 // };
     19 
     20 #include <memory>
     21 #include <new>
     22 #include <type_traits>
     23 #include <cassert>
     24 
     25 template <class T>
     26 struct A
     27 {
     28     typedef T value_type;
     29 
     30 };
     31 
     32 int b_construct = 0;
     33 
     34 template <class T>
     35 struct B
     36 {
     37     typedef T value_type;
     38 
     39 #ifndef _LIBCPP_HAS_NO_VARIADICS
     40     template <class U, class ...Args>
     41     void construct(U* p, Args&& ...args)
     42     {
     43         ++b_construct;
     44         ::new ((void*)p) U(std::forward<Args>(args)...);
     45     }
     46 #endif  // _LIBCPP_HAS_NO_VARIADICS
     47 };
     48 
     49 struct A0
     50 {
     51     static int count;
     52     A0() {++count;}
     53 };
     54 
     55 int A0::count = 0;
     56 
     57 struct A1
     58 {
     59     static int count;
     60     A1(char c)
     61     {
     62         assert(c == 'c');
     63         ++count;
     64     }
     65 };
     66 
     67 int A1::count = 0;
     68 
     69 struct A2
     70 {
     71     static int count;
     72     A2(char c, int i)
     73     {
     74         assert(c == 'd');
     75         assert(i == 5);
     76         ++count;
     77     }
     78 };
     79 
     80 int A2::count = 0;
     81 
     82 int main()
     83 {
     84     {
     85         A0::count = 0;
     86         A<int> a;
     87         std::aligned_storage<sizeof(A0)>::type a0;
     88         assert(A0::count == 0);
     89         std::allocator_traits<A<int> >::construct(a, (A0*)&a0);
     90         assert(A0::count == 1);
     91     }
     92     {
     93         A1::count = 0;
     94         A<int> a;
     95         std::aligned_storage<sizeof(A1)>::type a1;
     96         assert(A1::count == 0);
     97         std::allocator_traits<A<int> >::construct(a, (A1*)&a1, 'c');
     98         assert(A1::count == 1);
     99     }
    100     {
    101         A2::count = 0;
    102         A<int> a;
    103         std::aligned_storage<sizeof(A2)>::type a2;
    104         assert(A2::count == 0);
    105         std::allocator_traits<A<int> >::construct(a, (A2*)&a2, 'd', 5);
    106         assert(A2::count == 1);
    107     }
    108 #ifndef _LIBCPP_HAS_NO_VARIADICS
    109     {
    110         A0::count = 0;
    111         b_construct = 0;
    112         B<int> b;
    113         std::aligned_storage<sizeof(A0)>::type a0;
    114         assert(A0::count == 0);
    115         assert(b_construct == 0);
    116         std::allocator_traits<B<int> >::construct(b, (A0*)&a0);
    117         assert(A0::count == 1);
    118         assert(b_construct == 1);
    119     }
    120     {
    121         A1::count = 0;
    122         b_construct = 0;
    123         B<int> b;
    124         std::aligned_storage<sizeof(A1)>::type a1;
    125         assert(A1::count == 0);
    126         assert(b_construct == 0);
    127         std::allocator_traits<B<int> >::construct(b, (A1*)&a1, 'c');
    128         assert(A1::count == 1);
    129         assert(b_construct == 1);
    130     }
    131     {
    132         A2::count = 0;
    133         b_construct = 0;
    134         B<int> b;
    135         std::aligned_storage<sizeof(A2)>::type a2;
    136         assert(A2::count == 0);
    137         assert(b_construct == 0);
    138         std::allocator_traits<B<int> >::construct(b, (A2*)&a2, 'd', 5);
    139         assert(A2::count == 1);
    140         assert(b_construct == 1);
    141     }
    142 #endif  // _LIBCPP_HAS_NO_VARIADICS
    143 }
    144