static_assert in C++ 11 and how to use

C-Plus-Plus

In this post, we are going to understand the static_assert in C++ 11 and how to use code examples, and what all scope of static_asserst.

1.What are static assertion


The Static assert is a constant expression that is evaluated at compile-time and compared to zero. If the expression equates to zero, compile generates an error and displays a message (if provided) as part of the error message. Otherwise, if the expression does not equate to zero, then nothing happens.

Static assertions are used to check a condition at compile time. This acts as a static code analysis tool in C++11.

Before the introduction of static asserts this was done using #error & #if directive combinations. But with C++ 11 standard this is done using the below syntax:

Syntax:

static_assert( constant_expression, stringerror_literal ); 

Parameters:

  • constant_expression: An integral constant expression that can be evaluated to true or false.
    • If the ‘constant_expression’ condition is true at compile-time, the static_assert declaration has no effect. If the condition is false, the assertion fails, the compiler displays the message provided in stringerror_literal and the compilation will fail.
  • stringerror _literal: The error message that is displayed when the constant_expression result is false.
    • stringerror _ literal parameter is optional to pass. This needs to be passed only if you want an error message to be displayed.

C++ Program static_assert in C++ 11 and how to use

The example which we saw above will not compile due to static assert failure. As you can see the static assert is checking the length of the packet and while creating an FTP object we are passing length as 2 which is less than 128. This will result in a compilation failure.

Let us understand this with an example:

#include <iostream> 
using namespace std; 
template <class T, int length> 
class Packet
{         
  static_assert (length > 128, "packet size is shorter!");  //  static assert on length      
 T m_datapacket [length]; 
};    
int main() 
{     
   Packet <int, 132> TCPip; // This is OK as static check will    pass.     
   Packet <int, 2> FTP; // This is not OK as static check will not pass.       
 return 0; 
}

Static assertion scope usage


Static asserts can be declared and used in three scopes:

  • Class scope – Our example above is for declaring a static assert in the class scope.
  • Namespace scope – we can declare the static asserts in namespace scope also like the below example.
  • Block scope – we can declare the static asserts in function or block scope also like the below example:

C++ Program Example

#include <iostream> 
static_assert(sizeof(int*) == 8,  
"Expected size of Integer for this module is 4"); // at namespace scope
int main()
{
return 0;
}

Static assertion block scope

In this example, we are using static_assert() of c++ program with exaple

void foo(int num)
{
  static_assert(num <= 0, "num is already positive.");
  num++;
}
int main()
{
   foo(12);
   return 0;
}