$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [boost] [local] Help for the Alternatives section
From: Joel de Guzman (joel_at_[hidden])
Date: 2011-03-28 21:56:04
On 3/29/2011 12:37 AM, lcaminiti wrote:
>
> This worked :)) Finally Boost.Local's Phoenix example reads:
>
> #include<boost/spirit/include/phoenix.hpp>
> #include
> #include
> #include
>
> int main() {
>      double sum = 0.0;
>      int factor = 10;
>
>      std::vector v(3);
>      v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
>
>      // Passed as template parameter and also defined at expression level.
>      std::for_each(v.begin(), v.end(), boost::phoenix::let(
>              // Bind `factor` by constant (reference).
>              boost::phoenix::local_names::_f = boost::phoenix::cref(factor))[
>          // Unfortunately, body cannot use C++ statement syntax.
>          // Access `sum` by (non-constant) reference.
>          boost::phoenix::ref(sum) += boost::phoenix::local_names::_f *
>                  boost::phoenix::arg_names::_1,
>          std::cout<<  boost::phoenix::val("Summed: ")<<
>                  boost::phoenix::ref(sum)<<  "\n"
>      ]);
>
>      std::cout<<  sum<<  std::endl;
>      return 0;
> }
>
> BTW, this is pretty powerful stuff ;)
I'd use using declarations to avoid code clutter and make the
code more readable.
int main() {
     double sum = 0.0;
     int factor = 10;
     std::vector v(3);
     v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
     using boost::phoenix::let;
     using boost::phoenix::local_names::_f;
     using boost::phoenix::cref;
     using boost::phoenix::ref;
     using boost::phoenix::arg_names::_1;
     using boost::phoenix::val;
     // Passed as template parameter and also defined at expression level.
     std::for_each(v.begin(), v.end(), let(
             // Bind `factor` by constant (reference).
             _f = cref(factor))[
         // Unfortunately, body cannot use C++ statement syntax.
         // Access `sum` by (non-constant) reference.
         ref(sum) += _f * _1,
         std::cout << val("Summed: ") << ref(sum) << "\n"
     ]);
     std::cout << sum << std::endl;
     return 0;
}
Regards,
-- Joel de Guzman http://www.boostpro.com http://boost-spirit.com