C# generics compared to C++ templates

12,254

Solution 1

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.

Solution 2

http://blogs.msdn.com/csharpfaq/archive/2004/03/12/88913.aspx

Roughly, much of the difference has to do with the fact that templates are resolved at compile-time, and generics are resolved at runtime.

Solution 3

Extensive answer on Stack Overflow: What are the differences between Generics in C# and Java... and Templates in C++?

Solution 4

This blog entry from Eric Gunnerson covers this topic quite well.

The biggest immediate difference is that templates are a compile time feature whereas generics are a runtime feature.

Share:
12,254
Bjarke Freund-Hansen
Author by

Bjarke Freund-Hansen

Updated on June 18, 2022

Comments

  • Bjarke Freund-Hansen
    Bjarke Freund-Hansen almost 2 years

    Possible Duplicate:
    What are the differences between Generics in C# and Java… and Templates in C++?

    What are the differences between C# generics compared to C++ templates? I understand that they do not solve exactly the same problem, so what are the pros and cons of both?

  • Bjarke Freund-Hansen
    Bjarke Freund-Hansen almost 15 years
    Looks like this question is a duplicate. Thanks for the pointer.
  • Daniel Earwicker
    Daniel Earwicker almost 15 years
    Actually although generics can be instantiated at runtime (through reflection, just like anything else), a lot is figured out at compile time. For example, the JIT produces a specific implementation of List<int> rather than using a common type-erased version equivalent to List<object> (this is one way it differs from Java).
  • plankalkul
    plankalkul almost 15 years
    @Earwicker: tell me about it. That's why you can't do generic promotion (i.e. concatenating a List<int> to a List<object>). The functionality exists in the CLI, but they chose not to implement it that way in C# (a mistake, IMO).
  • dotNET
    dotNET over 7 years
    Another interesting attribute to C++'s templates is they can by applied to things other than classes - is this the same thing as C# allows us to define generics in functions (e.g. T SomeFunc<T>(T input){...}) ?
  • John Foll
    John Foll over 4 years
    C++ Templates will execute faster because they are compiled in C++ to machine code (and maybe because they are compiler macros, instead of being overly generic). But C# objects don't have to be re-compiled can be placed into a dll. If you are developing a program that allows for dynamic code execution, it is possible to use a C# Generics object or function that is plugged in at runtime, with no recompile needed. I believe that this trick would be harder to pull off in C++, since its templated functions or things have to be compiled to get different instances of them.