Home | History | Annotate | Download | only in array.special
      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 // <array>
     11 
     12 // template <class T, size_t N> void swap(array<T,N>& x, array<T,N>& y);
     13 
     14 #include <array>
     15 #include <cassert>
     16 
     17 #include "test_macros.h"
     18 // std::array is explicitly allowed to be initialized with A a = { init-list };.
     19 // Disable the missing braces warning for this reason.
     20 #include "disable_missing_braces_warning.h"
     21 
     22 struct NonSwappable {
     23   NonSwappable() {}
     24 private:
     25   NonSwappable(NonSwappable const&);
     26   NonSwappable& operator=(NonSwappable const&);
     27 };
     28 
     29 template <class Tp>
     30 decltype(swap(std::declval<Tp>(), std::declval<Tp>()))
     31 can_swap_imp(int);
     32 
     33 template <class Tp>
     34 std::false_type can_swap_imp(...);
     35 
     36 template <class Tp>
     37 struct can_swap : std::is_same<decltype(can_swap_imp<Tp>(0)), void> {};
     38 
     39 int main()
     40 {
     41     {
     42         typedef double T;
     43         typedef std::array<T, 3> C;
     44         C c1 = {1, 2, 3.5};
     45         C c2 = {4, 5, 6.5};
     46         swap(c1, c2);
     47         assert(c1.size() == 3);
     48         assert(c1[0] == 4);
     49         assert(c1[1] == 5);
     50         assert(c1[2] == 6.5);
     51         assert(c2.size() == 3);
     52         assert(c2[0] == 1);
     53         assert(c2[1] == 2);
     54         assert(c2[2] == 3.5);
     55     }
     56     {
     57         typedef double T;
     58         typedef std::array<T, 0> C;
     59         C c1 = {};
     60         C c2 = {};
     61         swap(c1, c2);
     62         assert(c1.size() == 0);
     63         assert(c2.size() == 0);
     64     }
     65     {
     66         typedef NonSwappable T;
     67         typedef std::array<T, 0> C0;
     68         static_assert(can_swap<C0&>::value, "");
     69         C0 l = {};
     70         C0 r = {};
     71         swap(l, r);
     72 #if TEST_STD_VER >= 11
     73         static_assert(noexcept(swap(l, r)), "");
     74 #endif
     75     }
     76 #if TEST_STD_VER >= 11
     77     {
     78         // NonSwappable is still considered swappable in C++03 because there
     79         // is no access control SFINAE.
     80         typedef NonSwappable T;
     81         typedef std::array<T, 42> C1;
     82         static_assert(!can_swap<C1&>::value, "");
     83     }
     84 #endif
     85 }
     86