Thread: assert() in C++

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    assert() in C++

    Hi, all. I'm watching a CPPCon 2016 video by Patrice Roy, in which he uses the code:

    Code:
    assert(denom && "divide by zero");
    1. This is the same assert() function that is provided by the C language in the assert.h header, right?
    2. Would a C++ programmer normally use assert()? I presume that for a compile-time assertion you would use static_assert().
    3. What is the point of adding a string literal there? Does the assert() macro do something special with it? Or is it just a way of marking the relevant line of source code? Isn't it the same as :

    Code:
    assert(denom); /* divide by zero error if this asserts */
    TIA,
    Richard

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    1. I'm not sure, but I'll say yes. It's a macro btw, not a function.
    2. You may want a run-time assertion instead though.
    3. assert() prints its expression if it fails. The programmer may want to have a visual queue on what went wrong. Maybe even explain to the user what went wrong.
    Last edited by GReaper; 03-05-2017 at 09:57 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Richardcavell View Post

    1. This is the same assert() function that is provided by the C language in the assert.h header, right?
    2. Would a C++ programmer normally use assert()?
    1. For C++ you write #include <cassert>
    2. Yes. It is pretty convenient. If you don't want to add string literals to your assert statements another approach is to write descriptive names for the things you're checking and put them in a namespace:

    Code:
    assert(Test::default_Money_is_zero());
    assert(Test::negative_Money_throws());
    assert(Test::fractional_Money_rounds_properly());
    assert(Test::currency_conversion_works());
    assert(Test::Money_plus_Money_works());
    assert(Test::Money_times_Money_throws());
    ...
    Inside those functions you would check stuff using whatever logic you want. Print diagnostics and return false if something goes wrong. In your release code, you will typically compile with NDEBUG defined, which will compile out all of these calls, making the production code faster.

  4. #4
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by Richardcavell View Post
    Hi, all. I'm watching a CPPCon 2016 video by Patrice Roy, in which he uses the code:

    Code:
    assert(denom && "divide by zero");
    3. What is the point of adding a string literal there? Does the assert() macro do something special with it? Or is it just a way of marking the relevant line of source code? Isn't it the same as :

    Code:
    assert(denom); /* divide by zero error if this asserts */
    TIA,
    Richard
    Assert() evaluates the expression within the (), and returns 0 if the argument is false. Such as Assert(1==0) would return a 0, and Assert(1==1) would return a true, so the string literal is only used to test the expression, to determine whether true or false should be returned.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Quote Originally Posted by Terrance View Post
    Assert() evaluates the expression within the (), and returns 0 if the argument is false. Such as Assert(1==0) would return a 0, and Assert(1==1) would return a true, so the string literal is only used to test the expression, to determine whether true or false should be returned.
    A standard-compliant assert() returns nothing. If its expression is true, nothing happens. If false, it prints an implementation-defined message, normally including the line and file where the error happened, and calls abort().

    Unless that capitalization wasn't a typo, and you literally meant "Assert()", in which case you could be right, but I wouldn't know.
    Devoted my life to programming...

  6. #6
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by GReaper View Post

    Unless that capitalization wasn't a typo, and you literally meant "Assert()", in which case you could be right, but I wouldn't know.
    I apologize, I meant assert (). The a automatically goes to uppercase when you type it in at the beginning of a sentence, and I put down Assert in all instances, though I meant assert ().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assert: When to and when not to.
    By Shamino in forum C++ Programming
    Replies: 22
    Last Post: 01-27-2012, 09:55 AM
  2. assert
    By George2 in forum C Programming
    Replies: 1
    Last Post: 10-22-2007, 03:17 AM
  3. Assert
    By Shamino in forum C++ Programming
    Replies: 8
    Last Post: 01-24-2006, 11:02 AM
  4. How to use assert?
    By jjbuchan in forum C Programming
    Replies: 2
    Last Post: 11-11-2005, 01:40 PM
  5. assert
    By ammar in forum C++ Programming
    Replies: 1
    Last Post: 10-19-2002, 08:17 AM

Tags for this Thread