[mpl] Using max_element inside a fold while constructing a vector
Hi, I have been using boost.mpl for the first time recently and have solved a number of tasks with boost.mpl now but have a problem with a bit of code I thought should be working. What is wrong with this bit of code using boost.mpl? typedef vector<int_<0>, int_<3>, int_<2> > v1; // I want to use max_element on a vector I am in the process of constructing using a fold typedef typename fold<v1, vector<int_<0> >, push_back<_1, deref<max_element<_1>::type >::type> >::type v2; // This code below compiles and runs fine to illustrate that max_element on a vector works, but is not what I am after. // I thought _1 in the max_element above would be a vector just as v1 below is, but the one below works and the one above does not. // typedef typename fold<v1, vector<int_<0> >, push_back<_1, deref<max_element<v1>::type >::type> >::type v2; I know this code is only going to create v2 like [0, 0, 0, 0] in this case but this is a smaller part of a larger expression that is failing and I cut it down to the minimum to show the problem at hand. In case it is useful I am using gcc 4.5.3 and boost 1.50
AMDG On 05/29/2013 08:55 PM, Brendon Costa wrote:
I have been using boost.mpl for the first time recently and have solved a number of tasks with boost.mpl now but have a problem with a bit of code I thought should be working.
What is wrong with this bit of code using boost.mpl?
typedef vector<int_<0>, int_<3>, int_<2> > v1;
// I want to use max_element on a vector I am in the process of constructing using a fold typedef typename fold<v1, vector<int_<0> >, push_back<_1, deref<max_element<_1>::type >::type> >::type v2;
try push_back<_1, deref<max_element<_1> > >. i.e. don't use ::type in an MPL Lambda expression. In Christ, Steven Watanabe
try push_back<_1, deref<max_element<_1> > >. i.e. don't use ::type in an MPL Lambda expression.
That doesn't work either. Below is a full code example as I think it should be from your feedback (I have tried in GCC and MSVC2010): #include <boost/mpl/vector.hpp> #include <boost/mpl/push_back.hpp> #include <boost/mpl/placeholders.hpp> #include <boost/mpl/for_each.hpp> #include <boost/mpl/fold.hpp> #include <boost/mpl/max_element.hpp> #include <boost/mpl/deref.hpp> #include <iostream> #include <typeinfo> using namespace boost::mpl; using namespace boost::mpl::placeholders; typedef vector<int_<0>, int_<3>, int_<2> > v1; // I want to use max_element on a vector I am in the process of constructing using a fold typedef fold<v1, vector<int_<0> >, push_back<_1, deref<max_element<_1> > >
::type v2;
struct print_value { template <typename data_tpt> void operator()(data_tpt p) { //Uncommenting this is what I would like but fails to compile //std::cerr << "value: " << data_tpt::value << std::endl; std::cerr << "value: " << typeid(p).name() << std::endl; } }; int main() { for_each<v2,_>(print_value()); return 0; } This produces a compile error in MSVC 2010 and compiles on GCC but produces bad results: value: N4mpl_4int_ILi0EEE value: N5boost3mpl5derefINS0_11max_elementIN4mpl_3argILi1EEENS0_4lessINS4_ILin1EEES7_EEEEEE value: N5boost3mpl5derefINS0_11max_elementIN4mpl_3argILi1EEENS0_4lessINS4_ILin1EEES7_EEEEEE value: N5boost3mpl5derefINS0_11max_elementIN4mpl_3argILi1EEENS0_4lessINS4_ILin1EEES7_EEEEEE I expect all to be similar to: N4mpl_4int_ILi0EEE
AMDG On 05/31/2013 06:35 PM, Brendon Costa wrote:
try push_back<_1, deref<max_element<_1> > >. i.e. don't use ::type in an MPL Lambda expression.
That doesn't work either. Below is a full code example as I think it should be from your feedback (I have tried in GCC and MSVC2010):
What version of Boost are you using? The code doesn't compile with the current trunk because of a bug that prevents max_element from working in lambdas. After I fix that it works fine. In Christ, Steven Watanabe
I am using: * gcc 4.5.3 and boost 1.50 : Compiles but produces unexpected result * MSVC 2010 and boost 1.53 : Doesn't compile On Sunday, 2 June 2013, Steven Watanabe wrote:
AMDG
On 05/31/2013 06:35 PM, Brendon Costa wrote:
try push_back<_1, deref<max_element<_1> > >. i.e. don't use ::type in an MPL Lambda expression.
That doesn't work either. Below is a full code example as I think it should be from your feedback (I have tried in GCC and MSVC2010):
What version of Boost are you using? The code doesn't compile with the current trunk because of a bug that prevents max_element from working in lambdas. After I fix that it works fine.
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Brendon Costa
-
Steven Watanabe