|
Boost Users : |
Subject: Re: [Boost-users] break and return statement in lambda expression
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2010-07-22 19:06:31
On Thu, Jul 22, 2010 at 6:06 AM, Conoscenza Silente
<abruzzoforteegentile_at_[hidden]> wrote:
> Well
> I was willing to do 2 things
> *Â to use call 'break' to get out of the std::for_each
> *Â to use return to exit out of the class function having this code.
> For the 'break' command now I found other means ( shame on me: I can't use
> break in a std::for_each like if ..typed without too much thinking ..sorry
> )Â but still I don't know how to fix the call to 'return'.
> If I put a call to 'return' in lambda expression inside std::for_each to
> exit the function I have the same error.
Please do not top post...
std::for_each is basically implemented like:
template<typename iter, typename functor
void for_each(iter start, const iter &end, functor f)
{
while(start!=end)
{
f(*start)
}
}
And the phoenix thing creates a function, so this:
(
if_( a_ > b_ )
[
std::cout << _1,
break
]
)
Creates a function equivalent to this:
template<typename t>
void phoenixfunc(t &_1)
{
if( a_ > b_ )
{
std::cout << _1
// place 1
}
}
Now, you are trying to put a break in "place 1", as you can see, break
makes absolutely no sense there. If you try to put a return, only
thing it does is just exit the function, which it is doing anyway, so
it is useless (and since return is not lazy, it does not work anyway).
So again, not quite sure what you are trying to accomplish.
If you are trying to bail out of a for_each early, then you are using
it incorrectly, a std::for_each is designed to iterate over each
element in an iterator, if you want to stop at a point, then perhaps
use std::find. If you really must use std::for_each for whatever
reason, then you have to throw an exception to exit, and catch it
outside of it.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net