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.
Posts Tagged generic
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.
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.
We looked at some generic algorithm examples in the previous article, but they weren’t very generic in one respect: they all required a
NativeArray<T>. What if we wanted to make them more generic so they could work on any type of collection? Today’s article shows two ways to do just that!