Boost logo

Boost :

Subject: Re: [boost] [phoenix] comma woes
From: Michael Caisse (boost_at_[hidden])
Date: 2011-03-26 19:26:08


On 03/26/2011 04:07 PM, Steven Watanabe wrote:
> AMDG
>
> I was going to post a response to Lorenzo's question using phoenix,
> but I ran into a few issues.
>
> The initial version I wrote compiles, but doesn't work.
>
> #include <boost/phoenix/core.hpp>
> #include <boost/phoenix/operator.hpp>
> #include <iostream>
> #include <vector>
> #include <algorithm>
>
> int main() {
> double sum = 0.0;
> int factor = 10;
>
> using namespace boost::phoenix;
> using namespace boost::phoenix::placeholders;
>
> std::vector<double> v(3);
> v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
> std::for_each(v.begin(), v.end(), (
> ref(sum) += factor * _1,
> ref(std::cout) << "Summed: " << ref(sum) << std::endl
> ));
> return 0;
> }
>
> This is because the overloaded comma operator
> is provided by statement.hpp, not operator.hpp as
> I had erroneously assumed. Can we please make
> sure that the built-in comma operator can't
> get silently called?
>
> After I added #include <boost/phoenix/statement.hpp>,
> it still doesn't compile and I have no idea how to
> make it compile, because it tries to copy std::cout.
> I'm guessing that the result of the comma operator
> is being deduced as a non-reference, since the
> cout part compiles fine by itself.
>
> In Christ,
> Steven Watanabe
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost

Unfortunately this is a common problem (statement.h). I agree that the
silent built-in should be changed.

I have had problems with std::endl and phoenix in the past... this
version compiles fine:

-----------------

#include <boost/phoenix/phoenix.hpp>
#include <boost/phoenix/statement.hpp>
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
     double sum = 0.0;
     int factor = 10;

     using namespace boost::phoenix;
     using namespace boost::phoenix::placeholders;

     std::vector<double> v(3);
     v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
     std::for_each(v.begin(), v.end(), (
                   ref(sum) += factor * _1,
                   std::cout << "Summed: " << ref(sum) << "\n" )
        );
     return 0;
}

-- 
Michael Caisse
Object Modeling Designs
www.objectmodelingdesigns.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk