Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++

What is static assert in C++?

Hammad Nasir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The static_assert macro

static_assert is a macro available in the assert.h standard library from the C++11 standard onward. The structure of the static_assert macro is as follows:

static_assert( bool_constexpr, message );

Note: The message parameter is optional.

Parameters:

  • bool_constexpr must be an expression which consists of constants or macros that eventually get evaluated to either true or false.
  • message must be a string literaltext enclosed in double quotes in the source code.

The static_assert macro throws a compile time error with the given message string literal if bool_constexpr evaluates to false.

The need for the static_assert macro

Before the static_assert macro was made available, the #error directive was used to throw an error before compilation. However, only errors that were caught during the preprocessing phase could be thrown. The figure below depicts the different phases throughout the compilation of a C++ program:

As seen in the image above, the C++ compiler works in different phases. After the preprocessing phase elapses, all preprocessor directives are evaluated and replaced with C++ code which eradicates any possibility of throwing a custom error during the compilation phase. The static_assert macro was introduced to allow programmers to throw an error during the compilation phase of the program.

Example usage of the static_assert macro

The following code snippet demonstrates how to use the static_assert macro:

#include <iostream>
#include <assert.h>
using namespace std;
int main() {
int x = 10;
static_assert(sizeof(x) == 8, "Size of variable x must be equal to 8 bytes");
return 0;
}

The above example evaluates the constant expression sizeof(x) == 8 to be false, since the size of x is 4 bytes, not 8 bytes. Thus, the error Size of variable x must be equal to 8 bytes is printed as a compilation error in the standard output.

If we use the same approach above with the #error directive, it will not work. For example:

#include <iostream>
using namespace std;
int main() {
int x = 10;
#if sizeof(x) != 8
#error "Size of variable x must be equal to 8 bytes"
#endif
return 0;
}

As seen in the error message in the above example, sizeof cannot be evaluated at the preprocessing phase, which makes static_assert necessary.

The following example shows the limitation of the static_assert macro:

#include <iostream>
#include <assert.h>
using namespace std;
int main() {
int x = 10;
static_assert(x < 100, "Value of x is too small");
return 0;
}

Here, the compilation error is not due to the static_assert macro, but rather because x cannot be evaluated at this phase. The reason is that the first argument of the static_assert macro is bool_constexpr, meaning a constant boolean expression is required.

Note: The value of constant expressions are deterministic at the time of compilation.

Since x is not a constant integer variable, if it is used inside an expression, that expression cannot classify as a constant expression.

RELATED TAGS

c++

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring