1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <array>
10 
11 // implicitly generated array constructors / assignment operators
12 
13 #include <array>
14 #include <type_traits>
15 #include <cassert>
16 #include "test_macros.h"
17 
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 // In C++03 the copy assignment operator is not deleted when the implicitly
23 // generated operator would be ill-formed; like in the case of a struct with a
24 // const member.
25 #if TEST_STD_VER < 11
26 #   define TEST_NOT_COPY_ASSIGNABLE(T) ((void)0)
27 #else
28 #   define TEST_NOT_COPY_ASSIGNABLE(T) static_assert(!std::is_copy_assignable<T>::value, "")
29 #endif
30 
31 struct NoDefault {
32     TEST_CONSTEXPR NoDefault(int) { }
33 };
34 
35 TEST_CONSTEXPR_CXX14 bool tests()
36 {
37     {
38         typedef double T;
39         typedef std::array<T, 3> C;
40         C c = {1.1, 2.2, 3.3};
41         C c2 = c;
42         c2 = c;
43         static_assert(std::is_copy_constructible<C>::value, "");
44         static_assert(std::is_copy_assignable<C>::value, "");
45     }
46     {
47         typedef double T;
48         typedef std::array<const T, 3> C;
49         C c = {1.1, 2.2, 3.3};
50         C c2 = c;
51         ((void)c2);
52         static_assert(std::is_copy_constructible<C>::value, "");
53         TEST_NOT_COPY_ASSIGNABLE(C);
54     }
55     {
56         typedef double T;
57         typedef std::array<T, 0> C;
58         C c = {};
59         C c2 = c;
60         c2 = c;
61         static_assert(std::is_copy_constructible<C>::value, "");
62         static_assert(std::is_copy_assignable<C>::value, "");
63     }
64     {
65         // const arrays of size 0 should disable the implicit copy assignment operator.
66         typedef double T;
67         typedef std::array<const T, 0> C;
68         C c = {{}};
69         C c2 = c;
70         ((void)c2);
71         static_assert(std::is_copy_constructible<C>::value, "");
72         TEST_NOT_COPY_ASSIGNABLE(C);
73     }
74     {
75         typedef NoDefault T;
76         typedef std::array<T, 0> C;
77         C c = {};
78         C c2 = c;
79         c2 = c;
80         static_assert(std::is_copy_constructible<C>::value, "");
81         static_assert(std::is_copy_assignable<C>::value, "");
82     }
83     {
84         typedef NoDefault T;
85         typedef std::array<const T, 0> C;
86         C c = {{}};
87         C c2 = c;
88         ((void)c2);
89         static_assert(std::is_copy_constructible<C>::value, "");
90         TEST_NOT_COPY_ASSIGNABLE(C);
91     }
92 
93     return true;
94 }
95 
96 int main(int, char**)
97 {
98     tests();
99 #if TEST_STD_VER >= 14
100     static_assert(tests(), "");
101 #endif
102     return 0;
103 }
104