mirror of
https://github.com/boostorg/fusion.git
synced 2025-07-21 00:02:42 +02:00
fusion fold and transform passing refs for lvalues as per result_of protocol
[SVN r38039]
This commit is contained in:
@ -13,6 +13,9 @@
|
|||||||
#include <boost/fusion/iterator/distance.hpp>
|
#include <boost/fusion/iterator/distance.hpp>
|
||||||
#include <boost/utility/result_of.hpp>
|
#include <boost/utility/result_of.hpp>
|
||||||
|
|
||||||
|
#include <boost/type_traits/add_const.hpp>
|
||||||
|
#include <boost/type_traits/add_reference.hpp>
|
||||||
|
|
||||||
namespace boost { namespace fusion {
|
namespace boost { namespace fusion {
|
||||||
namespace result_of
|
namespace result_of
|
||||||
{
|
{
|
||||||
@ -32,8 +35,11 @@ namespace detail
|
|||||||
|
|
||||||
template <typename Iterator, typename State, typename F>
|
template <typename Iterator, typename State, typename F>
|
||||||
struct fold_apply
|
struct fold_apply
|
||||||
: boost::result_of<F(typename result_of::value_of<Iterator>::type, State)>
|
{
|
||||||
{};
|
typedef typename result_of::deref<Iterator>::type dereferenced;
|
||||||
|
typedef typename add_reference<typename add_const<State>::type>::type lvalue_state;
|
||||||
|
typedef typename boost::result_of<F(dereferenced, lvalue_state)>::type type;
|
||||||
|
};
|
||||||
|
|
||||||
template <typename First, typename Last, typename State, typename F>
|
template <typename First, typename Last, typename State, typename F>
|
||||||
struct static_fold;
|
struct static_fold;
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <boost/type_traits/is_convertible.hpp>
|
#include <boost/type_traits/is_convertible.hpp>
|
||||||
#include <boost/mpl/if.hpp>
|
#include <boost/mpl/if.hpp>
|
||||||
|
#include <boost/type_traits/remove_reference.hpp>
|
||||||
|
|
||||||
namespace boost { namespace fusion { namespace detail
|
namespace boost { namespace fusion { namespace detail
|
||||||
{
|
{
|
||||||
@ -43,14 +44,15 @@ namespace boost { namespace fusion { namespace detail
|
|||||||
replacer(T const& old_value, T const& new_value)
|
replacer(T const& old_value, T const& new_value)
|
||||||
: old_value(old_value), new_value(new_value) {}
|
: old_value(old_value), new_value(new_value) {}
|
||||||
|
|
||||||
template<typename Params>
|
template<typename Sig>
|
||||||
struct result;
|
struct result;
|
||||||
|
|
||||||
template <typename U1, typename U2>
|
template <typename U1, typename U2>
|
||||||
struct result<replacer<U1>(U2)>
|
struct result<replacer<U1>(U2)>
|
||||||
{
|
{
|
||||||
|
typedef typename remove_reference<U2>::type value;
|
||||||
typedef typename
|
typedef typename
|
||||||
mpl::if_<is_convertible<T, U2>, U2, U2 const&>::type
|
mpl::if_<is_convertible<T, value>, value, value const&>::type
|
||||||
type;
|
type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <boost/utility/enable_if.hpp>
|
#include <boost/utility/enable_if.hpp>
|
||||||
#include <boost/mpl/if.hpp>
|
#include <boost/mpl/if.hpp>
|
||||||
|
#include <boost/type_traits/remove_reference.hpp>
|
||||||
|
|
||||||
namespace boost { namespace fusion { namespace detail
|
namespace boost { namespace fusion { namespace detail
|
||||||
{
|
{
|
||||||
@ -49,8 +50,9 @@ namespace boost { namespace fusion { namespace detail
|
|||||||
template <typename F1, typename T1, typename U>
|
template <typename F1, typename T1, typename U>
|
||||||
struct result<replacer_if<F1, T1>(U)>
|
struct result<replacer_if<F1, T1>(U)>
|
||||||
{
|
{
|
||||||
|
typedef typename remove_reference<U>::type value;
|
||||||
typedef typename
|
typedef typename
|
||||||
mpl::if_<is_convertible<T, U>, U, U const&>::type
|
mpl::if_<is_convertible<T, value>, value, value const&>::type
|
||||||
type;
|
type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ namespace boost { namespace fusion
|
|||||||
struct apply
|
struct apply
|
||||||
{
|
{
|
||||||
typedef typename
|
typedef typename
|
||||||
result_of::value_of<typename Iterator::first_type>::type
|
result_of::deref<typename Iterator::first_type>::type
|
||||||
value_type;
|
value_type;
|
||||||
|
|
||||||
typedef detail::apply_transform_result<typename Iterator::transform_type> transform_type;
|
typedef detail::apply_transform_result<typename Iterator::transform_type> transform_type;
|
||||||
@ -52,10 +52,10 @@ namespace boost { namespace fusion
|
|||||||
struct apply
|
struct apply
|
||||||
{
|
{
|
||||||
typedef typename
|
typedef typename
|
||||||
result_of::value_of<typename Iterator::first1_type>::type
|
result_of::deref<typename Iterator::first1_type>::type
|
||||||
value1_type;
|
value1_type;
|
||||||
typedef typename
|
typedef typename
|
||||||
result_of::value_of<typename Iterator::first2_type>::type
|
result_of::deref<typename Iterator::first2_type>::type
|
||||||
value2_type;
|
value2_type;
|
||||||
|
|
||||||
typedef detail::apply_transform_result<typename Iterator::transform_type> transform_type;
|
typedef detail::apply_transform_result<typename Iterator::transform_type> transform_type;
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include <boost/fusion/sequence/view/zip_view/zip_view_iterator_fwd.hpp>
|
#include <boost/fusion/sequence/view/zip_view/zip_view_iterator_fwd.hpp>
|
||||||
#include <boost/fusion/iterator/advance.hpp>
|
#include <boost/fusion/iterator/advance.hpp>
|
||||||
#include <boost/fusion/algorithm/transformation/transform.hpp>
|
#include <boost/fusion/algorithm/transformation/transform.hpp>
|
||||||
|
#include <boost/type_traits/remove_reference.hpp>
|
||||||
|
|
||||||
namespace boost { namespace fusion {
|
namespace boost { namespace fusion {
|
||||||
|
|
||||||
@ -21,13 +22,15 @@ namespace boost { namespace fusion {
|
|||||||
template<typename N>
|
template<typename N>
|
||||||
struct poly_advance
|
struct poly_advance
|
||||||
{
|
{
|
||||||
template<typename T>
|
template<typename Sig>
|
||||||
struct result;
|
struct result;
|
||||||
|
|
||||||
template<typename N1, typename It>
|
template<typename N1, typename It>
|
||||||
struct result<poly_advance<N1>(It)>
|
struct result<poly_advance<N1>(It)>
|
||||||
: result_of::advance<It,N>
|
{
|
||||||
{};
|
typedef typename remove_reference<It>::type it;
|
||||||
|
typedef typename result_of::advance<it,N>::type type;
|
||||||
|
};
|
||||||
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
typename result<poly_advance(It)>::type
|
typename result<poly_advance(It)>::type
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
#include <boost/mpl/eval_if.hpp>
|
#include <boost/mpl/eval_if.hpp>
|
||||||
#include <boost/mpl/identity.hpp>
|
#include <boost/mpl/identity.hpp>
|
||||||
#include <boost/type_traits/is_same.hpp>
|
#include <boost/type_traits/is_same.hpp>
|
||||||
|
#include <boost/type_traits/remove_reference.hpp>
|
||||||
|
#include <boost/type_traits/remove_const.hpp>
|
||||||
|
|
||||||
namespace boost { namespace fusion {
|
namespace boost { namespace fusion {
|
||||||
|
|
||||||
@ -25,15 +27,19 @@ namespace boost { namespace fusion {
|
|||||||
{
|
{
|
||||||
struct poly_deref
|
struct poly_deref
|
||||||
{
|
{
|
||||||
template<typename T>
|
template<typename Sig>
|
||||||
struct result;
|
struct result;
|
||||||
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
struct result<poly_deref(It)>
|
struct result<poly_deref(It)>
|
||||||
: mpl::eval_if<is_same<It, unused_type>,
|
{
|
||||||
|
typedef typename remove_const<
|
||||||
|
typename remove_reference<It>::type>::type it;
|
||||||
|
|
||||||
|
typedef typename mpl::eval_if<is_same<it, unused_type>,
|
||||||
mpl::identity<unused_type>,
|
mpl::identity<unused_type>,
|
||||||
result_of::deref<It> >
|
result_of::deref<it> >::type type;
|
||||||
{};
|
};
|
||||||
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
typename result<poly_deref(It)>::type
|
typename result<poly_deref(It)>::type
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
#include <boost/mpl/eval_if.hpp>
|
#include <boost/mpl/eval_if.hpp>
|
||||||
#include <boost/mpl/identity.hpp>
|
#include <boost/mpl/identity.hpp>
|
||||||
#include <boost/type_traits/is_same.hpp>
|
#include <boost/type_traits/is_same.hpp>
|
||||||
|
#include <boost/type_traits/remove_reference.hpp>
|
||||||
|
#include <boost/type_traits/remove_const.hpp>
|
||||||
|
|
||||||
namespace boost { namespace fusion {
|
namespace boost { namespace fusion {
|
||||||
|
|
||||||
@ -24,15 +26,19 @@ namespace boost { namespace fusion {
|
|||||||
{
|
{
|
||||||
struct poly_next
|
struct poly_next
|
||||||
{
|
{
|
||||||
template<typename T>
|
template<typename Sig>
|
||||||
struct result;
|
struct result;
|
||||||
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
struct result<poly_next(It)>
|
struct result<poly_next(It)>
|
||||||
: mpl::eval_if<is_same<It, unused_type>,
|
{
|
||||||
|
typedef typename remove_const<
|
||||||
|
typename remove_reference<It>::type>::type it;
|
||||||
|
|
||||||
|
typedef typename mpl::eval_if<is_same<it, unused_type>,
|
||||||
mpl::identity<unused_type>,
|
mpl::identity<unused_type>,
|
||||||
result_of::next<It> >
|
result_of::next<it> >::type type;
|
||||||
{};
|
};
|
||||||
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
typename result<poly_next(It)>::type
|
typename result<poly_next(It)>::type
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
#include <boost/mpl/eval_if.hpp>
|
#include <boost/mpl/eval_if.hpp>
|
||||||
#include <boost/mpl/identity.hpp>
|
#include <boost/mpl/identity.hpp>
|
||||||
#include <boost/type_traits/is_same.hpp>
|
#include <boost/type_traits/is_same.hpp>
|
||||||
|
#include <boost/type_traits/remove_reference.hpp>
|
||||||
|
#include <boost/type_traits/remove_const.hpp>
|
||||||
|
|
||||||
namespace boost { namespace fusion {
|
namespace boost { namespace fusion {
|
||||||
|
|
||||||
@ -24,15 +26,18 @@ namespace boost { namespace fusion {
|
|||||||
{
|
{
|
||||||
struct poly_prior
|
struct poly_prior
|
||||||
{
|
{
|
||||||
template<typename T>
|
template<typename Sig>
|
||||||
struct result;
|
struct result;
|
||||||
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
struct result<poly_prior(It)>
|
struct result<poly_prior(It)>
|
||||||
: mpl::eval_if<is_same<It, unused_type>,
|
{
|
||||||
|
typedef typename remove_const<
|
||||||
|
typename remove_reference<It>::type>::type it;
|
||||||
|
typedef typename mpl::eval_if<is_same<it, unused_type>,
|
||||||
mpl::identity<unused_type>,
|
mpl::identity<unused_type>,
|
||||||
result_of::prior<It> >
|
result_of::prior<it> >::type type;
|
||||||
{};
|
};
|
||||||
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
typename result<poly_prior(It)>::type
|
typename result<poly_prior(It)>::type
|
||||||
|
@ -39,15 +39,17 @@ namespace boost { namespace fusion {
|
|||||||
|
|
||||||
struct strictest_traversal_impl
|
struct strictest_traversal_impl
|
||||||
{
|
{
|
||||||
template<typename T>
|
template<typename Sig>
|
||||||
struct result;
|
struct result;
|
||||||
|
|
||||||
template<typename Next, typename StrictestSoFar>
|
template<typename Next, typename StrictestSoFar>
|
||||||
struct result<strictest_traversal_impl(Next, StrictestSoFar)>
|
struct result<strictest_traversal_impl(Next, StrictestSoFar)>
|
||||||
{
|
{
|
||||||
typedef StrictestSoFar tag1;
|
typedef typename remove_reference<Next>::type next_value;
|
||||||
typedef typename traits::category_of<
|
typedef typename remove_reference<StrictestSoFar>::type strictest_so_far;
|
||||||
typename remove_reference<Next>::type>::type tag2;
|
|
||||||
|
typedef strictest_so_far tag1;
|
||||||
|
typedef typename traits::category_of<next_value>::type tag2;
|
||||||
|
|
||||||
typedef typename stricter_traversal<tag1,tag2>::type type;
|
typedef typename stricter_traversal<tag1,tag2>::type type;
|
||||||
};
|
};
|
||||||
|
@ -73,8 +73,11 @@ namespace boost { namespace fusion {
|
|||||||
|
|
||||||
template<typename Lhs, typename Rhs>
|
template<typename Lhs, typename Rhs>
|
||||||
struct result<poly_min(Lhs, Rhs)>
|
struct result<poly_min(Lhs, Rhs)>
|
||||||
: mpl::min<Lhs, Rhs>
|
{
|
||||||
{};
|
typedef typename remove_reference<Lhs>::type lhs;
|
||||||
|
typedef typename remove_reference<Rhs>::type rhs;
|
||||||
|
typedef typename mpl::min<lhs, rhs>::type type;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Sequences>
|
template<typename Sequences>
|
||||||
|
@ -145,4 +145,3 @@ import testing ;
|
|||||||
run sequence/ext_/iterator_range_s.cpp ;
|
run sequence/ext_/iterator_range_s.cpp ;
|
||||||
explicit iterator_range_s ;
|
explicit iterator_range_s ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,10 @@
|
|||||||
#include <boost/mpl/int.hpp>
|
#include <boost/mpl/int.hpp>
|
||||||
#include <boost/mpl/vector.hpp>
|
#include <boost/mpl/vector.hpp>
|
||||||
|
|
||||||
|
#include <boost/type_traits/remove_const.hpp>
|
||||||
|
#include <boost/type_traits/remove_reference.hpp>
|
||||||
|
#include <boost/type_traits/is_reference.hpp>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using boost::mpl::if_;
|
using boost::mpl::if_;
|
||||||
@ -33,7 +37,8 @@ struct add_ints_only
|
|||||||
template <typename T, typename State>
|
template <typename T, typename State>
|
||||||
struct result<add_ints_only(T,State)>
|
struct result<add_ints_only(T,State)>
|
||||||
{
|
{
|
||||||
typedef State type;
|
typedef typename boost::remove_const<
|
||||||
|
typename boost::remove_reference<State>::type>::type type;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T, typename State>
|
template <typename T, typename State>
|
||||||
@ -58,11 +63,16 @@ struct count_ints
|
|||||||
template <typename T, typename CountT>
|
template <typename T, typename CountT>
|
||||||
struct result<count_ints(T,CountT)>
|
struct result<count_ints(T,CountT)>
|
||||||
{
|
{
|
||||||
|
typedef typename boost::remove_const<
|
||||||
|
typename boost::remove_reference<T>::type>::type elem;
|
||||||
|
typedef typename boost::remove_const<
|
||||||
|
typename boost::remove_reference<CountT>::type>::type state;
|
||||||
|
|
||||||
typedef typename
|
typedef typename
|
||||||
if_<
|
if_<
|
||||||
is_same<T, int>
|
is_same<elem, int>
|
||||||
, typename boost::mpl::next<CountT>::type
|
, typename boost::mpl::next<state>::type
|
||||||
, CountT
|
, state
|
||||||
>::type
|
>::type
|
||||||
type;
|
type;
|
||||||
};
|
};
|
||||||
@ -86,6 +96,25 @@ struct appender
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct lvalue_adder
|
||||||
|
{
|
||||||
|
template<typename Sig>
|
||||||
|
struct result;
|
||||||
|
|
||||||
|
template<typename T0, typename T1>
|
||||||
|
struct result<lvalue_adder(T0&, T1)>
|
||||||
|
{
|
||||||
|
// Second argument still needs to support rvalues - see definition of fusion::fold
|
||||||
|
typedef T0 type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T0, typename T1>
|
||||||
|
T0 operator()(T0& lhs, T1 const& rhs) const
|
||||||
|
{
|
||||||
|
return lhs + rhs;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
int
|
int
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
@ -130,6 +159,11 @@ main()
|
|||||||
== "abcde");
|
== "abcde");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
vector<int, int> vec(1,2);
|
||||||
|
BOOST_TEST(fusion::fold(vec, 0, lvalue_adder()) == 3);
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
typedef vector<int, char, int, double> vector_type;
|
typedef vector<int, char, int, double> vector_type;
|
||||||
vector_type v(12345, 'x', 678910, 3.36);
|
vector_type v(12345, 'x', 678910, 3.36);
|
||||||
|
@ -19,13 +19,12 @@
|
|||||||
|
|
||||||
struct square
|
struct square
|
||||||
{
|
{
|
||||||
template<typename T>
|
template<typename Sig>
|
||||||
struct result;
|
struct result;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct result<square(T)>
|
struct result<square(T)>
|
||||||
{
|
{
|
||||||
BOOST_STATIC_ASSERT(!boost::is_reference<T>::value);
|
|
||||||
typedef int type;
|
typedef int type;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -38,7 +37,7 @@ struct square
|
|||||||
|
|
||||||
struct add
|
struct add
|
||||||
{
|
{
|
||||||
template<typename T>
|
template<typename Sig>
|
||||||
struct result;
|
struct result;
|
||||||
|
|
||||||
template <typename A, typename B>
|
template <typename A, typename B>
|
||||||
@ -54,6 +53,42 @@ struct add
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct unary_lvalue_transform
|
||||||
|
{
|
||||||
|
template<typename Sig>
|
||||||
|
struct result;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct result<unary_lvalue_transform(T&)>
|
||||||
|
{
|
||||||
|
typedef T* type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T* operator()(T& t) const
|
||||||
|
{
|
||||||
|
return &t;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct binary_lvalue_transform
|
||||||
|
{
|
||||||
|
template<typename Sig>
|
||||||
|
struct result;
|
||||||
|
|
||||||
|
template<typename T0, typename T1>
|
||||||
|
struct result<binary_lvalue_transform(T0&,T1&)>
|
||||||
|
{
|
||||||
|
typedef T0* type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T0, typename T1>
|
||||||
|
T0* operator()(T0& t0, T1&) const
|
||||||
|
{
|
||||||
|
return &t0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
int
|
int
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
@ -92,6 +127,20 @@ main()
|
|||||||
BOOST_TEST((transform(tup1, tup2, add()) == make_vector(5, 7, 9)));
|
BOOST_TEST((transform(tup1, tup2, add()) == make_vector(5, 7, 9)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// Unary transform that requires lvalues, just check compilation
|
||||||
|
vector<int, int, int> tup1(1, 2, 3);
|
||||||
|
BOOST_TEST(at_c<0>(transform(tup1, unary_lvalue_transform())) == &at_c<0>(tup1));
|
||||||
|
BOOST_TEST(*begin(transform(tup1, unary_lvalue_transform())) == &at_c<0>(tup1));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
vector<int, int, int> tup1(1, 2, 3);
|
||||||
|
vector<int, int, int> tup2(4, 5, 6);
|
||||||
|
BOOST_TEST(at_c<0>(transform(tup1, tup2, binary_lvalue_transform())) == &at_c<0>(tup1));
|
||||||
|
BOOST_TEST(*begin(transform(tup1, tup2, binary_lvalue_transform())) == &at_c<0>(tup1));
|
||||||
|
}
|
||||||
|
|
||||||
return boost::report_errors();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user