2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <folly/Functional.h>
20 #include <folly/portability/GTest.h>
22 TEST(RvalueReferenceWrapper, MoveAndConvert) {
23 using folly::rvalue_reference_wrapper;
27 rvalue_reference_wrapper<int> rref1(std::move(i1));
28 ASSERT_TRUE(rref1.valid());
29 rvalue_reference_wrapper<int> rref0(std::move(rref1));
30 ASSERT_TRUE(rref0.valid());
31 ASSERT_FALSE(rref1.valid());
32 rref1 = std::move(rref0);
33 ASSERT_FALSE(rref0.valid());
34 ASSERT_TRUE(rref1.valid());
35 const int& r1 = std::move(rref1);
36 ASSERT_FALSE(rref1.valid());
39 // Destructive unwrap to T&&.
41 rvalue_reference_wrapper<int> rref2(std::move(i2));
42 int&& r2 = std::move(rref2);
45 // Destructive unwrap to const T&.
47 rvalue_reference_wrapper<const int> rref3(std::move(i3));
48 const int& r3 = std::move(rref3);
51 // Destructive unwrap to const T&&.
53 rvalue_reference_wrapper<const int> rref4(std::move(i4));
54 const int&& r4 = std::move(rref4);
58 * Things that intentionally do not compile. Copy construction, copy
59 * assignment, unwrap of lvalue reference to wrapper, const violations.
63 rvalue_reference_wrapper<int> rref5(i5);
64 rvalue_reference_wrapper<const int> rref6(i6);
67 const int& r6 = rref6;
69 const rvalue_reference_wrapper<int> rref7(std::move(i7));
70 int& r7 = std::move(rref7);
74 TEST(RvalueReferenceWrapper, Call) {
76 auto callMe = [&](int x, const int& y, int&& z) -> int {
82 int result = folly::rref(std::move(callMe))(a, b, std::move(c));