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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // struct identity;
12 
13 #include <functional>
14 
15 #include <cassert>
16 #include <concepts>
17 
18 #include "MoveOnly.h"
19 
20 static_assert(std::semiregular<std::identity>);
21 static_assert(requires { typename std::identity::is_transparent; });
22 
23 constexpr bool test() {
24   std::identity id;
25   int i = 42;
26   assert(id(i) == 42);
27   assert(id(std::move(i)) == 42);
28 
29   MoveOnly m1 = 2;
30   MoveOnly m2 = id(std::move(m1));
31   assert(m2.get() == 2);
32 
33   assert(&id(i) == &i);
34   static_assert(&id(id) == &id);
35 
36   const std::identity idc;
37   assert(idc(1) == 1);
38   assert(std::move(id)(1) == 1);
39   assert(std::move(idc)(1) == 1);
40 
41   id = idc; // run-time checks assignment
42   static_assert(std::is_same_v<decltype(id(i)), int&>);
43   static_assert(std::is_same_v<decltype(id(std::declval<int&&>())), int&&>);
44   static_assert(
45       std::is_same_v<decltype(id(std::declval<int const&>())), int const&>);
46   static_assert(
47       std::is_same_v<decltype(id(std::declval<int const&&>())), int const&&>);
48   static_assert(std::is_same_v<decltype(id(std::declval<int volatile&>())),
49                                int volatile&>);
50   static_assert(std::is_same_v<decltype(id(std::declval<int volatile&&>())),
51                                int volatile&&>);
52   static_assert(
53       std::is_same_v<decltype(id(std::declval<int const volatile&>())),
54                      int const volatile&>);
55   static_assert(
56       std::is_same_v<decltype(id(std::declval<int const volatile&&>())),
57                      int const volatile&&>);
58 
59   struct S {
60     constexpr S() = default;
61     constexpr S(S&&) noexcept(false) {}
62     constexpr S(S const&) noexcept(false) {}
63   };
64   S x;
65   static_assert(noexcept(id(x)));
66   static_assert(noexcept(id(S())));
67 
68   return true;
69 }
70 
71 int main(int, char**) {
72   test();
73   static_assert(test());
74 
75   return 0;
76 }
77