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.
1 2 3 |
static_assert (boolean_const_expression, error_message); |
Since the release of the C++17 standard, static_assert
syntax has this syntax too:
1 2 3 |
static_assert ( boolean_const_expression ) |
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.
1 2 3 |
static_assert( 5840 == 4220 ); // false |
for example, in bcc32c C++ Builder compiler, at code line 29, this will print out,
[bcc32c Error] test.cpp(29): static_assert failed
1 2 3 |
static_assert( false, "This line has 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”
1 2 3 |
static_assert( sizeof(T) <= 1, "Type is more than 1 byte" ); |
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:
1 2 3 4 5 6 7 8 9 10 |
template <class T> void temp(T x) { static_assert( sizeof(T) <= 1, "Type is more than 1 byte" ); }; int a; temp(a); // int has 4 bytes more than 1 byte |
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++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
template <class T> void temp(T x) { static_assert(sizeof(T) <= 1, "Type is more than 1 byte"); }; int main() { static_assert( 5840 == 4220 ); static_assert( false, "this line is failed" ); int a; temp(a); // int has 4 bytes more than 1 byte } |
these 3 examples will output in compilation to the console as shown below:
1 2 3 4 5 |
[bcc32c Error] test.cpp(18): static_assert failed [bcc32c Error] test.cpp(20): static_assert failed "this line is failed" [bcc32c Error] test.cpp(23): static_assert failed "Type is more than 1 byte" |
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.