mirror of
https://github.com/boostorg/fusion.git
synced 2025-07-29 12:07:36 +02:00
fusion fold and transform passing refs for lvalues as per result_of protocol
[SVN r38039]
This commit is contained in:
@ -145,4 +145,3 @@ import testing ;
|
||||
run sequence/ext_/iterator_range_s.cpp ;
|
||||
explicit iterator_range_s ;
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,10 @@
|
||||
#include <boost/mpl/int.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>
|
||||
|
||||
using boost::mpl::if_;
|
||||
@ -33,7 +37,8 @@ struct add_ints_only
|
||||
template <typename T, typename 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>
|
||||
@ -58,11 +63,16 @@ struct count_ints
|
||||
template <typename T, typename 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
|
||||
if_<
|
||||
is_same<T, int>
|
||||
, typename boost::mpl::next<CountT>::type
|
||||
, CountT
|
||||
is_same<elem, int>
|
||||
, typename boost::mpl::next<state>::type
|
||||
, state
|
||||
>::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
|
||||
main()
|
||||
{
|
||||
@ -130,6 +159,11 @@ main()
|
||||
== "abcde");
|
||||
}
|
||||
|
||||
{
|
||||
vector<int, int> vec(1,2);
|
||||
BOOST_TEST(fusion::fold(vec, 0, lvalue_adder()) == 3);
|
||||
}
|
||||
|
||||
{
|
||||
typedef vector<int, char, int, double> vector_type;
|
||||
vector_type v(12345, 'x', 678910, 3.36);
|
||||
|
@ -19,13 +19,12 @@
|
||||
|
||||
struct square
|
||||
{
|
||||
template<typename T>
|
||||
template<typename Sig>
|
||||
struct result;
|
||||
|
||||
template <typename T>
|
||||
struct result<square(T)>
|
||||
{
|
||||
BOOST_STATIC_ASSERT(!boost::is_reference<T>::value);
|
||||
typedef int type;
|
||||
};
|
||||
|
||||
@ -38,7 +37,7 @@ struct square
|
||||
|
||||
struct add
|
||||
{
|
||||
template<typename T>
|
||||
template<typename Sig>
|
||||
struct result;
|
||||
|
||||
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
|
||||
main()
|
||||
{
|
||||
@ -92,6 +127,20 @@ main()
|
||||
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();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user