15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier
9*31cbe0f2SLouis Dionne // UNSUPPORTED: c++03
10c24e6dd3SEric Fiselier
115a83710eSEric Fiselier // test forward
125a83710eSEric Fiselier
135a83710eSEric Fiselier #include <utility>
14c24e6dd3SEric Fiselier #include <type_traits>
155a83710eSEric Fiselier #include <cassert>
165a83710eSEric Fiselier
17c24e6dd3SEric Fiselier #include "test_macros.h"
18c24e6dd3SEric Fiselier
195a83710eSEric Fiselier struct A
205a83710eSEric Fiselier {
215a83710eSEric Fiselier };
225a83710eSEric Fiselier
source()23000f25a3SEric Fiselier A source() TEST_NOEXCEPT {return A();}
csource()24000f25a3SEric Fiselier const A csource() TEST_NOEXCEPT {return A();}
255a83710eSEric Fiselier
265a83710eSEric Fiselier
27c24e6dd3SEric Fiselier #if TEST_STD_VER > 11
test_constexpr_forward()28000f25a3SEric Fiselier constexpr bool test_constexpr_forward() {
29c24e6dd3SEric Fiselier int x = 42;
30c24e6dd3SEric Fiselier const int cx = 101;
31c24e6dd3SEric Fiselier return std::forward<int&>(x) == 42
32c24e6dd3SEric Fiselier && std::forward<int>(x) == 42
33c24e6dd3SEric Fiselier && std::forward<const int&>(x) == 42
34c24e6dd3SEric Fiselier && std::forward<const int>(x) == 42
35c24e6dd3SEric Fiselier && std::forward<int&&>(x) == 42
36c24e6dd3SEric Fiselier && std::forward<const int&&>(x) == 42
37c24e6dd3SEric Fiselier && std::forward<const int&>(cx) == 101
38c24e6dd3SEric Fiselier && std::forward<const int>(cx) == 101;
39c24e6dd3SEric Fiselier }
40000f25a3SEric Fiselier #endif
415a83710eSEric Fiselier
main(int,char **)422df59c50SJF Bastien int main(int, char**)
435a83710eSEric Fiselier {
445a83710eSEric Fiselier A a;
455a83710eSEric Fiselier const A ca = A();
465a83710eSEric Fiselier
47fb42f4c4SEric Fiselier ((void)a); // Prevent unused warning
48fb42f4c4SEric Fiselier ((void)ca); // Prevent unused warning
49fb42f4c4SEric Fiselier
50c24e6dd3SEric Fiselier static_assert(std::is_same<decltype(std::forward<A&>(a)), A&>::value, "");
51c24e6dd3SEric Fiselier static_assert(std::is_same<decltype(std::forward<A>(a)), A&&>::value, "");
52c24e6dd3SEric Fiselier static_assert(std::is_same<decltype(std::forward<A>(source())), A&&>::value, "");
53000f25a3SEric Fiselier ASSERT_NOEXCEPT(std::forward<A&>(a));
54000f25a3SEric Fiselier ASSERT_NOEXCEPT(std::forward<A>(a));
55000f25a3SEric Fiselier ASSERT_NOEXCEPT(std::forward<A>(source()));
565a83710eSEric Fiselier
57c24e6dd3SEric Fiselier static_assert(std::is_same<decltype(std::forward<const A&>(a)), const A&>::value, "");
58c24e6dd3SEric Fiselier static_assert(std::is_same<decltype(std::forward<const A>(a)), const A&&>::value, "");
59c24e6dd3SEric Fiselier static_assert(std::is_same<decltype(std::forward<const A>(source())), const A&&>::value, "");
60000f25a3SEric Fiselier ASSERT_NOEXCEPT(std::forward<const A&>(a));
61000f25a3SEric Fiselier ASSERT_NOEXCEPT(std::forward<const A>(a));
62000f25a3SEric Fiselier ASSERT_NOEXCEPT(std::forward<const A>(source()));
635a83710eSEric Fiselier
64c24e6dd3SEric Fiselier static_assert(std::is_same<decltype(std::forward<const A&>(ca)), const A&>::value, "");
65c24e6dd3SEric Fiselier static_assert(std::is_same<decltype(std::forward<const A>(ca)), const A&&>::value, "");
66c24e6dd3SEric Fiselier static_assert(std::is_same<decltype(std::forward<const A>(csource())), const A&&>::value, "");
67000f25a3SEric Fiselier ASSERT_NOEXCEPT(std::forward<const A&>(ca));
68000f25a3SEric Fiselier ASSERT_NOEXCEPT(std::forward<const A>(ca));
69000f25a3SEric Fiselier ASSERT_NOEXCEPT(std::forward<const A>(csource()));
705a83710eSEric Fiselier
71c24e6dd3SEric Fiselier #if TEST_STD_VER > 11
72c24e6dd3SEric Fiselier {
735a83710eSEric Fiselier constexpr int i2 = std::forward<int>(42);
74c24e6dd3SEric Fiselier static_assert(std::forward<int>(42) == 42, "");
75c24e6dd3SEric Fiselier static_assert(std::forward<const int&>(i2) == 42, "");
76c24e6dd3SEric Fiselier static_assert(test_constexpr_forward(), "");
77c24e6dd3SEric Fiselier }
78c24e6dd3SEric Fiselier #endif
79c24e6dd3SEric Fiselier #if TEST_STD_VER == 11 && defined(_LIBCPP_VERSION)
80c24e6dd3SEric Fiselier // Test that std::forward is constexpr in C++11. This is an extension
81c24e6dd3SEric Fiselier // provided by both libc++ and libstdc++.
82c24e6dd3SEric Fiselier {
83c24e6dd3SEric Fiselier constexpr int i2 = std::forward<int>(42);
84c24e6dd3SEric Fiselier static_assert(std::forward<int>(42) == 42, "" );
85c24e6dd3SEric Fiselier static_assert(std::forward<const int&>(i2) == 42, "");
86c24e6dd3SEric Fiselier }
875a83710eSEric Fiselier #endif
882df59c50SJF Bastien
892df59c50SJF Bastien return 0;
905a83710eSEric Fiselier }
91