Boost logo

Boost :

Subject: Re: [boost] [local] Help for the Alternatives section
From: Thomas Heller (thom.heller_at_[hidden])
Date: 2011-03-27 10:07:57


On Sunday, March 27, 2011 05:00:03 PM Lorenzo Caminiti wrote:
> On Sun, Mar 27, 2011 at 9:38 AM, Thomas Heller
> <thom.heller_at_[hidden]> wrote:
> > Correct version is:
> >
> > #include <boost/phoenix/phoenix.hpp>
> > // For Phoenix V2 uncomment this line, and comment the above:
> > #include <boost/spirit/include/phoenix.hpp>
> > #include <iostream>
> > #include <vector>
> > #include <algorithm>
> >
> > int main() {
> > double sum = 0.0;
> > int factor = 10;
> >
> > std::vector<double> v(3);
> > v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
> >
> > std::for_each(v.begin(), v.end(), (
> > boost::phoenix::ref(sum) += factor * boost::phoenix::arg_names::_1,
> > std::cout << boost::phoenix::val("Summed: ") <<
> > boost::phoenix::ref(sum) << "\n"
> > ));
> >
> > std::cout << sum << std::endl;
> > return 0;
>
> Yes, this works :) Thanks a lot!
>
> On a separate note, is there any way I can prevent the following
> `factor = 0` from compiling (e.g., making factor a const within the
> Phoenix function expression)?
>
> #include <boost/spirit/include/phoenix.hpp>
> #include <iostream>
> #include <vector>
> #include <algorithm>
>
> int main() {
> double sum = 0.0;
> int factor = 10;
>
> std::vector<double> v(3);
> v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
>
> std::for_each(v.begin(), v.end(), (
> factor = 0, // <==== Can I prevent this with a compiler error?

No, because this is just plain old C++. assigning zero to a value ... there is
nothing what phoenix can do here.
The guy who wrote the code could have said: const int factor = 10; assignment
would then be impossible^W not allowed.


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