• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 
12 // <optional>
13 
14 // This test asserts the triviality of special member functions of optional<T>
15 // whenever T has these special member functions trivial. The goal of this test
16 // is to make sure that we do not change the triviality of those, since that
17 // constitues an ABI break (small enough optionals would be passed by registers).
18 //
19 // constexpr optional(const optional& rhs);
20 // constexpr optional(optional&& rhs) noexcept(see below);
21 // constexpr optional<T>& operator=(const optional& rhs);
22 // constexpr optional<T>& operator=(optional&& rhs) noexcept(see below);
23 
24 #include <optional>
25 #include <type_traits>
26 #include <cassert>
27 
28 #include "archetypes.hpp"
29 
30 template <class T>
31 struct SpecialMemberTest {
32     using O = std::optional<T>;
33 
34     static_assert(std::is_trivially_destructible_v<O> ==
35         std::is_trivially_destructible_v<T>,
36         "optional<T> is trivially destructible if and only if T is.");
37 
38     static_assert(std::is_trivially_copy_constructible_v<O> ==
39         std::is_trivially_copy_constructible_v<T>,
40         "optional<T> is trivially copy constructible if and only if T is.");
41 
42     static_assert(std::is_trivially_move_constructible_v<O> ==
43         std::is_trivially_move_constructible_v<T> ||
44         (!std::is_move_constructible_v<T> && std::is_trivially_copy_constructible_v<T>),
45         "optional<T> is trivially move constructible if T is trivially move constructible, "
46         "or if T is trivially copy constructible and is not move constructible.");
47 
48     static_assert(std::is_trivially_copy_assignable_v<O> ==
49         (std::is_trivially_destructible_v<T> &&
50          std::is_trivially_copy_constructible_v<T> &&
51          std::is_trivially_copy_assignable_v<T>),
52         "optional<T> is trivially copy assignable if and only if T is trivially destructible, "
53         "trivially copy constructible, and trivially copy assignable.");
54 
55     static_assert(std::is_trivially_move_assignable_v<O> ==
56         (std::is_trivially_destructible_v<T> &&
57          ((std::is_trivially_move_constructible_v<T> && std::is_trivially_move_assignable_v<T>) ||
58           ((!std::is_move_constructible_v<T> || !std::is_move_assignable_v<T>) &&
59            std::is_trivially_copy_constructible_v<T> && std::is_trivially_copy_assignable_v<T>))),
60         "optional<T> is trivially move assignable if T is trivially destructible, and either "
61         "(1) trivially move constructible and trivially move assignable, or "
62         "(2) not move constructible or not move assignable, and "
63         "trivially copy constructible and trivially copy assignable.");
64 };
65 
sink(Args &&...)66 template <class ...Args> static void sink(Args&&...) {}
67 
68 template <class ...TestTypes>
69 struct DoTestsMetafunction {
DoTestsMetafunctionDoTestsMetafunction70     DoTestsMetafunction() { sink(SpecialMemberTest<TestTypes>{}...); }
71 };
72 
73 struct TrivialMoveNonTrivialCopy {
74     TrivialMoveNonTrivialCopy() = default;
TrivialMoveNonTrivialCopyTrivialMoveNonTrivialCopy75     TrivialMoveNonTrivialCopy(const TrivialMoveNonTrivialCopy&) {}
76     TrivialMoveNonTrivialCopy(TrivialMoveNonTrivialCopy&&) = default;
operator =TrivialMoveNonTrivialCopy77     TrivialMoveNonTrivialCopy& operator=(const TrivialMoveNonTrivialCopy&) { return *this; }
78     TrivialMoveNonTrivialCopy& operator=(TrivialMoveNonTrivialCopy&&) = default;
79 };
80 
81 struct TrivialCopyNonTrivialMove {
82     TrivialCopyNonTrivialMove() = default;
83     TrivialCopyNonTrivialMove(const TrivialCopyNonTrivialMove&) = default;
TrivialCopyNonTrivialMoveTrivialCopyNonTrivialMove84     TrivialCopyNonTrivialMove(TrivialCopyNonTrivialMove&&) {}
85     TrivialCopyNonTrivialMove& operator=(const TrivialCopyNonTrivialMove&) = default;
operator =TrivialCopyNonTrivialMove86     TrivialCopyNonTrivialMove& operator=(TrivialCopyNonTrivialMove&&) { return *this; }
87 };
88 
main()89 int main()
90 {
91     sink(
92         ImplicitTypes::ApplyTypes<DoTestsMetafunction>{},
93         ExplicitTypes::ApplyTypes<DoTestsMetafunction>{},
94         NonLiteralTypes::ApplyTypes<DoTestsMetafunction>{},
95         NonTrivialTypes::ApplyTypes<DoTestsMetafunction>{},
96         DoTestsMetafunction<TrivialMoveNonTrivialCopy, TrivialCopyNonTrivialMove>{}
97     );
98 }
99