]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/function/test/rvalues_test.cpp
28545d53af102e71a9eff61ec1d558c69ce9f216
1 // Copyright 2014 Antony Polukhin.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // For more information, see http://www.boost.org
9 #include <boost/function.hpp>
10 #include <boost/move/move.hpp>
11 #include <boost/core/lightweight_test.hpp>
15 #define BOOST_CHECK BOOST_TEST
19 BOOST_MOVABLE_BUT_NOT_COPYABLE(only_movable
)
24 only_movable(BOOST_RV_REF(only_movable
) x
)
31 only_movable
& operator=(BOOST_RV_REF(only_movable
) x
) {
38 explicit only_movable(int value
= 0) : value_(value
), moved_(false) {}
39 int get_value() const { return value_
; }
40 bool is_moved() const { return moved_
; }
44 int one(BOOST_RV_REF(only_movable
) v
) { return v
.get_value(); }
45 only_movable
two(BOOST_RV_REF(only_movable
) t
) {
46 only_movable t1
= boost::move(t
);
47 return BOOST_MOVE_RET(only_movable
, t1
);
50 only_movable
two_sum(BOOST_RV_REF(only_movable
) t1
, BOOST_RV_REF(only_movable
) t2
) {
51 only_movable
ret(t1
.get_value() + t2
.get_value());
52 return BOOST_MOVE_RET(only_movable
, ret
);
56 only_movable
operator()(BOOST_RV_REF(only_movable
) t1
, BOOST_RV_REF(only_movable
) t2
) const {
57 only_movable
ret(t1
.get_value() + t2
.get_value());
58 return BOOST_MOVE_RET(only_movable
, ret
);
62 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
63 int three(std::string
&&) { return 1; }
64 std::string
&& four(std::string
&& s
) { return boost::move(s
); }
69 using boost::function
;
71 function
<int(BOOST_RV_REF(only_movable
))> f1
= one
;
74 BOOST_CHECK(f1(boost::move(om1
)) == 1);
76 function
<only_movable(BOOST_RV_REF(only_movable
))> f2
= two
;
79 only_movable om2_2
= f2(boost::move(om2
));
80 BOOST_CHECK(om2_2
.get_value() == 2);
81 BOOST_CHECK(om2
.is_moved());
84 function
<only_movable(BOOST_RV_REF(only_movable
), BOOST_RV_REF(only_movable
))> f2_sum
= two_sum
;
85 only_movable
om1_sum(1), om2_sum(2);
86 only_movable om2_sum_2
= f2_sum(boost::move(om1_sum
), boost::move(om2_sum
));
87 BOOST_CHECK(om2_sum_2
.get_value() == 3);
92 function
<only_movable(BOOST_RV_REF(only_movable
), BOOST_RV_REF(only_movable
))> f2_sum
= s
;
93 only_movable
om1_sum(1), om2_sum(2);
94 only_movable om2_sum_2
= f2_sum(boost::move(om1_sum
), boost::move(om2_sum
));
95 BOOST_CHECK(om2_sum_2
.get_value() == 3);
98 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
99 function
<int(std::string
&&)> f3
= three
;
100 function
<std::string
&& (std::string
&& s
)> f4
= four
;
102 f3(std::string("Hello"));
103 BOOST_CHECK(f4(std::string("world")) == "world");
106 return boost::report_errors();