Posts Tagged type

C++ For C# Developers: Part 34 – Fold Expressions and Elaborated Type Specifiers

Tags: , , ,

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!

Read the rest of this article »

No Comments

C++ For C# Developers: Part 30 – Type Aliases

Tags: , ,

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.

Read the rest of this article »

No Comments

C++ For C# Developers: Part 28 – Variadic Templates

Tags: , ,

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.

Read the rest of this article »

2 Comments

C++ For C# Developers: Part 26 – Template Parameters

Tags: , , ,

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.

Read the rest of this article »

2 Comments

C++ For C# Developers: Part 20 – Implicit Type Conversion

Tags: , , , ,

We’ve actually seen quite a bit of implicit type conversion so far in the series. We’ve converted integers to floats (float f = 123), arrays to pointers (int* p = a), base type pointers to derived type pointers (D* p = &b), and many more. Today we’ll gather all those casual conversions up into one article that goes over all the rules, including user-defined type conversions.

Read the rest of this article »

2 Comments

Type-Agnostic Generic Algorithms

Tags: , , ,

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!

Read the rest of this article »

2 Comments

Strongly-Typed Integers

Tags: , ,

An int can be anything: points, health, currency, time, etc. We often make mistakes using one int where another int was supposed to go. Imagine a function DoDamage(int, int). It’s not obvious what the parameters mean. Today we’ll use the C# type system to make the code much more readable and less error-prone!

Read the rest of this article »

6 Comments

C++ Scripting: Part 13 – Operator Overloading, Indexers, and Type Conversion

Tags: , , ,

Today’s article continues the series by adding support for C++ to call the various overloaded operators and indexers that are written in C#. This includes support for all 24 overloadable operators in C# plus the explicit and implicit type conversion operators. Indexers aren’t quite overloaded operators, but they allow for array-like indexing into C# types so they’re included today. Read on to learn how all this support was implemented in the GitHub project!

Read the rest of this article »

No Comments

C++ Scripting: Part 10 – Full Generics Support

Tags: , , , , , , ,

C# APIs are chock-full of generics. Generic types, generic method parameters, generic return types, generic fields, generic properties, deriving from generic types, and generic constructors. We can find all of these in the Unity and .NET APIs. Some are more frequent than others, but we’re going to need support for all of them to make C++ scripting a viable alternative to C#. Today’s article continues the series by adding just that: support for all of these kinds of generics. Let’s dive into how to use them as well as some bonus items added to the project this week.

Read the rest of this article »

No Comments

How to Recover Anonymous Types

Tags: , , , ,

When we just need a quick and dirty type to hold some values, C#’s anonymous types fit the bill: var person = { First="John", Last="Doe", Age=42 }. On the down side, since these types are anonymous they have no explicit type. The var variable is strongly typed, but you have to use the object type when passing them to other functions. But then how do you get the fields back out? Today’s article shows you how so that anonymous types will be more useful to you. Read on to find out how to recover anonymous types!

Read the rest of this article »

No Comments