[phoenix] another problem with member_variable bind
Hi there, I'm trying to multiply a factor to my probabilities by using
the std::transform algorithm. But it results in a compiler error.
#include
AMDG Christian Henning wrote:
vector< point > points;
// doesn't work std::transform( points.begin() , points.end() , points.begin() , bind( &point::_prob, arg1 ) * val( 2.0 ) );
Certainly, it won't work. You're code does the equivalent of: points[i] = points[i]._prob * 2.0 In Christ, Steven Watanabe
Ok, my bad. What would be your solution?
For instance, I changed over to std::for_each but still need a
separate function to do the actual multiplication plus assignment.
struct point
{
double _prob;
double _x;
};
void mul_assign( double& l, const double& f )
{
l *= f;
}
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
using namespace boost::phoenix;
using namespace boost::phoenix::arg_names;
vector< point > points( 1 );
std::for_each( points.begin()
, points.end()
, bind( &mul_assign, bind( &point::_prob, arg1 ), 2.0 )
);
return 0;
}
Is there a way I could get rid off "mul_assign"?
Thanks,
Christian
On Fri, Nov 7, 2008 at 4:19 PM, Steven Watanabe
AMDG
Christian Henning wrote:
vector< point > points;
// doesn't work std::transform( points.begin() , points.end() , points.begin() , bind( &point::_prob, arg1 ) * val( 2.0 ) );
Certainly, it won't work. You're code does the equivalent of:
points[i] = points[i]._prob * 2.0
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
If std::for_each is acceptable then the following works. Or do you specifically
need std::transform?
//////////////////////
#include <algorithm>
#include <vector>
#include <iostream>
#include
Christian Henning
Hi there, I'm trying to multiply a factor to my probabilities by using the std::transform algorithm. But it results in a compiler error. <snip> The error is:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion)
That's working fine -- the result type of 'bind(&point::_prob, _1) * 2.' is a double, you're telling it to transform into a vector of points, and point doesn't have a double conversion constructor. Transform into a container of doubles and it compiles cleanly.
participants (3)
-
Adam Merz
-
Christian Henning
-
Steven Watanabe