[phoenix] accumulate with data member binding
Hi there, the following snippet doesn't compile which surprises me a
little. Could somebody tell what's wrong?
#include <algorithm>
#include <vector>
#include <numeric>
#include
Christian Henning wrote:
Hi there, the following snippet doesn't compile which surprises me a little. Could somebody tell what's wrong?
#include <algorithm> #include <vector> #include <numeric>
#include
#include #include using namespace boost::phoenix; using namespace boost::phoenix::arg_names; using namespace std;
struct point { double _prob; double _x; };
int _tmain(int argc, _TCHAR* argv[]) { std::vector< point > d;
double sum_prob = std::accumulate( d.begin() , d.end() , 0.0 , bind( &point::_prob, arg1 ) );
return 0; }
I'm using Visual Studio 2005. This is the error I'm getting:
error C2296: '.*' : illegal, left operand has type 'double'
Rightly so. arg1 is the state. arg2 is the point. You might want to try: bind( &point::_prob, arg2 ) For example, bind( &point::_prob, arg2 ) + arg1 adds all _prob(s) HTH. Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net
Thanks Joel. What does it mean when you say that arg1 is the state and
arg2 is the point? Also, looking in the phoenix documentation I see
that there is an accumulate version. How would I use that one in my
context?
Thanks,
Christian
On Fri, Oct 31, 2008 at 7:52 PM, Joel de Guzman
Christian Henning wrote:
Hi there, the following snippet doesn't compile which surprises me a little. Could somebody tell what's wrong?
#include <algorithm> #include <vector> #include <numeric>
#include
#include #include using namespace boost::phoenix; using namespace boost::phoenix::arg_names; using namespace std;
struct point { double _prob; double _x; };
int _tmain(int argc, _TCHAR* argv[]) { std::vector< point > d;
double sum_prob = std::accumulate( d.begin() , d.end() , 0.0 , bind( &point::_prob, arg1 ) );
return 0; }
I'm using Visual Studio 2005. This is the error I'm getting:
error C2296: '.*' : illegal, left operand has type 'double'
Rightly so. arg1 is the state. arg2 is the point. You might want to try:
bind( &point::_prob, arg2 )
For example,
bind( &point::_prob, arg2 ) + arg1
adds all _prob(s)
HTH.
Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Christian Henning wrote:
Thanks Joel. What does it mean when you say that arg1 is the state and arg2 is the point?
std::accumulate expects a binary function f, with the first arg being the state. In your example, that is the double you passed initially with value 0.0. This is your "accumulator". The second arg (arg2) is the reference to the element in your stl container (a reference to a point). Also, looking in the phoenix documentation I see
that there is an accumulate version. How would I use that one in my context?
See spirit/phoenix/test/algorithm for some examples. For example: void accumulate_test() { using namespace boost::phoenix; using namespace boost::phoenix::arg_names; int array[] = {1,2,3}; BOOST_TEST(accumulate(arg1, 0)(array) == 6); BOOST_TEST(boost::phoenix::accumulate( arg1, 0, std::minus<int>())(array) == -6); return; } HTH. Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net
Joel,
See spirit/phoenix/test/algorithm for some examples. For example:
void accumulate_test() { using namespace boost::phoenix; using namespace boost::phoenix::arg_names; int array[] = {1,2,3}; BOOST_TEST(accumulate(arg1, 0)(array) == 6); BOOST_TEST(boost::phoenix::accumulate( arg1, 0, std::minus<int>())(array) == -6); return; }
I know about that example. I was interested in how I can use that phoenix::accumulate when iterating over my _prob? In essence how can I rewrite the following code to use phoenix's version of accumulate? std::vector< point > d; double sum_prob = std::accumulate( d.begin() , d.end() , 0.0 , bind( &point::_prob, arg2 ) ); Thanks, Christian
Thanks Steven. That worked.
On Fri, Oct 31, 2008 at 9:17 PM, Steven Watanabe
AMDG
Christian Henning wrote:
bind( &point::_prob, arg1 )
std::accumulate takes a binary function object. Try:
arg1 + bind(&point::_prob, arg2)
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hello, Christian. Saturday, November 1, 2008 at 1:15:54 AM you wrote: CH> Hi there, the following snippet doesn't compile which surprises me a CH> little. Could somebody tell what's wrong? Try following: double sum_prob = std::accumulate( d.begin() , d.end() , 0.0 , bind( &point::_prob)(arg1) ); -- Best Regards, Sergey
Sergey, I tried your code but it fails on me when using Visual Studio
9. It creates 18 errors. Here is a snippet:
1>c:\boost\boost\spirit\home\phoenix\core\detail\function_eval.hpp(40)
: error C2825: 'boost::phoenix::detail::function_eval<0>::result
Hello, Christian.
Saturday, November 1, 2008 at 1:15:54 AM you wrote:
CH> Hi there, the following snippet doesn't compile which surprises me a CH> little. Could somebody tell what's wrong?
Try following: double sum_prob = std::accumulate( d.begin() , d.end() , 0.0 , bind( &point::_prob)(arg1) );
-- Best Regards, Sergey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Christian Henning wrote:
Sergey, I tried your code but it fails on me when using Visual Studio 9. It creates 18 errors. Here is a snippet:
To avoid confusion, Sergey suggested a phoenix-1 solution (which is now only used in Spirit1. The code is still wrong because arg1 should be arg2. Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net
participants (5)
-
Christian Henning
-
Joel de Guzman
-
Joel de Guzman
-
Sergey Sadovnikov
-
Steven Watanabe