Boost logo

Boost :

Subject: Re: [boost] New libraries implementing C++11 features in C++03
From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2011-11-24 11:52:14

On Fri, Nov 25, 2011 at 3:44 AM, Lorenzo Caminiti <lorcaminiti_at_[hidden]> wrote:
> On Thu, Nov 24, 2011 at 11:40 AM, Dean Michael Berris
> <mikhailberis_at_[hidden]> wrote:
>> int i;
>> bool b;
>> long l;
>> auto local_function = [i, b, l](std::string const & s) {
>>  // do what I want here.
>> }
>> // use local_function somewhere else
>> Hmmm?
>> Or better yet, works now:
>> namespace foo {
>>  void non_local_function(int i, bool b, long l, std::string const & s) {
>>    // do what I want here.
>>  }
>>  void f(std::vector<double> const &v) {
>>    int i;
>>    bool b;
>>    long l;
>>    for_each(begin(v), end(v), bind(&non_local_function, i, b, l, _1));
>>  }
>> }
>> See, no voodoo required here.
>>> Of course, if you don't need to capture any variable from the enclosing function, using Boost.Local will only make your code more ugly.
>> I agree.
>> Now, do I still miss anything?
> Again, you are missing C++03 which will make your local_function
> example above not compile (obviously) and which is the point of this
> thread.

But I did show a solution that works in both C++03 and C++11 which is
to use bind to do it and a non-local function. Phoenix already enables
doing magical things for in-line lambda's.

Actually in all honesty, for any function that's going to be more than
a couple of instructions long, as far as good engineering practice is
concerned (and of course YMMV) you're going to want to make it an
external function anyway so that:

1. You can test it.
2. You can make it externally linked.
3. You can use it again somewhere else (for free, you don't really
have to if you don't want to).


Dean Michael Berris

Boost list run by bdawes at, gregod at, cpdaniel at, john at