124c44c37SChristopher Di Bella //===----------------------------------------------------------------------===//
224c44c37SChristopher Di Bella //
324c44c37SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
424c44c37SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
524c44c37SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
624c44c37SChristopher Di Bella //
724c44c37SChristopher Di Bella //===----------------------------------------------------------------------===//
824c44c37SChristopher Di Bella
924c44c37SChristopher Di Bella // UNSUPPORTED: c++03, c++11, c++14, c++17
1024c44c37SChristopher Di Bella
1124c44c37SChristopher Di Bella // struct identity;
1224c44c37SChristopher Di Bella
1324c44c37SChristopher Di Bella #include <functional>
1424c44c37SChristopher Di Bella
1524c44c37SChristopher Di Bella #include <cassert>
1624c44c37SChristopher Di Bella #include <concepts>
17*7afa1598SNikolas Klauser #include <utility>
1824c44c37SChristopher Di Bella
1924c44c37SChristopher Di Bella #include "MoveOnly.h"
2024c44c37SChristopher Di Bella
2124c44c37SChristopher Di Bella static_assert(std::semiregular<std::identity>);
2224c44c37SChristopher Di Bella static_assert(requires { typename std::identity::is_transparent; });
2324c44c37SChristopher Di Bella
test()2424c44c37SChristopher Di Bella constexpr bool test() {
2524c44c37SChristopher Di Bella std::identity id;
2624c44c37SChristopher Di Bella int i = 42;
2724c44c37SChristopher Di Bella assert(id(i) == 42);
2824c44c37SChristopher Di Bella assert(id(std::move(i)) == 42);
2924c44c37SChristopher Di Bella
3024c44c37SChristopher Di Bella MoveOnly m1 = 2;
3124c44c37SChristopher Di Bella MoveOnly m2 = id(std::move(m1));
3224c44c37SChristopher Di Bella assert(m2.get() == 2);
3324c44c37SChristopher Di Bella
3424c44c37SChristopher Di Bella assert(&id(i) == &i);
3524c44c37SChristopher Di Bella static_assert(&id(id) == &id);
3624c44c37SChristopher Di Bella
3724c44c37SChristopher Di Bella const std::identity idc;
3824c44c37SChristopher Di Bella assert(idc(1) == 1);
3924c44c37SChristopher Di Bella assert(std::move(id)(1) == 1);
4024c44c37SChristopher Di Bella assert(std::move(idc)(1) == 1);
4124c44c37SChristopher Di Bella
4224c44c37SChristopher Di Bella id = idc; // run-time checks assignment
4324c44c37SChristopher Di Bella static_assert(std::is_same_v<decltype(id(i)), int&>);
4424c44c37SChristopher Di Bella static_assert(std::is_same_v<decltype(id(std::declval<int&&>())), int&&>);
4524c44c37SChristopher Di Bella static_assert(
4624c44c37SChristopher Di Bella std::is_same_v<decltype(id(std::declval<int const&>())), int const&>);
4724c44c37SChristopher Di Bella static_assert(
4824c44c37SChristopher Di Bella std::is_same_v<decltype(id(std::declval<int const&&>())), int const&&>);
4924c44c37SChristopher Di Bella static_assert(std::is_same_v<decltype(id(std::declval<int volatile&>())),
5024c44c37SChristopher Di Bella int volatile&>);
5124c44c37SChristopher Di Bella static_assert(std::is_same_v<decltype(id(std::declval<int volatile&&>())),
5224c44c37SChristopher Di Bella int volatile&&>);
5324c44c37SChristopher Di Bella static_assert(
5424c44c37SChristopher Di Bella std::is_same_v<decltype(id(std::declval<int const volatile&>())),
5524c44c37SChristopher Di Bella int const volatile&>);
5624c44c37SChristopher Di Bella static_assert(
5724c44c37SChristopher Di Bella std::is_same_v<decltype(id(std::declval<int const volatile&&>())),
5824c44c37SChristopher Di Bella int const volatile&&>);
5924c44c37SChristopher Di Bella
6024c44c37SChristopher Di Bella struct S {
6124c44c37SChristopher Di Bella constexpr S() = default;
6224c44c37SChristopher Di Bella constexpr S(S&&) noexcept(false) {}
6324c44c37SChristopher Di Bella constexpr S(S const&) noexcept(false) {}
6424c44c37SChristopher Di Bella };
6524c44c37SChristopher Di Bella S x;
6624c44c37SChristopher Di Bella static_assert(noexcept(id(x)));
6724c44c37SChristopher Di Bella static_assert(noexcept(id(S())));
6824c44c37SChristopher Di Bella
6924c44c37SChristopher Di Bella return true;
7024c44c37SChristopher Di Bella }
7124c44c37SChristopher Di Bella
main(int,char **)7224c44c37SChristopher Di Bella int main(int, char**) {
7324c44c37SChristopher Di Bella test();
7424c44c37SChristopher Di Bella static_assert(test());
7524c44c37SChristopher Di Bella
7624c44c37SChristopher Di Bella return 0;
7724c44c37SChristopher Di Bella }
78