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

source code of libcxx/test/std/utilities/function.objects/func.identity/identity.pass.cpp