Statically Checked

Contents[Show]

static_assert is the tool in modern C++ to make you code safe.

static_assert

The usage of static_assert is quite easy. static_assert requires an expression and a string. The expression has to be a predicate that can be evaluated at compile time. Predicate means the expression returns true or false. If the expression evaluates to false you will get at compile time an error message with the string as message. Of course you get no executable.

There are a few points you have to keep in your mind.

  • The static_assert expression will be evaluated at compile time and you have no runtime overhead.
  • Expressions that can be evaluated at compile time are called constant expressions. I have a lot to tell about the new keyword constexpr; but in a later post.
  • You can use static_assert expressions in all parts of your program. Therefore, It's a good idea to put general requirements on your source code in a separate header. The result is, the static_assert expression will be automatically verified at compile time if you include the header. 

At here is the code example to static_assert.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// static_assert.cpp

#include <string>

static_assert(sizeof(void*) == 4 ,"32-bit adressing is required!");
static_assert(sizeof(void*) >= 8 ,"64-bit adressing is required!");

template < typename T, int Line, int Column >
struct Matrix{
   static_assert(Line >= 0, "Line number must be positive.");
   static_assert(Column >= 0, "Column number must be positive.");
   static_assert( Line + Column > 0, "Line and Column must be greater than 0.");
};

int main() {
  
  static_assert(sizeof(int) == sizeof(long int),"int and long int must be of the same length.");
  
  Matrix<int,10,5> intArray;
  Matrix<std::string,3,4> strArray;
  Matrix<double,0,1> doubleArray;
  Matrix<long long,1,0> longArray;
  
  Matrix<char,0,0> charArray;

}

 

The program uses static_assert in the global scope (line 5 and 6); it uses it in the class scope (line 10 - line 12); it uses it in the local scope (line 17). One of the two assertions in line 5 and 6 must inevitably fall. The assertions in the class definition guarantees on one hand that the numbers of columns and lines must be greater than 0 and it guarantees on the other hand that the sum has to be positive.That's the reason why the template instantiation in line 14 is not valid. On my computer int is smaller than long int. The reverse relation is guaranteed by the C++ standard.

Let's have a look at the broken run of my compiler.

static assert

What's next?

The power of static_assert is due to the fact what can be evaluate at compile time. Glad, we have the new type-traits library in C++11. The powerful type-traits library empowers you to check, compare and change types at compile time. But this is the story of the next post.

 

 

 

 

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner,  Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, Sudhakar Balagurusamy, lennonli, and Pramod Tikare Muralidhara.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, and Dendi Suhubdy

 

Seminars

I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable (Online)

Deutsch

English

Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmSmall

 

Comments   

0 #1 visit this hyperlink 2016-11-29 06:11
This article will help the internet visitors for creating new web site
or even a weblog from start to end.
Quote

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 8104

Yesterday 5442

Week 8104

Month 215507

All 5084821

Currently are 148 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments