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_asser
t 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.
Table of Contents
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 time, static_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:
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.