C# generics compared to C++ templates [duplicate]

You can consider C++ templates to be an interpreted, functional programming language disguised as a generics system. If this doesn’t scare you, it should 🙂

C# generics are very restricted; you can parameterize a class on a type or types, and use those types in methods. So, to take an example from MSDN, you could do:

public class Stack<T>
{
   T[] m_Items; 
   public void Push(T item)
   {...}
   public T Pop()
   {...}
}

And now you can declare a Stack<int> or Stack<SomeObject> and it’ll store objects of that type, safely (ie, no worried about putting SomeOtherObject in by mistake).

Internally, the .NET runtime will specialize it into variants for fundamental types like int, and a variant for object types. This allows the representation for Stack<byte> to be much smaller than that of Stack<SomeObject>, for example.

C++ templates allow a similar use:

template<typename T>
class Stack
{
    T *m_Items;
    public void Push(const T &item)
    {...}
    public T Pop()
    {...}
};

This looks similar at first glance, but there are a few important differences. First, instead of one variant for each fundamental type and one for all object types, there is one variant for each type it’s instantiated against. That can be a lot of types!

The next major difference is (on most C++ compilers) it will be compiled in each translation unit it’s used in. That can slow down compiles a lot.

Another interesting attribute to C++’s templates is they can by applied to things other than classes – and when they are, their arguments can be automatically detected. For example:

template<typename T>
T min(const T &a, const T &b) {
  return a > b ? b : a;
}

The type T will be automatically determined by the context the function is used in.

These attributes can be used to good ends, at the expense of your sanity. Because a C++ template is recompiled for each type it’s used against, and the implementation of a template is always available to the compiler, C++ can do very aggressive inlining on templates. Add to that the automatic detection of template values in functions, and you can make anonymous pseudo-functions in C++, using boost::lambda. Thus, an expression like:

_1 + _2 + _3

Produces an object with a seriously scary type, which has an operator() which adds up its arguments.

There are plenty of other dark corners of the C++ template system – it’s an extremely powerful tool, but can be painful to think about, and sometimes hard to use – particularly when it gives you a twenty-page long error message. The C# system is much simpler – less powerful, but easier to understand and harder to abuse.

Leave a Comment