Is there a way to shorten this?
More specifically, is there a way to as such:Code:for(foo == bar || foo == pub) { // Incert drunken code here }
Any help would be appreciatedCode:for(foo == bar||pub){}
-Thank You
This is a discussion on for loop, multiple checks within the C++ Programming forums, part of the General Programming Boards category; Is there a way to shorten this? Code: for(foo == bar || foo == pub) { // Incert drunken code ...
Is there a way to shorten this?
More specifically, is there a way to as such:Code:for(foo == bar || foo == pub) { // Incert drunken code here }
Any help would be appreciatedCode:for(foo == bar||pub){}
-Thank You
Nope.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Friends don't let friends code drunk.
errm oh wait thats not shorterCode:for(foo == bar) { for(foo == pub) { } break }
You might be able to use bit checks:
Code:enum { bar = 1 << 0, pub = 1 << 1 }; while (foo & (bar | pub)) ...
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
It can be done, it can be approximated, but how much work are you willing to put into making this expression style work?
I only ask because the code below illustrates about 1/200 of what you'd actually need to cleanly pull off this semantic modification.
Soma
Code:template < typename type_F > struct lhs_type { lhs_type ( const type_F & value_f ): value_m(value_f) { } const type_F & value_m; }; template < typename type_F > lhs_type<type_F> lhs ( const type_F & lhs_f ) { return(lhs_type<type_F>(lhs_f)); } template < typename type_F > struct rhs_type { rhs_type ( const type_F & value_f ): value_m(value_f) { } const type_F & value_m; }; template < typename type_F > rhs_type<type_F> rhs ( const type_F & rhs_f ) { return(rhs_type<type_F>(rhs_f)); } template < typename lhs_F, typename rhs_F > struct lor_type { lor_type ( const lhs_F & lhs_f, const rhs_F & rhs_f ): lhs_m(lhs_f), rhs_m(rhs_f) { } const lhs_F & lhs_m; const rhs_F & rhs_m; }; template < typename lhs_F, typename rhs_F > lor_type<lhs_F, rhs_F> operator || ( const lhs_type<lhs_F> & lhs_f, const rhs_type<rhs_F> & rhs_f ) { return(lor_type<lhs_F, rhs_F>(lhs_f.value_m, rhs_f.value_m)); } template < typename lhs_F, typename rhs_lhs_F, typename rhs_rhs_F > bool operator == ( const lhs_F & lhs_f, const lor_type<rhs_lhs_F, rhs_rhs_F> & rhs_f ) { return((lhs_f == rhs_f.lhs_m) || (lhs_f == rhs_f.rhs_m)); } struct operator_placeholder_type{} operator_placeholder; template < typename lhs_F > lhs_type<lhs_F> operator || ( const lhs_F & lhs_f, const operator_placeholder_type & rhs_f ) { return(lhs_type<lhs_F>(lhs_f)); } template < typename lhs_F, typename rhs_F > lor_type<lhs_F, rhs_F> operator || ( const lhs_type<lhs_F> & lhs_f, const rhs_F & rhs_f ) { return(lor_type<lhs_F, rhs_F>(lhs_f.value_m, rhs_f)); } #include <iostream> #define my_log_or || operator_placeholder || int main() { if(3 == (lhs(3)||rhs(5))) { std::cout << '(' << 3 << ':' << 3 << ':' << 5 << ')' << '\n'; } if(4 == (lhs(3)||rhs(5))) { std::cout << '(' << 4 << ':' << 3 << ':' << 5 << ')' << '\n'; } if(5 == (lhs(3)||rhs(5))) { std::cout << '(' << 5 << ':' << 3 << ':' << 5 << ')' << '\n'; } if(6 == (6 my_log_or 8)) { std::cout << '(' << 6 << ':' << 6 << ':' << 8 << ')' << '\n'; } if(7 == (6 my_log_or 8)) { std::cout << '(' << 7 << ':' << 6 << ':' << 8 << ')' << '\n'; } if(8 == (6 my_log_or 8)) { std::cout << '(' << 8 << ':' << 6 << ':' << 8 << ')' << '\n'; } return(0); }
Last edited by phantomotap; 02-17-2009 at 05:22 PM. Reason: ^_^
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
^_^
Well, if you had access to 'Boost::Spirit::Phoenix'... you'd mostly be done.
Soma