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#include <__type_traits/is_trivially_relocatable.h>
10#include <memory>
11#include <string>
12
13#include "constexpr_char_traits.h"
14#include "test_allocator.h"
15
16static_assert(std::__libcpp_is_trivially_relocatable<char>::value, "");
17static_assert(std::__libcpp_is_trivially_relocatable<int>::value, "");
18static_assert(std::__libcpp_is_trivially_relocatable<double>::value, "");
19
20struct Empty {};
21static_assert(std::__libcpp_is_trivially_relocatable<Empty>::value, "");
22
23struct TriviallyCopyable {
24 char c;
25 int i;
26 Empty s;
27};
28static_assert(std::__libcpp_is_trivially_relocatable<TriviallyCopyable>::value, "");
29
30struct NotTriviallyCopyable {
31 NotTriviallyCopyable(const NotTriviallyCopyable&);
32 ~NotTriviallyCopyable();
33};
34static_assert(!std::__libcpp_is_trivially_relocatable<NotTriviallyCopyable>::value, "");
35
36struct MoveOnlyTriviallyCopyable {
37 MoveOnlyTriviallyCopyable(const MoveOnlyTriviallyCopyable&) = delete;
38 MoveOnlyTriviallyCopyable& operator=(const MoveOnlyTriviallyCopyable&) = delete;
39 MoveOnlyTriviallyCopyable(MoveOnlyTriviallyCopyable&&) = default;
40 MoveOnlyTriviallyCopyable& operator=(MoveOnlyTriviallyCopyable&&) = default;
41};
42#ifndef _MSC_VER
43static_assert(std::__libcpp_is_trivially_relocatable<MoveOnlyTriviallyCopyable>::value, "");
44#else
45static_assert(!std::__libcpp_is_trivially_relocatable<MoveOnlyTriviallyCopyable>::value, "");
46#endif
47// standard library types
48// ----------------------
49
50// basic_string
51struct MyChar {
52 char c;
53};
54template <class T>
55struct NotTriviallyRelocatableCharTraits : constexpr_char_traits<T> {
56 NotTriviallyRelocatableCharTraits(const NotTriviallyRelocatableCharTraits&);
57 NotTriviallyRelocatableCharTraits& operator=(const NotTriviallyRelocatableCharTraits&);
58 ~NotTriviallyRelocatableCharTraits();
59};
60
61static_assert(std::__libcpp_is_trivially_relocatable<
62 std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::value,
63 "");
64static_assert(std::__libcpp_is_trivially_relocatable<
65 std::basic_string<char, NotTriviallyRelocatableCharTraits<char>, std::allocator<char> > >::value,
66 "");
67static_assert(std::__libcpp_is_trivially_relocatable<
68 std::basic_string<MyChar, constexpr_char_traits<MyChar>, std::allocator<MyChar> > >::value,
69 "");
70static_assert(
71 std::__libcpp_is_trivially_relocatable<
72 std::basic_string<MyChar, NotTriviallyRelocatableCharTraits<MyChar>, std::allocator<MyChar> > >::value,
73 "");
74static_assert(!std::__libcpp_is_trivially_relocatable<
75 std::basic_string<char, std::char_traits<char>, test_allocator<char> > >::value,
76 "");
77static_assert(
78 !std::__libcpp_is_trivially_relocatable<
79 std::basic_string<MyChar, NotTriviallyRelocatableCharTraits<MyChar>, test_allocator<MyChar> > >::value,
80 "");
81
82// unique_ptr
83struct NotTriviallyRelocatableDeleter {
84 NotTriviallyRelocatableDeleter(const NotTriviallyRelocatableDeleter&);
85 NotTriviallyRelocatableDeleter& operator=(const NotTriviallyRelocatableDeleter&);
86 ~NotTriviallyRelocatableDeleter();
87
88 template <class T>
89 void operator()(T*);
90};
91
92struct NotTriviallyRelocatablePointer {
93 struct pointer {
94 pointer(const pointer&);
95 pointer& operator=(const pointer&);
96 ~pointer();
97 };
98
99 template <class T>
100 void operator()(T*);
101};
102
103static_assert(std::__libcpp_is_trivially_relocatable<std::unique_ptr<int> >::value, "");
104static_assert(std::__libcpp_is_trivially_relocatable<std::unique_ptr<NotTriviallyCopyable> >::value, "");
105static_assert(std::__libcpp_is_trivially_relocatable<std::unique_ptr<int[]> >::value, "");
106static_assert(!std::__libcpp_is_trivially_relocatable<std::unique_ptr<int, NotTriviallyRelocatableDeleter> >::value,
107 "");
108static_assert(!std::__libcpp_is_trivially_relocatable<std::unique_ptr<int[], NotTriviallyRelocatableDeleter> >::value,
109 "");
110static_assert(!std::__libcpp_is_trivially_relocatable<std::unique_ptr<int, NotTriviallyRelocatablePointer> >::value,
111 "");
112static_assert(!std::__libcpp_is_trivially_relocatable<std::unique_ptr<int[], NotTriviallyRelocatablePointer> >::value,
113 "");
114
115// TODO: Mark all the trivially relocatable STL types as such
116

source code of libcxx/test/libcxx/type_traits/is_trivially_relocatable.compile.pass.cpp