Boost logo

Boost :

Subject: Re: [boost] [local] Help for the Alternatives section
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2011-03-27 11:00:03


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?
        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;
}

Thanks,

--
Lorenzo

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