Thread: for loop, multiple checks

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    for loop, multiple checks

    Is there a way to shorten this?
    Code:
    for(foo == bar || foo == pub)
    {
          // Incert drunken code here
    }
    More specifically, is there a way to as such:
    Code:
    for(foo == bar||pub){}
    Any help would be appreciated

    -Thank You

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Friends don't let friends code drunk.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Code:
    for(foo == bar)
    {
           for(foo == pub)
           {
            }
    break
    }
    errm oh wait thats not shorter

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You might be able to use bit checks:

    Code:
    enum { bar = 1 << 0, pub = 1 << 1 };
    
    while (foo & (bar | pub))
    ...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    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: ^_^

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by phantomotap View Post
    It can be done, it can be approximated, but how much work are you willing to put into making this expression style work?
    That's reminiscent of Boost::MPL. In fact, if you had access to Boost::MPL, you could slap together something like what the OP is asking in just a few minutes.

    The question is why you'd want to.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    ^_^

    Well, if you had access to 'Boost::Spirit::Phoenix'... you'd mostly be done.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM