
lcaminiti wrote:
On Sun, Mar 27, 2011 at 11:35 AM, Thomas Heller <thom.heller@googlemail.com> wrote: > On Sunday, March 27, 2011 05:43:39 PM Lorenzo Caminiti wrote: >> On Sun, Mar 27, 2011 at 10:07 AM, Thomas Heller >> <thom.heller@googlemail.com> wrote: >> > The guy who wrote the code could have said: const int factor = 10; > assignment >> > would then be impossible^W not allowed. >> >> Yes, but the use case would be to have factor const *only* within the >> "function" passed to for_each while keeping it mutable in the >> enclosing scope. Therefore, programmers have to declare factor >> not-const within main() and I was wondering if there was a way using >> Phoenix to add the const only locally within the "function" passed to >> for_each (this is done by Boost.Loccal using "constant-binding" as in >> `const bind& factor`). > > Oh right, you can do that ...: > > let(_a = ref(factor))[...]; // <-- bind as non-const reference > let(_a = cref(factor))[...]; // <-- bind as const reference
Yes, I think that is what I was looking for. I will add it to the Boost.Phoenix example.
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 ;) --Lorenzo -- View this message in context: http://boost.2283326.n4.nabble.com/local-Help-for-the-Alternatives-section-t... Sent from the Boost - Dev mailing list archive at Nabble.com.