constant literal on LHS of conditional operator

This is a discussion on constant literal on LHS of conditional operator within the C Programming forums, part of the General Programming Boards category; I was just wondering - I see things like this a lot in other people's code: Code: assert(0 <= index); ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    constant literal on LHS of conditional operator

    I was just wondering - I see things like this a lot in other people's code:

    Code:
    assert(0 <= index);
    And I was just wondering, why write it like this, when the meaning of:

    Code:
    assert(index >= 0);
    ...is so much more immediately apparent? Is there some reasoning behind this style, or are they just trying to be clever?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,118
    The idea is that if you make a mistake by performing an assignment instead:
    Code:
    assert(0 = index);
    you would get a compile error.

    The weakness is that since this only applies when an rvalue or const lvalue is involved, it is no guarantee of protection.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    I see! Thanks.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I let the compiler find those errors for me.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21