How to Use Assert() With a Message in C++?

Assert with Message in CPP

Do you want to learn more about assert() with a message in CPP (C++) and how to use static and dynamic assert in CPP? 🤔

In C++, an assert is a predefined construct that allows us to test certain program assumptions. The program executes normally when the conditional phrase in an assert statement is set to true. However, if the expression is incorrect, a notification is sent and the application is terminated.

In this article, we’ll discuss what assertions are in the CPP (C++) and how to use static and dynamic assertions, assert(), with a message. So without further ado, let’s dive deep into the topic and see some real examples. 

 

 

What is Assert() in CPP?

As we’ve discussed in Cpp, the basic purpose of assertions is to test for logical impossibilities. They can be used, for instance, to verify a code’s intended state prior to execution or its actual state during execution. 

An assertion has the following syntax:

void assert( int expression );

 

Code

#include <iostream>

#include <assert.h>


int main()

{

   int a = 5;

   assert (a==3);

   std::cout << a;

   return 0;

}

 

Output

 

Assertions, in contrast to standard error handling, are often deactivated at run-time. As a result, it is not a good idea to use assert() to create assertions that can have unintended consequences. As an illustration, it is not a good idea to write anything like assert(a = 3) since x is altered and this change won’t occur if assertions are disabled.

 

How to Handle Assertion With NDEBUG in CPP?

Using the C/C++ preprocessor NDEBUG, assertions can be entirely removed at compile time.  Assertions are tested in real time when used. Although assertions speed up debugging, care should be taken to avoid including them in the application’s release build. This is because we are aware that we only release an application after being certain that it has undergone extensive testing.

Therefore, when we deploy the software, we must turn off all assertions. Using the NDEBUG macro, we may stop a program from making assertions. When the NDEBUG macro is used in a program, all assert calls are blocked.

To remove all assert statements from the program, we may add the line shown below.

 

Code

#define NDEBUG

#include <iostream>

#include <assert.h>



int main()

{


    int a = 5;

    assert (a==3);

   std::cout <<"  The value of a is  ";

   std::cout << a;

   return 0;

}

 

Output

The value of a is 5

The above example successfully builds and runs.

Assertions are disabled by default in Java; to activate them, we must supply an option to the run-time engine.

 

 

How to Use Assert() With a Message in C++?

Following are the two methods through which a message can be asserted:

  1. Use Static Assertion
  2. Use Dynamic Assertion

 

Method 1: Use Static Assertion

Unlike the assert() statement, which is evaluated at runtime, static assert is evaluated at the time of compilation.  Static assert has been a part of C++. It accepts as parameters a conditional expression and a message to be shown. When the condition is evaluated as false, the message is shown and a compiler fault is raised. Then the program is executed.

 

Code

#include <cassert>


int main(){


 int D = 3;

 int d = 0;

 assert(( "Message: d is divisor and divisor can't be zero", divisor != 0));

int Divison = D/d;

 return 0;

}

 

Output

 

As we know, we cannot divide any integer by 0, therefore this program’s attempt to divide 3 by 0 (3/0) will result in an error as predicted. We have included a customized message to make the mistake easier to read.

 

 

Method 2: Use Dynamic Assertion

To further illustrate possible solutions, we first construct the map structure map and populate it with random key/value pairs, which we will emit to stdout.

 

Code

#include <iostream>

#include <cassert>


using namespace std;

bool print_if_false(const bool Assert, const char* Message) {

    if(!Assert) {

       cout << Message << endl;

   }

   return Assert;
}


int main()

{

 int D = 8;

 int d = 0;

 assert(print_if_false(d != 0,"Message: d is divisor and divisor can't be zero" ));

 return 0;

}

 

Output

 

The example above is similar to the one before, except it has been altered. Overall, the features are the same. As we cannot divide an integer by zero, the assertion fails and a message is displayed.

To show the message on a distinct line, we have developed our own custom function.

Enhancing readability and learning how to build assert macros to display personalized messages are the goals.

 

 

Conclusion

To summarize the article, on how to iterate through a map in c++ we’ve discussed what is an iterator in CPP and how you iterate through a map in C++. In addition to that, we’ve discussed iterators and how to use them in CPP. There are some methods discussed above to iterate through maps in C++.

As discussed in this article, we have discussed the assert() statement in C++, its syntax, and how it works. We have discussed static and dynamic methods to assert c++ messages and also to handle assertions with NDEBUG.   We can use the static assert function for assertion with messages and also the dynamic assert function for assertion with messages and also we can use NDEBUG as discussed in the above article to avoid error

Let’s have a quick recap of the topics discussed in this article.

  1. What is an assert in CPP?
  2. Handling Assertion With NDEBUG.
  3. Use static Assertion
  4. Use Dynamic Assertion

If you’ve found this article helpful, don’t forget to share and comment below. 👇: Which solutions have helped you solve the problem?

 

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share