break and return statement in lambda expression
Hi All I created a lambda expression inside my std::for_each call. Inside it there is code like this one, but I have building error telling me that error: expected primary-expression before ‘break’ error: expected `]' before ‘break’ Do you know what it is and how to fix it? The same applies to return statement Thanks AFG namespace bl = boost::lambda; int a, b; bl::var_type< int >::type a_( bl::var( a ) ); bl::var_type< int >::type b_( bl::var( b ) ); std::for_each( v.begin(), v.end(), ( if_( a_ > _b_ ) [ std::cout << _1, break ] ));
On Thu, Jul 22, 2010 at 5:36 AM, Conoscenza Silente <abruzzoforteegentile@gmail.com> wrote:
Hi All I created a lambda expression inside my std::for_each call. Inside it there is code like this one, but I have building error telling me that
error: expected primary-expression before ‘break’ error: expected `]' before ‘break’
Do you know what it is and how to fix it? The same applies to return statement Thanks AFG
namespace bl = boost::lambda; int a, b; bl::var_type< int >::type a_( bl::var( a ) ); bl::var_type< int >::type b_( bl::var( b ) );
std::for_each( v.begin(), v.end(), ( if_( a_ > _b_ ) [ std::cout << _1, break ] ));
'break' is not a lazy command, hence it will not work there. What are you even trying to do anyway?
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. On Thu, Jul 22, 2010 at 1:45 PM, OvermindDL1 <overminddl1@gmail.com> wrote:
On Thu, Jul 22, 2010 at 5:36 AM, Conoscenza Silente <abruzzoforteegentile@gmail.com> wrote:
Hi All I created a lambda expression inside my std::for_each call. Inside it there is code like this one, but I have building error telling me that
error: expected primary-expression before ‘break’ error: expected `]' before ‘break’
Do you know what it is and how to fix it? The same applies to return statement Thanks AFG
namespace bl = boost::lambda; int a, b; bl::var_type< int >::type a_( bl::var( a ) ); bl::var_type< int >::type b_( bl::var( b ) );
std::for_each( v.begin(), v.end(), ( if_( a_ > _b_ ) [ std::cout << _1, break ] ));
'break' is not a lazy command, hence it will not work there. What are you even trying to do anyway? _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Thu, Jul 22, 2010 at 6:06 AM, Conoscenza Silente <abruzzoforteegentile@gmail.com> 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.
On Thu, Jul 22, 2010 at 5:06 PM, OvermindDL1 <overminddl1@gmail.com> wrote:
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) } }
Whoops, meant like this: template<typename iter, typename functor void for_each(iter start, const iter &end, functor f) { while(start!=end) { f(*start) ++start; } }
Hi OvermindDL1 You covered all possible usage of it I need. Thanks for the great response. Kind Regards AFG
participants (2)
-
Conoscenza Silente
-
OvermindDL1