# Modern C++ in scientific computing?

I am looking for books or articles, or blog-posts, or any published material in general, that address specifically the uses of C++ modern features (move semantics, the STL, iterators, lazy evaluation, etc.) in scientific computing. Can you suggest any?

I think that these new features will make it easier to write efficient code, but I haven't found real examples. Most references I've read are about generic uses of C++, and do not contain examples of scientific computing. So I am looking for examples (do not have to be production code examples, just pedagogical examples, at the level of, say, Numerical Recipes) of scientific computing code using C++ modern features.

Note that I am not asking about libraries that use these features. I am asking about articles/books/etc explaining how I can exploit these features in scientific computing.

• Are you asking about "modern" in the sense of what is understood to be the best practice nowadays (vs 20 years ago), or "modern" in the sense of specifically C++11/14? – Kirill Jul 24 '15 at 18:52
• @Kirill I guess it's both. Mostly something that uses C++11/14, but following best practices. – becko Jul 24 '15 at 19:12

Two examples of libraries that use modern C++ constructs:

• Both the eigen and armadillo libraries (linear algebra) use several modern C++ constructs. For instance, they use both expression templates to simplify arithmetic expressions and can sometimes eliminate some temporaries:

http://arma.sourceforge.net/

• The CGAL library (computational geometry) uses many modern C++ features (it heavily uses templates and specializations):

http://www.cgal.org

Note:

modern C++ constructs are very elegant and can be very fun to use. It is both a strong point and a weakness: when using them, it is so tempting to add several layers of templates / specializations / lambdas that in the end you sometimes get more "administration" than effective code in the program (in other words, your program "talks" more about the problem than describing the solution). Finding the right balance is very subtle. Conclusion: one needs to track the evolution of the "signal/noise" ratio in the code by measuring:

• how many lines of code in the program ?
• how many classes/templates ?
• running time ?
• memory consumption ?

Everything that increases the first two ones may be considered as a cost (because it may make the program harder to understand and to maintain), everything that decreases the last two ones is a gain.

For instance, introducing an abstraction (a virtual class or a template) can factor code and make the program simpler (gain), but if it is never derivated / instanced once only, then it introduces a cost for no associated gain (again it is subtle because the gain may come later in the future evolution of the program, therefore there is no "golden rule").

Programmer's comfort is also an important factor to be taken into account in the cost/gain balance: with too many templates, compilation time may increase significantly, and error messages become difficult to parse.

To what extent is generic and meta-programming using C++ templates useful in computational science?

• Same goes for Armadillo as well as Eigen, no? – dr.blochwave Jul 27 '15 at 13:58
• Yes, you are right (I just checked in Armadillo's documentation, I'm less familiar with it). I'm editing the post. – BrunoLevy Jul 27 '15 at 14:03
• Longer compilation times are another cost worth mentioning. – Kirill Jul 28 '15 at 8:46
• @Kirill, yes good point (edited the post to mention it), thanks. – BrunoLevy Aug 31 '15 at 7:14

I would suggest taking a look at Deal.II. It uses the STL, it's own iterators, shared pointers, etc.

The various linear solvers can use the various matrices because of how it was designed. I haven't come across any use of move semantics, but that doesn't mean they aren't there. Here is a link.

• Also OpenFOAM -- pretty extensive use of templates. – tpg2114 Jul 24 '15 at 17:34
• deal.II doesn't use move semantics (because it refrains from using C++11 language features -- but it uses C++11 library features for which there are replacements in BOOST for older compilers). It uses pretty much every other C++ language feature, though, including all of the ones asked in the original question. – Wolfgang Bangerth Jul 25 '15 at 3:16
• It seems not easy to look at a large library in details. – Michael Feb 5 '19 at 14:55

The HPX library makes heavy use of a range of C++11 features such as move constructors and is also aiming to be a complete implementation of N4409 (Working Draft, Technical Specification for C++ Extensions for Parallelism).

They have a list of publications on their site which includes a number of example of using the library to speed up scientific computation. There is also so interesting discussion of the library and it's use of modern C++ in this CppCast episode.

• Welcome to scicomp.se! If you add links or cite some articles/books/blog-posts that discuss this library for scientific computation, I'll happily upvote your Answer! – hardmath Jul 25 '15 at 3:00

I suggest taking a look at Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples by Barton and Nackmann.

The fact that this book was published in 1994 makes it appear to violate your criterion of "modern techniques." However, Barton and Nackmann were on the cutting edge of what was possible with C++ templates at the time and the innovative techniques they devised for achieving good performance are still used in the latest C++ class libraries.

Besides deal.ii (which was already suggested here) you can also have a look at the Dune library which makes extensive use of some advanced C++ features like template metaprogramming, iterator ranges, smart pointers, and so on. There is also a recent preprint by Joachim Schöberl, which comments on the use of C++ 11 features, like for instance lambda functions, for simplifying the implementation of the finite element methods in NGSolve. Boost also has some libraries related to scientific programming like uBLAS, Graph, etc. I guess in most of these libraries you will find good examples of modern C++ usage. However, be aware that you may encounter also bad examples of using advanced/modern C++. In some cases, while reading the code/documentation, I got the feeling that occasionally things are vastly over-generalized for the sake of showing off advanced skills like TMP, where for 99% of all potential applications a more straightforward implementation would also do the job.

The book "Guide to Scientific Computing in C++" by Pitt-Francis & Whiteley was written to answer exactly this sort of thing (use of STL, iterators etc.) it is available via Amazon, or as an e-Book from publisher.

Disclosure - I work in the same research group as the authors, but still think it's a very good resource for this!

I think that this book is perfect for you, as it did for me: Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers (C++ In-Depth) by Peter Gottschling especially if used in conjunction with Programming Principles and Practice Using C++ 2nd Edition Bjarne Stroustrup. The inventor of C++ himself. Both should provide a solid ground to stand on.

The Blaze library for linear algebra makes heavy use of C++14 in the form of deduced and trailing return types. Other modern C++ features in use are constexpr, alias templates, and a whole lot of template metaprogramming with expression SFINAE.

You can also use initializer lists for your vectors and matrices, e.g.

blaze::DynamicVector<int> x{ 4, -1, 3 };


For more details see their getting started page.