Javatpoint Logo
Javatpoint Logo

static_assert in C++

In this article, we will discuss the static_assert in C++ with its syntax, parameters, and examples.

What is the static_assert?

The static_assert is a built-in feature in C++. It allows us to assert the statements during compile time. It is introduced in the C++11 version. This feature contains two main fields, conditions and messages. The condition should be a constant expression, and the message should be literal. This feature is mainly used to prevent potential errors from reaching runtime and make code more robust and clearer.

Syntax:

It has the following syntax:

Parameters:

  • The constant expression will give the constant Boolean value at compile time.
  • String_literal is an error message that must be displayed when the constant expression is evaluated as false. Sometimes, the programmer does not give the message or the string literal. In that case, a default message is displayed.
  • Static_assert will ensure that the condition is met only at compile time, not during the program's execution. However, the runtime assertions are used for debugging to catch unexpected conditions during execution.

Example:

Let us take a program to illustrate the static_assert method in C++.

Output:

static_assert in C++

Explanation:

  • In this example, the program has a function template named printSize(), which is used to print the size of a given type. So here, we used the assert feature so that if the size of the type is less than 4 bytes, the error message is displayed in the console.
  • There are many situations where we use the static_assert Some of them are checking constant expressions, checking the type sizes, validating configuration parameters, checking Enum values, validating template parameters, and validating compile-time computations, which are also used to ensure API compatibility.
  • Before introducing this feature, other techniques are used to achieve a similar functionality for compile-time assertions. Some of them:
  • The #error directive will stop the program when a particular condition is met. Using Enumerations to trigger a compilation error when the condition wasn't met.
  • Situations where the static_assert feature is commonly used in programming.

Checking constant expressions:

Let us take a program to demonstrate the usage of the statc_assert function for checking constant expressions in C++.

Output:

static_assert in C++

Explanation:

In this program, a constexpr variable is defined and given the value of 100. After that, the assert will check whether the MAX_SIZE is greater than 0. If the condition fails, errors are displayed, saying the MAX_SIZe must be greater than zero.

Validation the configuration parameters:

Let us take a sample program to illustrate the usage of the feature in validating configuration parameters.

Output:

static_assert in C++

Explanation:

In the program, a variable BUFFER_SIZE is initialized to 3000. After that, the assert will check whether the BUFFER_SIZE is within the range of 512 and 1048. If the BUFFER_SIZE is not in that range, it will give the error message.

Validating the compile time computations:

Let us take an example to demonstrate the usage of the static_assert in validating the compile time computations:

Output:

static_assert in C++

Explanation:

This program illustrates the compile-time computations. It is used for compile-time verification of compute's correctness with its usage.

To verify size compatibility

Let us take an example to verify size compatibility using the static_assert method in C++.

Output:

static_assert in C++

Explanation:

Here, the static_assert feature is used to check whether the size of the given struct is 8 bytes or not. If it is 8 bytes, it compiles without any errors, and the size of the point struct is displayed; otherwise, it prints the message that the point struct does not have the expected size.

Disabling the code for unsupported feature:

Let us take a sample program to disable the code for unsupported feature in C++.

Output:

static_assert in C++

Using feature without the string_literal

Let us take an example to demonstrate the static_assert function without using the message in C++.

Output:

static_assert in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA