C++C++11C++14C++17Introduction to C++Learn C++Syntax

What Is Static Assertion And How To Use static_assert In C++?

What is Static Assertion And How To Use static assert In C++

In C++, as with any programming language, it’s important to ensure that the logic of your program is correct even when the compiler thinks the syntax is valid. This is true no matter how good your C++ dev tool may be. Logical errors – where something in the program code should be true or false, can be very difficult to spot and track down because the code looks correct but the flaw in in how it is being used programmatically. The static_assert method is one way to check that an expression which should evaluate to true or false at a given point is actually doing that. In this article we describe how to use static_assert in C++ and what it does.

What is static assertion and how to use static_assert in C++?

Static Assertion is a method that is used to test that an expression is what we expect it to be (an assertion) at compile time. The static_assert keyword is used to test assertions during the compilation of the code, rather than at preprocessor or run time. This is one of the modern C++ features that comes with the C++11 standard and later.

The static_assert keyword operates differently than the macro assert, which raises assertions at run time. The keyword static_assert also differs from the preprocessor directive #error, which operates during preprocessing and simply emits a message.

Before C++17, the static_assert syntax was as follows.

Since the release of the C++17 standard, static_assert syntax has this syntax too:

if the boolean_const_expression is false i.e. ( 3==2, duck==cat, right==wrong, 9<3) this will raise an assert a message in compilation.

Is there an example of how to use static_assert in C++?

The constant-expression must be one that can be statically evaluated as a boolean. If the constant expression is true, the statement does nothing. If it evaluates to false, the compiler generates an error with either an in-built error-message or a custom text string indicating that.

Here are some examples.

for example, in bcc32c C++ Builder compiler, at code line 29, this will print out,
[bcc32c Error] test.cpp(29): static_assert failed

for example in bcc32c C++ Builder compiler, at code line 30, this will print,
[bcc32c Error] test.cpp(30): static_assert failed “This line has failed”

for example in bcc32c C++ Builder compiler, at code line 30, this will print,
[bcc32c Error] test.cpp(23): static_assert failed “Type is more than 1 byte”

Is there an example of how to use static_assert in C++ templates?

Because the assertion is tested at compile timestatic_assert can do error checking in templates. For instance:

static_assert is useful for static type checking. A certain function might fail if the implementation of an int is too small, so static_assert has utility outside of templates.

Is there a full example of how to use static assertion in C++?

Here is a full example of using static assertion in C++.

these 3 examples will output in compilation to the console as shown below:

What is Static Assertion And How To Use static_assert In C++? The C++ Builder Logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap