You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.9 KiB

  1. From cbdb354a274c8ab51a15dd43eafd02c2b6a576f0 Mon Sep 17 00:00:00 2001
  2. From: Edward Catmur <edward.catmur@mavensecurities.com>
  3. Date: Wed, 17 Apr 2019 15:05:37 +0100
  4. Subject: [PATCH 1/2] Copy variant cvref when determining result type
  5. Ensures that we can e.g. call apply_visitor on a lvalue variant
  6. ---
  7. boost/variant/detail/apply_visitor_unary.hpp | 9 +++++----
  8. 1 file changed, 5 insertions(+), 4 deletions(-)
  9. --- a/boost/variant/detail/apply_visitor_unary.hpp
  10. +++ b/boost/variant/detail/apply_visitor_unary.hpp
  11. @@ -23,6 +23,7 @@
  12. # include <boost/mpl/size.hpp>
  13. # include <boost/utility/declval.hpp>
  14. # include <boost/core/enable_if.hpp>
  15. +# include <boost/type_traits/copy_cv_ref.hpp>
  16. # include <boost/type_traits/remove_reference.hpp>
  17. # include <boost/variant/detail/has_result_type.hpp>
  18. #endif
  19. @@ -85,7 +86,7 @@ namespace detail { namespace variant {
  20. // This class serves only metaprogramming purposes. none of its methods must be called at runtime!
  21. template <class Visitor, class Variant>
  22. struct result_multideduce1 {
  23. - typedef typename Variant::types types;
  24. + typedef typename remove_reference<Variant>::type::types types;
  25. typedef typename boost::mpl::begin<types>::type begin_it;
  26. typedef typename boost::mpl::advance<
  27. begin_it, boost::mpl::int_<boost::mpl::size<types>::type::value - 1>
  28. @@ -95,14 +96,14 @@ struct result_multideduce1 {
  29. struct deduce_impl {
  30. typedef typename boost::mpl::next<It>::type next_t;
  31. typedef typename boost::mpl::deref<It>::type value_t;
  32. - typedef decltype(true ? boost::declval< Visitor& >()( boost::declval< value_t >() )
  33. + typedef decltype(true ? boost::declval< Visitor& >()( boost::declval< copy_cv_ref_t< value_t, Variant > >() )
  34. : boost::declval< typename deduce_impl<next_t>::type >()) type;
  35. };
  36. template <class Dummy>
  37. struct deduce_impl<last_it, Dummy> {
  38. typedef typename boost::mpl::deref<last_it>::type value_t;
  39. - typedef decltype(boost::declval< Visitor& >()( boost::declval< value_t >() )) type;
  40. + typedef decltype(boost::declval< Visitor& >()( boost::declval< copy_cv_ref_t< value_t, Variant > >() )) type;
  41. };
  42. typedef typename deduce_impl<begin_it>::type type;
  43. @@ -132,7 +133,7 @@ inline decltype(auto) apply_visitor(Visi
  44. boost::detail::variant::has_result_type<Visitor>
  45. >::type* = 0)
  46. {
  47. - boost::detail::variant::result_wrapper1<Visitor, typename remove_reference<Visitable>::type> cpp14_vis(::boost::forward<Visitor>(visitor));
  48. + boost::detail::variant::result_wrapper1<Visitor, Visitable> cpp14_vis(::boost::forward<Visitor>(visitor));
  49. return ::boost::forward<Visitable>(visitable).apply_visitor(cpp14_vis);
  50. }
  51. --- a/libs/variant/test/const_ref_apply_visitor.cpp
  52. +++ b/libs/variant/test/const_ref_apply_visitor.cpp
  53. @@ -217,6 +217,44 @@ void test_cpp14_visitor(const variant_ty
  54. #endif
  55. }
  56. +void test_cpp14_visitor(variant_type& test_var)
  57. +{
  58. + std::cout << "Testing lvalue visitable for c++14\n";
  59. +
  60. + BOOST_TEST(boost::apply_visitor([](auto& v) { return lvalue_rvalue_detector()(v); }, test_var) == "lvalue reference");
  61. +}
  62. +
  63. +void test_cpp14_mutable_visitor(variant_type& test_var)
  64. +{
  65. + std::cout << "Testing lvalue visitable for c++14 with inline mutable lambda\n";
  66. +
  67. + BOOST_TEST(boost::apply_visitor([](auto& v) mutable -> auto { return lvalue_rvalue_detector()(v); }, test_var) == "lvalue reference");
  68. +}
  69. +
  70. +void test_cpp14_visitor(variant_type& test_var, variant_type& test_var2)
  71. +{
  72. + std::cout << "Testing lvalue visitable for c++14\n";
  73. +
  74. + BOOST_TEST(boost::apply_visitor([](auto& v, auto& vv) { return lvalue_rvalue_detector()(v, vv); }, test_var, test_var2)
  75. + == "lvalue reference, lvalue reference");
  76. +}
  77. +
  78. +void test_cpp14_visitor(variant_type& test_var, variant_type& test_var2, variant_type& test_var3)
  79. +{
  80. +#if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_HDR_TUPLE)
  81. + std::cout << "Testing lvalue visitable for c++14\n";
  82. +
  83. + auto result = boost::apply_visitor([](auto& v, auto& t, auto& p) { return lvalue_rvalue_detector()(v, t, p); },
  84. + test_var, test_var2, test_var3);
  85. + std::cout << "result: " << result << std::endl;
  86. + BOOST_TEST(result == "lvalue reference, lvalue reference, lvalue reference");
  87. +#else
  88. + (void)test_var;
  89. + (void)test_var2;
  90. + (void)test_var3;
  91. +#endif
  92. +}
  93. +
  94. void test_cpp14_visitor(variant_type&& test_var)
  95. {
  96. std::cout << "Testing rvalue visitable for c++14\n";
  97. @@ -333,8 +371,14 @@ void run_cpp14_mixed_tests()
  98. void run_cpp14_tests()
  99. {
  100. #ifndef BOOST_NO_CXX14_DECLTYPE_AUTO
  101. + variant_type const c1(10), c2(20), c3(30);
  102. variant_type v1(10), v2(20), v3(30);
  103. + test_cpp14_visitor(c1);
  104. + test_cpp14_mutable_visitor(c1);
  105. + test_cpp14_visitor(c2, c3);
  106. + test_cpp14_visitor(c1, c2, c3);
  107. +
  108. test_cpp14_visitor(v1);
  109. test_cpp14_mutable_visitor(v1);
  110. test_cpp14_visitor(v2, v3);