Boost logo

Boost :

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


On Sun, Mar 27, 2011 at 10:07 AM, Thomas Heller
<thom.heller_at_[hidden]> wrote:
>> 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.

Ok, I understand. Thanks.

> 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`).

-- 
Lorenzo

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