Generic algorithms have been available in C++ for decades, but the last two versions of the language have really ramped up the functionality. C++17 added support for parallel execution of generic algorithms to easily take advantage of multi-core CPUs. Then C++20 added support for ranges, a composable version of generic algorithms that’s even closer to LINQ in C#. Today we’ll explore both of these!
Posts Tagged generic
The C++ Standard Library’s algorithms are culmination of a lot of C++ language and library features. They’re like a much more featureful, much faster version of LINQ in C#. This powerful combination makes most “raw” loops unnecessary as they can be replaced by named function calls that are well-tested and often compile to the same machine code as a “raw” loop. Read on to learn about them!
Today we’ll continue to explore the C++ Standard Library by delving into its utility classes and functions. These extremely common tools provide us with basics like std::tuple
whose C# equivalent is so essential it’s built right into the language.
Today we’ll cover a couple of more minor features that don’t have C# equivalents: fold expressions and elaborated type specifiers. Though they are small, they can be quite useful!
C# has support for type aliases in the form of using ScoreMap = System.Collections.Generic.Dictionary<string, int>;
directives. This allows us to use ScoreMap
instead of the verbose System.Collections.Generic.Dictionary<string, int>
or even Dictionary<string, int>
. C++ also has type aliases, but they go way beyond what C# supports. Today we’ll dig into everything C++ offers us to make our code more concise and readable.
C# where
constraints enable our generics to do a lot more. C++ also has constraints and they enable us to write more expressive and efficient code. Today we’ll see how to add some constraints to our templates to achieve these goals.
All of the templates we’ve written so far had a fixed number of parameters, but C++ lets us take a variable number of parameters too. This is like params
in C# functions, but for parameters to C++ templates. Today we’ll dig into this feature, which has no C# equivalent, and learn how to write and use templates with any number of parameters.
Template deduction in C++ is like generic type parameter deduction in C#: it allows us to omit template arguments. Template specialization has no C# equivalent, but enables special-casing of templates based on certain arguments. Today we’ll look at how these features can make our code a lot less noisy and also a lot more efficient.
Last time, we started looking at a core feature of C++: templates. We compared and contrasted them to C# generics and saw how they’re applied to classes, functions, lambdas, and even variables. Today we’ll leverage the power of so-called “non-type template parameters” and “template template parameters” to write some really interesting code.
We’ve seen how to create powerful, Burst-compatible generic algorithms already, but today we’ll take another approach to generic algorithms and implement them in the style of C#’s LINQ. Along the way, we’ll tackle a new challenge by implementing a generic algorithm that allocates a new collection.