Update of /cvsroot/boost/boost/libs/fusion/test/algorithm
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv9289/algorithm
Modified Files:
fold.cpp transform.cpp
Log Message:
fusion fold and transform passing refs for lvalues as per result_of protocol
Index: fold.cpp
===================================================================
RCS file: /cvsroot/boost/boost/libs/fusion/test/algorithm/fold.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- fold.cpp 13 Jun 2007 20:30:30 -0000 1.5
+++ fold.cpp 19 Jun 2007 21:28:45 -0000 1.6
@@ -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 @@
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 @@
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 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()
{
@@ -131,6 +160,11 @@
}
{
+ 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);
int result = accumulate(v, 0, add_ints_only());
Index: transform.cpp
===================================================================
RCS file: /cvsroot/boost/boost/libs/fusion/test/algorithm/transform.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- transform.cpp 11 Jun 2007 06:59:05 -0000 1.3
+++ transform.cpp 19 Jun 2007 21:28:45 -0000 1.4
@@ -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 add
{
- template<typename T>
+ template<typename Sig>
struct result;
template <typename A, typename B>
@@ -54,6 +53,42 @@
}
};
+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 @@
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();
}
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Boost-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/boost-cvs