[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 <boost/spirit/include/phoenix_algorithm.hpp> #include <boost/spirit/include/phoenix_bind.hpp> #include <boost/spirit/include/phoenix_container.hpp> #include <boost/spirit/include/phoenix_core.hpp> #include <boost/spirit/include/phoenix_function.hpp> #include <boost/spirit/include/phoenix_fusion.hpp> #include <boost/spirit/include/phoenix_object.hpp> #include <boost/spirit/include/phoenix_operator.hpp> #include <boost/spirit/include/phoenix_scope.hpp> #include <boost/spirit/include/phoenix_statement.hpp> #include <boost/spirit/include/phoenix_stl.hpp> #include <algorithm> #include <iostream> #include <fstream> #include <vector> struct point { double _prob; double _x; }; int _tmain(int argc, _TCHAR* argv[]) { using namespace std; using namespace boost::phoenix; using namespace boost::phoenix::arg_names; vector< int > ints; // works std::transform( ints.begin() , ints.end() , ints.begin() , arg1 + 1 ); vector< point > points; // doesn't work std::transform( points.begin() , points.end() , points.begin() , bind( &point::_prob, arg1 ) * val( 2.0 ) ); return 0; } The error is: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion) Regards, Christian

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 <watanabesj@gmail.com> wrote:
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 <boost/spirit/include/phoenix_core.hpp> #include <boost/spirit/include/phoenix_operator.hpp> #include <boost/spirit/include/phoenix_bind.hpp> struct point { double _prob; double _x; }; point make_point(double prob, double x) { point const p = { prob, x }; return p; } int main() { namespace phx = boost::phoenix; using namespace phx::arg_names; typedef std::vector<point> points_t; points_t points; points.push_back(make_point(1., 5.)); points.push_back(make_point(2., 6.)); points.push_back(make_point(3., 7.)); points.push_back(make_point(4., 8.)); std::for_each( points.begin(), points.end(), phx::bind(&point::_prob, _1) *= 2. ); for (points_t::const_iterator i = points.begin(), i_end = points.end(); i != i_end; ++i) std::cout << i->_prob << ' ' << i->_x << std::endl; return 0; }

Christian Henning <chhenning <at> gmail.com> writes:
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