1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03
11 
12 // test forward
13 
14 #include <utility>
15 #include <type_traits>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 
20 struct A
21 {
22 };
23 
source()24 A source() noexcept {return A();}
csource()25 const A csource() noexcept {return A();}
26 
27 
test_constexpr_forward()28 constexpr bool test_constexpr_forward() {
29 #if TEST_STD_VER > 11
30     int x = 42;
31     const int cx = 101;
32     return std::forward<int&>(x)        == 42
33         && std::forward<int>(x)         == 42
34         && std::forward<const int&>(x)  == 42
35         && std::forward<const int>(x)   == 42
36         && std::forward<int&&>(x)       == 42
37         && std::forward<const int&&>(x) == 42
38         && std::forward<const int&>(cx) == 101
39         && std::forward<const int>(cx)  == 101;
40 #else
41     return true;
42 #endif
43 }
44 
main()45 int main()
46 {
47     A a;
48     const A ca = A();
49 
50     ((void)a); // Prevent unused warning
51     ((void)ca); // Prevent unused warning
52 
53     static_assert(std::is_same<decltype(std::forward<A&>(a)), A&>::value, "");
54     static_assert(std::is_same<decltype(std::forward<A>(a)), A&&>::value, "");
55     static_assert(std::is_same<decltype(std::forward<A>(source())), A&&>::value, "");
56     static_assert(noexcept(std::forward<A&>(a)), "");
57     static_assert(noexcept(std::forward<A>(a)), "");
58     static_assert(noexcept(std::forward<A>(source())), "");
59 
60     static_assert(std::is_same<decltype(std::forward<const A&>(a)), const A&>::value, "");
61     static_assert(std::is_same<decltype(std::forward<const A>(a)), const A&&>::value, "");
62     static_assert(std::is_same<decltype(std::forward<const A>(source())), const A&&>::value, "");
63     static_assert(noexcept(std::forward<const A&>(a)), "");
64     static_assert(noexcept(std::forward<const A>(a)), "");
65     static_assert(noexcept(std::forward<const A>(source())), "");
66 
67     static_assert(std::is_same<decltype(std::forward<const A&>(ca)), const A&>::value, "");
68     static_assert(std::is_same<decltype(std::forward<const A>(ca)), const A&&>::value, "");
69     static_assert(std::is_same<decltype(std::forward<const A>(csource())), const A&&>::value, "");
70     static_assert(noexcept(std::forward<const A&>(ca)), "");
71     static_assert(noexcept(std::forward<const A>(ca)), "");
72     static_assert(noexcept(std::forward<const A>(csource())), "");
73 
74 #if TEST_STD_VER > 11
75     {
76     constexpr int i2 = std::forward<int>(42);
77     static_assert(std::forward<int>(42) == 42, "");
78     static_assert(std::forward<const int&>(i2) == 42, "");
79     static_assert(test_constexpr_forward(), "");
80     }
81 #endif
82 #if TEST_STD_VER == 11 && defined(_LIBCPP_VERSION)
83     // Test that std::forward is constexpr in C++11. This is an extension
84     // provided by both libc++ and libstdc++.
85     {
86     constexpr int i2 = std::forward<int>(42);
87     static_assert(std::forward<int>(42) == 42, "" );
88     static_assert(std::forward<const int&>(i2) == 42, "");
89     }
90 #endif
91 }
92