[memprof] Move YAML traits to MemProf.h (NFC) (#118668)
[llvm-project.git] / libcxx / test / std / utilities / utility / forward / forward.pass.cpp
blob689e3309a2ffc9b9195049c5a0f7f7fc4e4341e4
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03
11 // test forward
13 #include <utility>
14 #include <type_traits>
15 #include <cassert>
17 #include "test_macros.h"
19 struct A
23 A source() TEST_NOEXCEPT {return A();}
24 const A csource() TEST_NOEXCEPT {return A();}
27 #if TEST_STD_VER > 11
28 constexpr bool test_constexpr_forward() {
29 int x = 42;
30 const int cx = 101;
31 return std::forward<int&>(x) == 42
32 && std::forward<int>(x) == 42
33 && std::forward<const int&>(x) == 42
34 && std::forward<const int>(x) == 42
35 && std::forward<int&&>(x) == 42
36 && std::forward<const int&&>(x) == 42
37 && std::forward<const int&>(cx) == 101
38 && std::forward<const int>(cx) == 101;
40 #endif
42 int main(int, char**)
44 A a;
45 const A ca = A();
47 ((void)a); // Prevent unused warning
48 ((void)ca); // Prevent unused warning
50 static_assert(std::is_same<decltype(std::forward<A&>(a)), A&>::value, "");
51 static_assert(std::is_same<decltype(std::forward<A>(a)), A&&>::value, "");
52 static_assert(std::is_same<decltype(std::forward<A>(source())), A&&>::value, "");
53 ASSERT_NOEXCEPT(std::forward<A&>(a));
54 ASSERT_NOEXCEPT(std::forward<A>(a));
55 ASSERT_NOEXCEPT(std::forward<A>(source()));
57 static_assert(std::is_same<decltype(std::forward<const A&>(a)), const A&>::value, "");
58 static_assert(std::is_same<decltype(std::forward<const A>(a)), const A&&>::value, "");
59 static_assert(std::is_same<decltype(std::forward<const A>(source())), const A&&>::value, "");
60 ASSERT_NOEXCEPT(std::forward<const A&>(a));
61 ASSERT_NOEXCEPT(std::forward<const A>(a));
62 ASSERT_NOEXCEPT(std::forward<const A>(source()));
64 static_assert(std::is_same<decltype(std::forward<const A&>(ca)), const A&>::value, "");
65 static_assert(std::is_same<decltype(std::forward<const A>(ca)), const A&&>::value, "");
66 static_assert(std::is_same<decltype(std::forward<const A>(csource())), const A&&>::value, "");
67 ASSERT_NOEXCEPT(std::forward<const A&>(ca));
68 ASSERT_NOEXCEPT(std::forward<const A>(ca));
69 ASSERT_NOEXCEPT(std::forward<const A>(csource()));
71 #if TEST_STD_VER > 11
73 constexpr int i2 = std::forward<int>(42);
74 static_assert(std::forward<int>(42) == 42, "");
75 static_assert(std::forward<const int&>(i2) == 42, "");
76 static_assert(test_constexpr_forward(), "");
78 #endif
79 #if TEST_STD_VER == 11 && defined(_LIBCPP_VERSION)
80 // Test that std::forward is constexpr in C++11. This is an extension
81 // provided by both libc++ and libstdc++.
83 constexpr int i2 = std::forward<int>(42);
84 static_assert(std::forward<int>(42) == 42, "" );
85 static_assert(std::forward<const int&>(i2) == 42, "");
87 #endif
89 return 0;