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 //===----------------------------------------------------------------------===//
8 
9 // <utility>
10 
11 // template <class T>
12 //     typename conditional
13 //     <
14 //         !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
15 //         const T&,
16 //         T&&
17 //     >::type
18 //     move_if_noexcept(T& x);
19 
20 #include <utility>
21 
22 #include "test_macros.h"
23 
24 class A
25 {
26     A(const A&);
27     A& operator=(const A&);
28 public:
29 
30     A() {}
31     A(A&&) {}
32 };
33 
34 struct legacy
35 {
36     legacy() {}
37     legacy(const legacy&);
38 };
39 
40 int main(int, char**)
41 {
42     int i = 0;
43     const int ci = 0;
44 
45     legacy l;
46     A a;
47     const A ca;
48 
49 #if TEST_STD_VER >= 11
50     static_assert((std::is_same<decltype(std::move_if_noexcept(i)), int&&>::value), "");
51     static_assert((std::is_same<decltype(std::move_if_noexcept(ci)), const int&&>::value), "");
52     static_assert((std::is_same<decltype(std::move_if_noexcept(a)), A&&>::value), "");
53     static_assert((std::is_same<decltype(std::move_if_noexcept(ca)), const A&&>::value), "");
54     static_assert((std::is_same<decltype(std::move_if_noexcept(l)), const legacy&>::value), "");
55 #else  // C++ < 11
56     // In C++03 we don't have noexcept so we can never move :-(
57     static_assert((std::is_same<decltype(std::move_if_noexcept(i)), const int&>::value), "");
58     static_assert((std::is_same<decltype(std::move_if_noexcept(ci)), const int&>::value), "");
59     static_assert((std::is_same<decltype(std::move_if_noexcept(a)), const A&>::value), "");
60     static_assert((std::is_same<decltype(std::move_if_noexcept(ca)), const A&>::value), "");
61     static_assert((std::is_same<decltype(std::move_if_noexcept(l)), const legacy&>::value), "");
62 #endif
63 
64 #if TEST_STD_VER > 11
65     constexpr int i1 = 23;
66     constexpr int i2 = std::move_if_noexcept(i1);
67     static_assert(i2 == 23, "" );
68 #endif
69 
70 
71   return 0;
72 }
73