Generic Programming Without (Writing Your Own) Templates

By Tina Ulbrich

Generic programming is a technique where functions and data structures are defined as general as possible. The goal is that they work with different data types and therefore are reusable.

In C++ generic functions and data structures are typically realized by using templates.

Templates are a great tool for generic programming but they come with their own set of challenges. They can be hard to read/write and influence compile times negatively. And sometimes they are even too generic. C++20’s concepts can help with that but I want to show how you can be generic in your code without writing your own templates.

To do that, we will explore some C++17 and C++20 features from the standard library, like std::span, std::variant and std::any. I will explain what they are, where they are useful and how to use them.

I will also show some features you can find in other libraries, e.g. GSL.