Thread: Baffled by part of an assert function condition :)

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    110

    Lightbulb Baffled by part of an assert function condition :)

    Hi there!

    Check this out:

    Code:
    m4xMsaaQuality = msQualityLevels.NumQualityLevels;
    assert(m4xMsaaQuality > 0 && "Unexpected MSAA quality level.");
    The variable m4xMsaaQuality is just an unsigned integer that records the available quality levels for a given display adapter and specified format. Just a number.

    And it should be above zero. So I can understand the first part of the assert condition, but just not the second part with the string? It looks to me like the string is just a temporary variable created there and then and isn't even linked to a defined variable so I don't understand what checking it is doing?

    Surely if I just create a temp variable like that in a condition check it will always simply return as true because it exists?

    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,156
    Recall that a string constant has a type of 'const char *'

    And all string constants will compare as not equal to NULL.
    A pointer in a boolean context is implied as comparing it with NULL.
    So in other words
    assert(m4xMsaaQuality > 0 && "Unexpected MSAA quality level." != NULL);

    So you could regard the assert as being logically equivalent to
    assert(m4xMsaaQuality > 0 && true);

    But using a string gives you a super handy way of documenting the reason for the assert.
    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
    Jan 2010
    Posts
    110
    Oh that's snazzy! So if the assert fails you'd get a little error message telling you why too So it's just for convenience of documentation and error hunting and that's it! Thanks very much Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-06-2013, 04:07 PM
  2. Can I call a function inside condition of if statement?
    By Meerul264 in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2012, 11:06 PM
  3. If function - or condition with character input
    By Luponius in forum C Programming
    Replies: 5
    Last Post: 04-17-2011, 04:43 PM
  4. Help with a condition function
    By swanley007 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2006, 02:20 PM
  5. baffled!
    By The Dog in forum C Programming
    Replies: 3
    Last Post: 07-03-2002, 05:58 PM

Tags for this Thread