1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
14 #include <type_traits>
17 #include "test_macros.h"
23 A
source() TEST_NOEXCEPT
{return A();}
24 const A
csource() TEST_NOEXCEPT
{return A();}
28 constexpr bool test_constexpr_forward() {
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;
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()));
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(), "");
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, "");