Assert and static_assert in C++

In this tutorial, you will study about two most useful assertion statements provided in C++. These are:

  • assert
  • static_assert

Functionalities of these statements are present in C++ header <cassert>.

assert

An assert is a statement in C++ which tests for a condition like constant expression. If the condition is examined as true, the program continues without any problem but if it is false, the program is stopped and an error message is shown on the screen. For example, we can check whether an integer value given by the user exceeds a certain parameter or not. This statement is evaluated at run time of the program.

Now if the value turned out to be false(get evaluated to 0), then the source code filename, line number, and the expression are sent to the standard error and abort() function is called. It will display the error message on the screen. To use assert statement we must include C++ header such as <cassert> or <assert.h>.

Syntax:

void assert( constant expression );

Parameters passed:

An integral constant expression to be evaluated. If it evaluates to 0(false) 
then it cause assertion failure and will ultimately result in termination of 
the program.

Return type:

None

We can understand it’s working with the help of the following example:

#include<iostream>//std::cout
#include<assert.h>//std::assert
using namespace std;
void display(int x)

 { 
    assert(x>5);//assert statement evaluates the expression
    cout<<"The number is greater than 5"<<endl;
  }

int main()

{
   display(7);//passing value 7 to the function display
   display(4);// passing value 4 to the function display
    return 0;
 }

The output of the following program is:

The number is greater than 5 
a.out: main.cpp:10: void display(int): Assertion `x>5' failed. 
Aborted (core dumped)

Explanation: We are checking in the assert expression whether x is greater than 5 or not. So when we pass 7 in display function it evaluates to true and prints the statement “The number is greater than 5”. But when we pass 4 in the display function, the expression evaluates to false and thus causes assertion failure and results in termination of the program by calling abort() function.

static_assert

A static_assert is a statement in C++ which tests for a condition like constant expression at the compile time of the program. If the condition results as 1 or true, the static_assert declaration has no effect. And as soon as the condition is examined to be false or 0, then the compiler shows the message which is passed as a parameter which leads to the compilation failure with an error. It is, however, important to note that the message passed as a parameter to display is not compulsory.

Syntax:

static_assert( statement, message );

Parameters passed:

statement: An integral constant expression that result in true or false i.e. of Boolean nature. 
message: The error message(string) that is displayed when the constant_expression parameter is evaluated to false. 

Return type:

None

We can understand it’s working using the following example:

#include<iostream>//std::cout
using namespace std;
int main()

  {
       static_assert(7>5,"Number is less than 5");//static_assert evaluates the expression
       cout<<"Number is greater than 5"<<endl;
        static_assert(4>5,"Number is less than 5");//static_assert evaluates the expression
        cout<<"Number is greater than 5"<<endl;
    return 0;
  }
The output of the following program is: 
main.cpp: In function ‘int main()’:
main.cpp:8:9: error: static assertion failed: Number is less than 5
static_assert(4>5,"Number is less than 5");
^~~~~~~~~~~~~

Explanation: Since static_assert statement gets evaluated at the compile-time therefore, the above code program results in termination of the program as soon as compiler evaluates the expression of second static_assert statement of the program as false or 0 at compile time itself and display the message “Number is less than 5” passed as parameter. However, the first static_assert statement expression is true but we can see “Number is greater than 5” do not get printed because the error is found at compile time rather than run time.

Comparison of working of assert and static_assert statement can be seen through the following example:

#include <iostream>//std::cout
#include <cassert>//std::assert
using namespace std;
int main()
{
    assert(7<8);
    static_assert(7<8, "It is a false statement.");
    cout << "It is a true statement.\n";
    assert(5>8);
    static_assert(5>8, "It is a false statement.");
    cout << "It is a true statement.\n";
}
The output of the following program is: 
main.cpp: In function ‘int main()’:
main.cpp:10:5: error: static assertion failed: It is a false statement.
static_assert(5>8, "It is a false statement.");
^~~~~~~~~~~~~

Explanation: In the above program the assertion failure is caused as soon as the compiler reaches the second static_assert statement (as the expression passed there is evaluated to be false). Here we can see the difference that termination of the program is caused by the second static_assert statement instead of the second assert statement. It is because static_assert statement expression is examined at compile-time while the assert statement expression, at the run time.

Also read: std::allocator() function in C++

Leave a Reply

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