Thread: assert question

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    Question assert question

    I was just reading the definition of assert in assert.h and basically it was this :

    Code:
    #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
    My question is why are there 2 ! operators ? isn't that the same as saying

    Code:
    #define assert(_Expression) (void)( (_Expression) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
    Sorry if this was a stupid question, but it was just confusing me. Thanks!

  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
    !0 is 1
    !anything else is 0

    !!0 is 0
    !!anything else is 1 (by statement 1 and 2)
    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
    Oct 2008
    Posts
    84
    I see. Thanks!

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Salem View Post
    !0 is 1
    !anything else is 0

    !!0 is 0
    !!anything else is 1 (by statement 1 and 2)
    So are you saying the !! is useless here? Any non-zero value is true, so converting them to 1 shouldn't make a difference.
    I saw a !! in code I was modifying a while ago and I just removed it since it was pointless.
    "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

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

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    But it's not entirely useless here...

    If _Expression were of type <someClass> and that class did not provide a cast operator giving an implicit conversion to bool or etc, but it did have an operator !, then the code with !! works and the code without it doesn't. Does that description make sense or should I post a code sample?

    That is precisely why the macro is written like that. It's just one more scenario that it will work for, however obscure that scenario may be.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    If _Expression were of type <someClass> and that class did not provide a cast operator giving an implicit conversion to bool or etc, but it did have an operator !, then the code with !! works and the code without it doesn't. Does that description make sense or should I post a code sample?

    That is precisely why the macro is written like that. It's just one more scenario that it will work for, however obscure that scenario may be.
    The scenario is not that obscure.

    Several coding standards specifically disallow classes supporting any conversion to a basic type (or to raw pointers) because those conversions can happen implicitly (i.e. the compiler does the conversion) in circumstances where such a conversion is unwanted by the programmer.

    However, supporting an operator!() is not forbidden in such cases: an operator!() cannot be accidentally invoked.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    @iMalc
    But why would you want to do that?

    I can see why you would overload ! for say 'class mybool', but that would be overloading the data within the object, not somehow asserting the validity of the class object to begin with.

    Likewise, for say 'class vehicle', what would overloading the ! operator achieve? It doesn't seem to make sense to overload it "just because I can / just in case I use assert".

    vehicle car;
    assert( car ); // huh?

    If you couldn't construct the class instance to begin with, then shouldn't you have thrown an exception? Rather than relying on the caller to use assert (which is a debug-only thing anyway).


    Please post your example.
    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.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Smart pointers. Of course, I expect any good smart pointer to use the safebool idiom to be usable in conditions, but weird company policies may be in place.

    There is another, more likely reason to use the double-not. It may avoid compiler-specific warnings.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Actually I'm still not back on my own computer so I wont post the example I had in mind. I was thinking of a bigint class, but yes a smart pointer class also fits.

    In fact, as CornedBee referred to, I remembered that I use the safe-bool idiom nowdays anyway.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  2. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 05:52 PM
  3. assert
    By George2 in forum C Programming
    Replies: 1
    Last Post: 10-22-2007, 03:17 AM
  4. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM