Debian Codegraph @

libcxx/test/libcxx-03/utilities/utility/pairs/pairs.pair/abi.trivial_copy_move.pass.cpp

raw
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 // <utility>
10
11 // template <class T1, class T2> struct pair
12
13 // Test that we properly provide the trivial copy operations by default.
14
15 // FreeBSD still provides the old ABI for std::pair.
16 // XFAIL: freebsd
17 // ADDITIONAL_COMPILE_FLAGS: -Wno-invalid-offsetof
18
19 #include <utility>
20 #include <type_traits>
21 #include <cstdlib>
22 #include <cstddef>
23 #include <cassert>
24
25 #include "test_macros.h"
26
27 template <class T>
28 struct HasTrivialABI : std::integral_constant<bool,
29 std::is_trivially_destructible<T>::value
30 && (!std::is_copy_constructible<T>::value || std::is_trivially_copy_constructible<T>::value)
31 > {};
32
33 struct TrivialNoAssignment {
34 int arr[4];
35 TrivialNoAssignment& operator=(const TrivialNoAssignment&) = delete;
36 };
37
38 struct TrivialNoConstruction {
39 int arr[4];
40 TrivialNoConstruction() = default;
41 TrivialNoConstruction(const TrivialNoConstruction&) = delete;
42 TrivialNoConstruction& operator=(const TrivialNoConstruction&) = default;
43 };
44
45 void test_trivial()
46 {
47 {
48 typedef std::pair<int, short> P;
49 static_assert(std::is_copy_constructible<P>::value, "");
50 static_assert(HasTrivialABI<P>::value, "");
51 }
52 {
53 using P = std::pair<TrivialNoAssignment, int>;
54 static_assert(std::is_trivially_copy_constructible<P>::value, "");
55 static_assert(std::is_trivially_move_constructible<P>::value, "");
56 static_assert(std::is_trivially_destructible<P>::value, "");
57 }
58 {
59 using P = std::pair<TrivialNoConstruction, int>;
60 static_assert(!std::is_trivially_copy_assignable<P>::value, "");
61 static_assert(!std::is_trivially_move_assignable<P>::value, "");
62 static_assert(std::is_trivially_destructible<P>::value, "");
63 }
64 }
65
66 void test_layout() {
67 typedef std::pair<std::pair<char, char>, char> PairT;
68 static_assert(sizeof(PairT) == 3, "");
69 static_assert(TEST_ALIGNOF(PairT) == TEST_ALIGNOF(char), "");
70 static_assert(offsetof(PairT, first) == 0, "");
71 }
72
73 int main(int, char**) {
74 test_trivial();
75 test_layout();
76 return 0;
77 }