1*24c44c37SChristopher Di Bella //===----------------------------------------------------------------------===//
2*24c44c37SChristopher Di Bella //
3*24c44c37SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*24c44c37SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
5*24c44c37SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*24c44c37SChristopher Di Bella //
7*24c44c37SChristopher Di Bella //===----------------------------------------------------------------------===//
8*24c44c37SChristopher Di Bella
9*24c44c37SChristopher Di Bella // UNSUPPORTED: c++03, c++11, c++14, c++17
10*24c44c37SChristopher Di Bella
11*24c44c37SChristopher Di Bella // struct identity;
12*24c44c37SChristopher Di Bella
13*24c44c37SChristopher Di Bella #include <functional>
14*24c44c37SChristopher Di Bella
15*24c44c37SChristopher Di Bella #include <cassert>
16*24c44c37SChristopher Di Bella #include <concepts>
17*24c44c37SChristopher Di Bella
18*24c44c37SChristopher Di Bella #include "MoveOnly.h"
19*24c44c37SChristopher Di Bella
20*24c44c37SChristopher Di Bella static_assert(std::semiregular<std::identity>);
21*24c44c37SChristopher Di Bella static_assert(requires { typename std::identity::is_transparent; });
22*24c44c37SChristopher Di Bella
test()23*24c44c37SChristopher Di Bella constexpr bool test() {
24*24c44c37SChristopher Di Bella std::identity id;
25*24c44c37SChristopher Di Bella int i = 42;
26*24c44c37SChristopher Di Bella assert(id(i) == 42);
27*24c44c37SChristopher Di Bella assert(id(std::move(i)) == 42);
28*24c44c37SChristopher Di Bella
29*24c44c37SChristopher Di Bella MoveOnly m1 = 2;
30*24c44c37SChristopher Di Bella MoveOnly m2 = id(std::move(m1));
31*24c44c37SChristopher Di Bella assert(m2.get() == 2);
32*24c44c37SChristopher Di Bella
33*24c44c37SChristopher Di Bella assert(&id(i) == &i);
34*24c44c37SChristopher Di Bella static_assert(&id(id) == &id);
35*24c44c37SChristopher Di Bella
36*24c44c37SChristopher Di Bella const std::identity idc;
37*24c44c37SChristopher Di Bella assert(idc(1) == 1);
38*24c44c37SChristopher Di Bella assert(std::move(id)(1) == 1);
39*24c44c37SChristopher Di Bella assert(std::move(idc)(1) == 1);
40*24c44c37SChristopher Di Bella
41*24c44c37SChristopher Di Bella id = idc; // run-time checks assignment
42*24c44c37SChristopher Di Bella static_assert(std::is_same_v<decltype(id(i)), int&>);
43*24c44c37SChristopher Di Bella static_assert(std::is_same_v<decltype(id(std::declval<int&&>())), int&&>);
44*24c44c37SChristopher Di Bella static_assert(
45*24c44c37SChristopher Di Bella std::is_same_v<decltype(id(std::declval<int const&>())), int const&>);
46*24c44c37SChristopher Di Bella static_assert(
47*24c44c37SChristopher Di Bella std::is_same_v<decltype(id(std::declval<int const&&>())), int const&&>);
48*24c44c37SChristopher Di Bella static_assert(std::is_same_v<decltype(id(std::declval<int volatile&>())),
49*24c44c37SChristopher Di Bella int volatile&>);
50*24c44c37SChristopher Di Bella static_assert(std::is_same_v<decltype(id(std::declval<int volatile&&>())),
51*24c44c37SChristopher Di Bella int volatile&&>);
52*24c44c37SChristopher Di Bella static_assert(
53*24c44c37SChristopher Di Bella std::is_same_v<decltype(id(std::declval<int const volatile&>())),
54*24c44c37SChristopher Di Bella int const volatile&>);
55*24c44c37SChristopher Di Bella static_assert(
56*24c44c37SChristopher Di Bella std::is_same_v<decltype(id(std::declval<int const volatile&&>())),
57*24c44c37SChristopher Di Bella int const volatile&&>);
58*24c44c37SChristopher Di Bella
59*24c44c37SChristopher Di Bella struct S {
60*24c44c37SChristopher Di Bella constexpr S() = default;
61*24c44c37SChristopher Di Bella constexpr S(S&&) noexcept(false) {}
62*24c44c37SChristopher Di Bella constexpr S(S const&) noexcept(false) {}
63*24c44c37SChristopher Di Bella };
64*24c44c37SChristopher Di Bella S x;
65*24c44c37SChristopher Di Bella static_assert(noexcept(id(x)));
66*24c44c37SChristopher Di Bella static_assert(noexcept(id(S())));
67*24c44c37SChristopher Di Bella
68*24c44c37SChristopher Di Bella return true;
69*24c44c37SChristopher Di Bella }
70*24c44c37SChristopher Di Bella
main(int,char **)71*24c44c37SChristopher Di Bella int main(int, char**) {
72*24c44c37SChristopher Di Bella test();
73*24c44c37SChristopher Di Bella static_assert(test());
74*24c44c37SChristopher Di Bella
75*24c44c37SChristopher Di Bella return 0;
76*24c44c37SChristopher Di Bella }
77