1 //-----------------------------------------------------------------------------
2 // boost variant/detail/apply_visitor_unary.hpp header file
3 // See http://www.boost.org for updates, documentation, and revision history.
4 //-----------------------------------------------------------------------------
6 // Copyright (c) 2002-2003 Eric Friedman
7 // Copyright (c) 2014-2019 Antony Polukhin
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #ifndef BOOST_VARIANT_DETAIL_APPLY_VISITOR_UNARY_HPP
14 #define BOOST_VARIANT_DETAIL_APPLY_VISITOR_UNARY_HPP
16 #include <boost/config.hpp>
17 #include <boost/move/utility.hpp>
19 #if !defined(BOOST_NO_CXX14_DECLTYPE_AUTO) && !defined(BOOST_NO_CXX11_DECLTYPE_N3276)
20 # include <boost/mpl/distance.hpp>
21 # include <boost/mpl/advance.hpp>
22 # include <boost/mpl/deref.hpp>
23 # include <boost/mpl/size.hpp>
24 # include <boost/utility/declval.hpp>
25 # include <boost/core/enable_if.hpp>
26 # include <boost/type_traits/copy_cv_ref.hpp>
27 # include <boost/type_traits/remove_reference.hpp>
28 # include <boost/variant/detail/has_result_type.hpp>
33 //////////////////////////////////////////////////////////////////////////
34 // function template apply_visitor(visitor, visitable)
36 // Visits visitable with visitor.
40 // nonconst-visitor version:
43 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
44 template <typename Visitor, typename Visitable>
45 inline typename Visitor::result_type
46 apply_visitor(Visitor& visitor, Visitable&& visitable)
48 return ::boost::forward<Visitable>(visitable).apply_visitor(visitor);
51 template <typename Visitor, typename Visitable>
52 inline typename Visitor::result_type
53 apply_visitor(Visitor& visitor, Visitable& visitable)
55 return visitable.apply_visitor(visitor);
60 // const-visitor version:
63 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
64 template <typename Visitor, typename Visitable>
65 inline typename Visitor::result_type
66 apply_visitor(const Visitor& visitor, Visitable&& visitable)
68 return ::boost::forward<Visitable>(visitable).apply_visitor(visitor);
71 template <typename Visitor, typename Visitable>
72 inline typename Visitor::result_type
73 apply_visitor(const Visitor& visitor, Visitable& visitable)
75 return visitable.apply_visitor(visitor);
80 #if !defined(BOOST_NO_CXX14_DECLTYPE_AUTO) && !defined(BOOST_NO_CXX11_DECLTYPE_N3276)
81 #define BOOST_VARIANT_HAS_DECLTYPE_APPLY_VISITOR_RETURN_TYPE
84 namespace detail { namespace variant {
86 // This class serves only metaprogramming purposes. none of its methods must be called at runtime!
87 template <class Visitor, class Variant>
88 struct result_multideduce1 {
89 typedef typename remove_reference<Variant>::type::types types;
90 typedef typename boost::mpl::begin<types>::type begin_it;
91 typedef typename boost::mpl::advance<
92 begin_it, boost::mpl::int_<boost::mpl::size<types>::type::value - 1>
95 template <class It, class Dummy = void> // avoid explicit specialization in class scope
97 typedef typename boost::mpl::next<It>::type next_t;
98 typedef typename boost::mpl::deref<It>::type value_t;
99 typedef decltype(true ? boost::declval< Visitor& >()( boost::declval< copy_cv_ref_t< value_t, Variant > >() )
100 : boost::declval< typename deduce_impl<next_t>::type >()) type;
103 template <class Dummy>
104 struct deduce_impl<last_it, Dummy> {
105 typedef typename boost::mpl::deref<last_it>::type value_t;
106 typedef decltype(boost::declval< Visitor& >()( boost::declval< copy_cv_ref_t< value_t, Variant > >() )) type;
109 typedef typename deduce_impl<begin_it>::type type;
112 template <class Visitor, class Variant>
113 struct result_wrapper1
115 typedef typename result_multideduce1<Visitor, Variant>::type result_type;
118 explicit result_wrapper1(Visitor&& visitor) BOOST_NOEXCEPT
119 : visitor_(::boost::forward<Visitor>(visitor))
123 result_type operator()(T&& val) const {
124 return visitor_(::boost::forward<T>(val));
128 }} // namespace detail::variant
130 template <typename Visitor, typename Visitable>
131 inline decltype(auto) apply_visitor(Visitor&& visitor, Visitable&& visitable,
132 typename boost::disable_if<
133 boost::detail::variant::has_result_type<Visitor>
136 boost::detail::variant::result_wrapper1<Visitor, Visitable> cpp14_vis(::boost::forward<Visitor>(visitor));
137 return ::boost::forward<Visitable>(visitable).apply_visitor(cpp14_vis);
140 #endif // !defined(BOOST_NO_CXX14_DECLTYPE_AUTO) && !defined(BOOST_NO_CXX11_DECLTYPE_N3276)
144 #endif // BOOST_VARIANT_DETAIL_APPLY_VISITOR_UNARY_HPP