unary c++14 rvalue move semantics

This commit is contained in:
diplay
2017-10-13 14:22:55 +03:00
parent ec3c0964f1
commit 3cab439be0
2 changed files with 14 additions and 21 deletions
@@ -167,41 +167,31 @@ struct result_wrapper1
{}
template <class T>
result_type operator()(T& val) const {
return visitor_(val);
result_type operator()(T&& val) const {
return visitor_(::boost::forward<T>(val));
}
};
}} // namespace detail::variant
template <typename Visitor, typename Visitable>
inline decltype(auto) apply_visitor(Visitor& visitor,
#ifdef USE_UNIVERSAL_REF
Visitable&& visitable,
#else
Visitable& visitable,
#endif
inline decltype(auto) apply_visitor(Visitor& visitor, Visitable&& visitable,
typename boost::disable_if<
boost::detail::variant::has_result_type<Visitor>
>::type* = 0)
{
boost::detail::variant::result_wrapper1<Visitor, typename remove_reference<Visitable>::type> cpp14_vis(visitor);
return visitable.apply_visitor(cpp14_vis);
return ::boost::forward<Visitable>(visitable).apply_visitor(cpp14_vis);
}
template <typename Visitor, typename Visitable>
inline decltype(auto) apply_visitor(const Visitor& visitor,
#ifdef USE_UNIVERSAL_REF
Visitable&& visitable,
#else
Visitable& visitable,
#endif
inline decltype(auto) apply_visitor(const Visitor& visitor, Visitable&& visitable,
typename boost::disable_if<
boost::detail::variant::has_result_type<Visitor>
>::type* = 0)
{
boost::detail::variant::result_wrapper1<const Visitor, typename remove_reference<Visitable>::type> cpp14_vis(visitor);
return visitable.apply_visitor(cpp14_vis);
return ::boost::forward<Visitable>(visitable).apply_visitor(cpp14_vis);
}
+8 -5
View File
@@ -161,26 +161,28 @@ void test_rvalue_parameter4(variant_type&& test_var, variant_type&& test_var2, v
#ifndef BOOST_NO_CXX14_DECLTYPE_AUTO
#define FORWARD(x) std::forward<decltype(x)>(x)
void test_cpp14_visitor(const variant_type& test_var)
{
std::cout << "Testing const lvalue visitable for c++14\n";
BOOST_CHECK(boost::apply_visitor([](auto&& v) { return lcs(v); }, test_var) == lcs(test_var));
BOOST_CHECK(boost::apply_visitor([](auto&& v) { return lvalue_rvalue_detector()(FORWARD(v)); }, test_var) == "lvalue reference");
}
void test_cpp14_visitor(const variant_type& test_var, const variant_type& test_var2)
{
std::cout << "Testing const lvalue visitable for c++14\n";
BOOST_CHECK(boost::apply_visitor([](auto&& v, auto&& vv) { return lcs(v) + '+' + lcs(vv); }, test_var, test_var2) == lcs(test_var) + '+' + lcs(test_var2));
BOOST_CHECK(boost::apply_visitor([](auto&& v, auto&& vv) { return lvalue_rvalue_detector()(FORWARD(v), FORWARD(vv)); }, test_var, test_var2)
== "lvalue reference, lvalue reference");
}
void test_cpp14_visitor(variant_type&& test_var)
{
std::cout << "Testing rvalue visitable for c++14\n";
const auto expected_val = lcs(test_var);
BOOST_CHECK(boost::apply_visitor([](auto&& v) { return lcs(v); }, test_var) == expected_val);
BOOST_CHECK(boost::apply_visitor([](auto&& v) { return lvalue_rvalue_detector()(FORWARD(v)); }, std::move(test_var)) == "rvalue reference");
}
void test_cpp14_visitor(variant_type&& test_var, variant_type&& test_var2)
@@ -188,7 +190,8 @@ void test_cpp14_visitor(variant_type&& test_var, variant_type&& test_var2)
std::cout << "Testing rvalue visitable for c++14\n";
const auto expected_val = lcs(test_var) + '+' + lcs(test_var2);
BOOST_CHECK(boost::apply_visitor([](auto&& v, auto&& vv) { return lcs(v) + '+' + lcs(vv); }, std::move(test_var), std::move(test_var2)) == expected_val);
BOOST_CHECK(boost::apply_visitor([](auto&& v, auto&& vv) { return lvalue_rvalue_detector()(FORWARD(v), FORWARD(vv)); }, std::move(test_var), std::move(test_var2))
== "rvalue reference, rvalue reference");
}
#endif