C++

We are now using a C++ compiler to compile. This does not mean that we want to use all possible constructs from C++, but we want to be able to profit from a number of very useful improvements of C++ over plain C.

C++ documentation
Good documentation on C++ can be found at http://www.cplusplus.com/ or in the book "The C++ programming language" by Bjarne Stroustrup (the inventor of C++), which is available in the ICP library.

How to improve your C-code by using C++
The following things can be done to improve 's C-code by using C++ constructs. In fact, some of these do already work in C when using the C99 standard.

Define a variable close to its first use
Define a variable close to its first use instead of at the beginning of the function/block. This has the advantages that the variable can be directly initialized, so there is no danger to use an uninitialized variable. Also, the variable is used close to where it is actually used, and not somewhere far away.

Define a loop variable in the for-statement
Define a loop variable like this:

This has the advantage that the variable is visible only within the scope of the loop and it can not be accidentally used somewhere else. And no, it does not cost performance to do so.

Use C++ containers
The main benefit of using C++ is that we can use the C++ containers, which provide different useful data structures that are easy to use and do not have the dangers of dynamic C data structures. They are well documented here: http://www.cplusplus.com/reference/stl/. We strongly suggest that in the future you use these instead of plain C arrays!

Example
If you use containers you can also use ranged-fore loops, which have less boilerplate:

Example
Containers can also be combined with many algorithms from the standard library (cf. eg. ). These are often very optimized and clearly state what you want to do. In addition, they are typically shorter than handwritten code.

What not to do

 * Use of free and malloc, and we try do avoid new and delete.
 * Free and malloc do not run constructors and make it possible to leak memory. When you use RAII solutions like vector you almost never need to manage memory explicitly.


 * Excessive templating
 * We do not use excessive templating in, as this slows down the compilation and makes it significantly more resource hungry. In particular, we will not be using any template metaprogramming.


 * Import a namespace in a header
 * If there is need to use symbols from another namespace (e.g. ) in a header file, please use the fully-qualified name (e.g.  ) and do not use   as this would import the namespace into all files that include this header.


 * Use exceptions in MPI code
 * At the moment exceptions in code that is called via an MPI callback can not throw, because this leads to a deadlock. This is because the other nodes no nothing about a local exception.