CSC/ECE 517 Fall 2012/ch1b 1w47 sk: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 290: Line 290:
=== Generics in C++===
=== Generics in C++===


In C++ generics are called templates. Function templates are special functions that can operate with generic types. This allows one to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.
In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type.
The format for declaring function templates with type parameters is:
<pre>
template <class identifier> function_declaration;
template <typename identifier> function_declaration;
</pre>
The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.
For example, to create a template function that returns the greater one of two objects the following code could be used:
<pre>
template <class myType>
myType GetMax (myType a, myType b) {
return (a>b?a:b);
}
</pre>
The above code creates a template function with myType as its template parameter. This template parameter represents a type that has not yet been specified, but that can be used in the template function as if it were a regular type. The function template GetMax returns the greater of two parameters of this still-undefined type.
To use this function template one can use the following format for the function call:
<pre>
function_name <type> (parameters);
</pre>
For example, to call GetMax to compare two integer values of type int one can use:
<pre>
int x,y;
GetMax <int> (x,y);
</pre>
When the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appearance of myType by the type passed as the actual template parameter (int in this case) and then calls it. This process is automatically performed by the compiler and is invisible to the programmer.
Consider the code below.
<pre>
// function template
#include <iostream>
using namespace std;
template <class T>
T GetMax (T a, T b) {
  T result;
  result = (a>b)? a : b;
  return (result);
}
int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax<int>(i,j);
  n=GetMax<long>(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}
</pre>
In this case, we have used T as the template parameter name instead of myType because it is shorter and in fact is a very common template parameter name. But any identifier can be used. In the example above the function template GetMax() is used twice. The first time with arguments of type int and the second one with arguments of type long. The compiler has instantiated and then called each time the appropriate version of the function.
The type T is used within the GetMax() template function to declare new objects of that type:
<pre>
T result;
</pre>
Therefore, result will be an object of the same type as the parameters a and b when the function template is instantiated with a specific type.
In this specific case where the generic type T is used as a parameter for GetMax the compiler can find out automatically which data type has to instantiate without having to explicitly specify it within angle brackets (like it was done before specifying <int> and <long>). So it could be written as below instead:
<pre>
int i,j;
GetMax (i,j);
</pre>
Since both i and j are of type int, and the compiler can automatically find out that the template parameter can only be int. This implicit method produces exactly the same result:
<pre>
// function template II
#include <iostream>
using namespace std;
template <class T>
T GetMax (T a, T b) {
  return (a>b?a:b);
}
int main () {
  int i=5, j=6, k;
  long l=10, m=5, n;
  k=GetMax(i,j);
  n=GetMax(l,m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}
</pre>
Notice how in this case, we called our function template GetMax() without explicitly specifying the type between angle-brackets <>. The compiler automatically determines what type is needed on each call.
Because the template function includes only one template parameter (class T) and the function template itself accepts two parameters, both of this T type, we cannot call our function template with two objects of different types as arguments:
<pre>
int i;
long l;
k = GetMax (i,l);
</pre>
This would not be correct, since our GetMax function template expects two arguments of the same type, and in this call to it we use objects of two different types.
We can also define function templates that accept more than one type parameter, simply by specifying more template parameters between the angle brackets. For example:
<pre>
template <class T, class U>
T GetMin (T a, U b) {
  return (a<b?a:b);
}
</pre>
In this case, our function template GetMin() accepts two parameters of different types and returns an object of the same type as the first parameter (T) that is passed. For example, after that declaration we could call GetMin() with:
<pre>
int i,j;
long l;
i = GetMin<int,long> (j,l);
</pre>
or simply:
<pre>
i = GetMin (j,l);
</pre>
even though j and l have different types, since the compiler can determine the appropriate instantiation anyway.
Class templates
We also have the possibility to write class templates, so that a class can have members that use template parameters as types. For example:
<pre>
template <class T>
class mypair {
    T values [2];
  public:
    mypair (T first, T second)
    {
      values[0]=first; values[1]=second;
    }
};
</pre>
The class that we have just defined serves to store two elements of any valid type. For example, if we wanted to declare an object of this class to store two integer values of type int with the values 115 and 36 we would write:
<pre>
mypair<int> myobject (115, 36);
</pre>
this same class would also be used to create an object to store any other type:
<pre>
mypair<double> myfloats (3.0, 2.18);
</pre>
The only member function in the previous class template has been defined inline within the class declaration itself. In case that we define a function member outside the declaration of the class template, we must always precede that definition with the template <...> prefix:
<pre>
// class templates
#include <iostream>
using namespace std;
template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();
};
template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}
int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}
</pre>
Notice the syntax of the definition of member function getmax:
<pre>
template <class T>
T mypair<T>::getmax ()
</pre>
Confused by so many T's? There are three T's in this declaration: The first one is the template parameter. The second T refers to the type returned by the function. And the third T (the one between angle brackets) is also a requirement: It specifies that this function's template parameter is also the class template parameter.
Template specialization
If we want to define a different implementation for a template when a specific type is passed as template parameter, we can declare a specialization of that template.
For example, let's suppose that we have a very simple class called mycontainer that can store one element of any type and that it has just one member function called increase, which increases its value. But we find that when it stores an element of type char it would be more convenient to have a completely different implementation with a function member uppercase, so we decide to declare a class template specialization for that type:
<pre>
// template specialization
#include <iostream>
using namespace std;
// class template:
template <class T>
class mycontainer {
    T element;
  public:
    mycontainer (T arg) {element=arg;}
    T increase () {return ++element;}
};
// class template specialization:
template <>
class mycontainer <char> {
    char element;
  public:
    mycontainer (char arg) {element=arg;}
    char uppercase ()
    {
      if ((element>='a')&&(element<='z'))
      element+='A'-'a';
      return element;
    }
};
int main () {
  mycontainer<int> myint (7);
  mycontainer<char> mychar ('j');
  cout << myint.increase() << endl;
  cout << mychar.uppercase() << endl;
  return 0;
}
</pre>
This is the syntax used in the class template specialization:
<pre>
template <> class mycontainer <char> { ... };
</pre>
First of all, notice that we precede the class template name with an empty template<> parameter list. This is to explicitly declare it as a template specialization.
But more important than this prefix, is the <char> specialization parameter after the class template name. This specialization parameter itself identifies the type for which we are going to declare a template class specialization (char). Notice the differences between the generic class template and the specialization:
<pre>
template <class T> class mycontainer { ... };
template <> class mycontainer <char> { ... };
</pre>
The first line is the generic template, and the second one is the specialization.
When we declare specializations for a template class, we must also define all its members, even those exactly equal to the generic template class, because there is no "inheritance" of members from the generic template to the specialization.
Non-type parameters for templates
Besides the template arguments that are preceded by the class or typename keywords , which represent types, templates can also have regular typed parameters, similar to those found in functions. As an example, have a look at this class template that is used to contain sequences of elements:
<pre>
// sequence template
#include <iostream>
using namespace std;
template <class T, int N>
class mysequence {
    T memblock [N];
  public:
    void setmember (int x, T value);
    T getmember (int x);
};
template <class T, int N>
void mysequence<T,N>::setmember (int x, T value) {
  memblock[x]=value;
}
template <class T, int N>
T mysequence<T,N>::getmember (int x) {
  return memblock[x];
}
int main () {
  mysequence <int,5> myints;
  mysequence <double,5> myfloats;
  myints.setmember (0,100);
  myfloats.setmember (3,3.1416);
  cout << myints.getmember(0) << '\n';
  cout << myfloats.getmember(3) << '\n';
  return 0;
}
100
3.1416
</pre>
It is also possible to set default values or types for class template parameters. For example, if the previous class template definition had been:
<pre>
template <class T=char, int N=10> class mysequence {..};
</pre>
We could create objects using the default template parameters by declaring:
<pre>
mysequence<> myseq;
</pre>
Which would be equivalent to:
<pre>
mysequence<char,10> myseq;
</pre>


==== Advantages and Disadvantages ====
==== Advantages and Disadvantages ====


These actually generate different classes based on the input type. An std::vector<int> is a completely different class than an std::vector<float>. There is no support for covariance or contravariance, but there is support for passing non-types to templates, partial template specialization. They basically allow you to do whatever you want.
These actually generate different classes based on the input type. An std::vector<int> is a completely different class than an std::vector<float>. However, since C++ templates create different classes for every variation of their template parameters, the size of the compiled executable is larger. Beyond that, compilation time increases greatly, since all template code must be included with each compilation unit and much more code must be generated. However, actual runtime memory footprint is typically smaller than the alternative (frees an extra void*) and performance is better, since the compiler can perform more aggressive optimizations with the known type.
 
However, since C++ templates create different classes for every variation of their template parameters, the size of the compiled executable is larger. Beyond that, compilation time increases greatly, since all template code must be included with each compilation unit and much more code must be generated. However, actual runtime memory footprint is typically smaller than the alternative (frees an extra void*) and performance is better, since the compiler can perform more aggressive optimizations with the known type.


While a generic Java class compiles it's entire self, when using a C++ template, you only compile what you use. So, if you create an std::vector<int> and only use push_back and size, only those functions will be compiled into the object file. This eases the size of executable problem.
While a generic Java class compiles it's entire self, when using a C++ template, you only compile what you use. So, if you create an std::vector<int> and only use push_back and size, only those functions will be compiled into the object file. This eases the size of executable problem.

Revision as of 00:40, 2 October 2012

Introduction

Generic Programming is a programming paradigm for developing efficient and reusable software libraries. The Generic Programming process focuses on finding commonality among similar implementations of the same algorithm, then providing suitable abstractions so that a single, generic algorithm can cover many concrete implementations. This process is repeated until the generic algorithm has reached a suitable level of abstraction, where it provides maximal re-usability while still yielding efficient, concrete implementations.

In the simplest definition, generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by Ada in 1983, permits writing common functions or types that differ only in the set of types on which they operate on, thus reducing duplication. Such software entities are known as generics in Ada, Eiffel, Java and .NET; parametric polymorphism in ML, Scala and Haskell ; templates in C++; and parameterized types in the influential 1994 book, Design Patterns.

The term generic programming was originally coined by David Musser and Alexander Stepanov in a more specific sense than the above, to describe an approach to software decomposition whereby fundamental requirements on types are abstracted from across concrete examples of algorithms and data structures and formalized as concepts, analogously to the abstraction of algebraic theories in abstract algebra. Early examples of this programming approach were implemented in Scheme and Ada, although the best known example is the Standard Template Library (STL) in which is developed a theory of iterators which is used to decouple sequence data structures and the algorithms operating on them.

Generics in Object Oriented Languages

Generics .NET framework

Generics are classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use. A generic collection class might use a type parameter as a placeholder for the type of objects that it stores; the type parameters appear as the types of its fields and the parameter types of its methods. A generic method might use its type parameter as the type of its return value or as the type of one of its formal parameters.

The following code illustrates a simple generic class definition in C#.

public class Generic<T>
{
    public T Field;
}

The following code illustrates a simple generic class definition in Visual Basic.

Public Class Generic(Of T)
    Public Field As T
End Class

When an instance of a generic class is created, one can specify the actual types to substitute for the type parameters. This establishes a new generic class, referred to as a constructed generic class, with the chosen types substituted everywhere that the type parameters appear. The result is a type-safe class that is tailored to the user's choice of types, which is illustrated in the code below in C# and VB.

//Code in C#
public static void Main()
{
    Generic<string> g = new Generic<string>();
    g.Field = "A string";
}
'Code in Visual Basic
Public Shared Sub Main()
    Dim g As New Generic(Of String)
    g.Field = "A string" 
End Sub

Generics Terminology in .NET

  • A generic type definition is a class, structure, or interface declaration that functions as a template, with placeholders for the types that it can contain or use. For example, the Dictionary class can contain two types: keys and values. Because a generic type definition is only a template, so one cannot create instances of a class, structure, or interface that is a generic type definition.
  • Generic type parameters, or type parameters, are the placeholders in a generic type or method definition. The Dictionary generic type has two type parameters, TKey and TValue, that represent the types of its keys and values.
  • A constructed generic type, or constructed type, is the result of specifying types for the generic type parameters of a generic type definition.
  • A generic type argument is any type that is substituted for a generic type parameter.
  • The general term generic type includes both constructed types and generic type definitions.
  • Covariance and contravariance of generic type parameters enable one to use constructed generic types whose type arguments are more derived (covariance) or less derived (contravariance) than a target constructed type. Covariance and contravariance are collectively referred to as variance.
  • Constraints are limits placed on generic type parameters. For example, one might limit a type parameter to types that implement the IComparer generic interface, to ensure that instances of the type can be ordered. One can also constrain type parameters to types that have a particular base class, that have a default constructor, or that are reference types or value types. Users of the generic type cannot substitute type arguments that do not satisfy the constraints.
  • A generic method definition is a method with two parameter lists: a list of generic type parameters and a list of formal parameters. Type parameters can appear as the return type or as the types of the formal parameters. The following code shows this in C# and Visual Basic.
//Code in C#
T Generic<T>(T arg)
{
    T temp = arg;
    //... 
    return temp;
}
'Code in Visual Basic
Function Generic(Of T)(ByVal arg As T) As T
    Dim temp As T = arg
    '... 
    Return temp
End Function
  • Generic methods can appear on generic or nongeneric types. A method is not generic just because it belongs to a generic type, or even because it has formal parameters whose types are the generic parameters of the enclosing type. A method is generic only if it has its own list of type parameters. In the following code, only method G is generic.
//Code in C#
class A
{
    T G<T>(T arg)
    {
        T temp = arg;
        //... 
        return temp;
    }
}
class Generic<T>
{
    T M(T arg)
    {
        T temp = arg;
        //... 
        return temp;
    }
}
'Code in Visual Basic
Class A
    Function G(Of T)(ByVal arg As T) As T
        Dim temp As T = arg
        '... 
        Return temp
    End Function 
End Class 
Class Generic(Of T)
    Function M(ByVal arg As T) As T
        Dim temp As T = arg
        '... 
        Return temp
    End Function 
End Class

Nested Types and Generics

A type that is nested in a generic type can depend on the type parameters of the enclosing generic type. The common language runtime considers nested types to be generic, even if they do not have generic type parameters of their own. When you create an instance of a nested type, you must specify type arguments for all enclosing generic types.

Language Support

The .NET Framework provides a number of generic collection classes in the following namespaces:

  • The System.Collections.Generic namespace catalogs most of the generic collection types provided by the .NET Framework, such as the List and Dictionary generic classes.
  • The System.Collections.ObjectModel namespace catalogs additional generic collection types, such as the ReadOnlyCollection generic class, that are useful for exposing object models to users of your classes.
  • Generic interfaces for implementing sort and equality comparisons are provided in the System namespace, along with generic delegate types for event handlers, conversions, and search predicates.
  • The common language runtime provides new opcodes and prefixes to support generic types in Microsoft intermediate language (MSIL), including Stelem, Ldelem, Unbox_Any, Constrained, and Readonly.
  • Visual C++, C#, and Visual Basic all provide full support for defining and using generics.


Generics in Java

Generics were introduced in Java from JDK 1.5. Generics allows the abstraction over types. The most common examples are container types, such as those in the Collection hierarchy. So, if one could say that the code works with some unspecified type, rather than a specific interface or class, then the code uses Generics. Below is an example of a Genric:

List myIntList = new LinkedList(); 
myIntList.add(new Integer(0)); 
Integer x = (Integer) myIntList.iterator().next(); 

Typically, a programmer knows what kind of data has been placed into a particular list. However, the cast is essential. The compiler can only guarantee that an Object will be returned by the iterator. To ensure the assignment to a variable of type Integer is type safe, the cast is required. Of course, the cast not only introduces clutter, but also introduces the possibility of a run time error, since the programmer might be mistaken. It would be better if programmers could actually express their intent, and mark a list as being restricted to contain a particular data type. This is the core idea behind generics.

Here is a variation of the program fragment given above using generics:

List<Integer> myIntList = new LinkedList<Integer>();
myIntList.add(new Integer(0)); 
Integer x = myIntList.iterator().next();

The type declaration for the variable myIntList specifies that this is not just an arbitrary List, but a List of Integer, written List<Integer>. The List is a generic interface that takes a type parameter - in this case, Integer. One has to specify a type parameter when creating the list object. The compiler can now check the type correctness of the program at compile-time. Because myIntList is declared with type List<Integer>, this indicates that myIntList holds a list of integers, which holds true wherever and whenever it is used, and the compiler ensures to guarantee it. In contrast, the cast tells something the programmer thinks is true at a single point in the code. The net effect, especially in large programs, is improved readability and robustness.

Below is code for generic interfaces List and Iterator in package java.util.The content in angle brackets are the declarations of the formal type parameters of the interfaces List and Iterator.

public interface List<E> {
void add(E x);
Iterator<E> iterator();
}
public interface Iterator<E> {
E next();
boolean hasNext();
}

Generics and Subtyping

Below is an example showing code for Generics and Subtyping.

List<String> ls = new ArrayList<String>(); //1
List<Object> lo = ls; //2
lo.add(new Object()); //3
String s = ls.get(0); //4 attempts to assign an Object to a String

Line 1 is legal. Lines 2,3 and 4 aliased ls and lo. Accessing ls, a list of String, through the alias lo, one can insert arbitrary objects into it. As a result ls does not hold just Strings anymore. The Java compiler will prevent this from happening and Line 2 causes a compile time error.In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is some generic type declaration, it is not the case that G<Foo> is a subtype of G<Bar>.

Wildcards

Consider the problem of writing a routine that prints out all the elements in a collection. In older version's of Java, it can be written as below:

void printCollection(Collection c) {
   Iterator i = c.iterator();
   for (k = 0; k < c.size(); k++) {
       System.out.println(i.next());
   }
}

In the later versions of Java, the same logic above can be written using generics using the syntax of enhanced for loop:

void printCollection(Collection<Object> c) {
   for (Object e : c) {
      System.out.println(e);
   }
}

The old code could be called with any kind of collection as a parameter, the new code only takes Collection<Object>, which is not a supertype of all kinds of collections. The supertype of all kinds of collections is written Collection<?>, that is, a collection whose element type matches anything. It’s called a wildcard type. Below is the code expressing the same functionality using a wildcard.

void printCollection(Collection<?> c) {
   for (Object e : c) {
      System.out.println(e);
   }
}

The above code can be called with any type of collection. Inside printCollection(), one can still read elements from c and give them type Object. This is always safe, since whatever the actual type of the collection, it does contain objects. However, It is not safe to add arbitrary objects to it.

Generic Methods

Generic methods are used more often than generic classes. A generic class is independent of whether you have a generic method. If a method is static, it has no access to the generic type parameters of the class, so if it needs to use genericity it must be a generic method. Unlike generic class one don’t usually have to specify the parameter types, because the compiler can figure that out.

public class Gen {
   public <T> void f(T x) {
       System.out.println(x.getClass().getName());
   }

   public static void main(String[] args) {
       Gen a = new Gen();
       a.f("");
       a.f(‘c’);
   }
}

Erasure

To implement generics, the Java compiler applies type erasure to:

  • Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
  • Insert type casts if necessary to preserve type safety.
  • Generate bridge methods to preserve polymorphism in extended generic types.

Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.

Consider the example below.

public class A {
   public static void main(String[] args) {
       System.out.println(new ArrayList<String>().getClass() == new ArrayList<Integer>().getClass());
   }
}

Here ArrayList<String> and ArrayList<Integer> seem to be of different types and so one expects them to behave differently. But the execution of the above program returns true which suggests they are of the same type.

class Fn {}
class Foo<Q> {}
class Boo<X,Y> {}

public class AbsentInformation {
   public static void main(String[] args) {
      Foo<Fn> foo = new Foo<Fn>();
      Boo<Long,Double> b = new Boo<Long,Double>();

      System.out.println(Arrays.toString(foo.getClass().getTypeParameters()));
      System.out.println(Arrays.toString(b.getClass().getTypeParameters()));
   }
} 
Output:
[Q]
[X,Y]

Class.getTypeParameters() suggest that one might be able to find out what the parameter types are. But in reality, the identifiers that are used as the parameter placeholders. This is one important feature where Java differs from C++. Java generics are implemented using erasure. So when one uses generics, the specific information is erased. The only thing one knows is that we are using an object. So both List<String> and List< Integer> have the same type at runtime.

Generics in C++

Advantages and Disadvantages

These actually generate different classes based on the input type. An std::vector<int> is a completely different class than an std::vector<float>. However, since C++ templates create different classes for every variation of their template parameters, the size of the compiled executable is larger. Beyond that, compilation time increases greatly, since all template code must be included with each compilation unit and much more code must be generated. However, actual runtime memory footprint is typically smaller than the alternative (frees an extra void*) and performance is better, since the compiler can perform more aggressive optimizations with the known type.

While a generic Java class compiles it's entire self, when using a C++ template, you only compile what you use. So, if you create an std::vector<int> and only use push_back and size, only those functions will be compiled into the object file. This eases the size of executable problem.

Comparing C# and Java Generics

Java's generics implementation was based on a project originally called Pizza, which was done by Martin Odersky and others. Pizza was renamed GJ, then it turned into a JSR and ended up being adopted into the Java language. And this particular generics proposal had as a key design goal that it could run on an unmodified VM [Virtual Machine]. It is, of course, great that you don't have to modify your VM, but it also brings about a whole bunch of odd limitations. The limitations are not necessarily directly apparent, but you very quickly go, "Hmm, that's strange."

For example, with Java generics, you don't actually get any of the execution efficiency that I talked about, because when you compile a generic class in Java, the compiler takes away the type parameter and substitutes Object everywhere. So the compiled image for List<T> is like a List where you use the type Object everywhere. Of course, if you now try to make a List<int>, you get boxing of all the ints. So there's a bunch of overhead there. Furthermore, to keep the VM happy, the compiler actually has to insert all of the type casts you didn't write. If it's a List of Object and you're trying to treat those Objects as Customers, at some point the Objects must be cast to Customers to keep the verifier happy. And really all they're doing in their implementation is automatically inserting those type casts for you. So you get the syntactic sugar, or some of it at least, but you don't get any of the execution efficiency. So that's issue number one I have with Java's solution.

Issue number two, and I think this is probably an even bigger issue, is that because Java's generics implementation relies on erasure of the type parameter, when you get to runtime, you don't actually have a faithful representation of what you had at compile time. When you apply reflection to a generic List in Java, you can't tell what the List is a List of. It's just a List. Because you've lost the type information, any type of dynamic code-generation scenario, or reflection-based scenario, simply doesn't work. If there's one trend that's pretty clear to me, it's that there's more and more of that. And it just doesn't work, because you've lost the type information. Whereas in our implementation, all of that information is available. You can use reflection to get the System.Type for object List<T>. You cannot actually create an instance of it yet, because you don't know what T is. But then you can use reflection to get the System.Type for int. You can then ask reflection to please put these two together and create a List<int>, and you get another System.Type for List<int>. So representationally, anything you can do at compile time you can also do at runtime.

Comparing C# Generics to C++ Templates

To me the best way to understand the distinction between C# generics and C++ templates is this: C# generics are really just like classes, except they have a type parameter. C++ templates are really just like macros, except they look like classes.

The big difference between C# generics and C++ templates shows up in when the type checking occurs and how the instantiation occurs. First of all, C# does the instantiation at runtime. C++ does it at compile time, or perhaps at link time. But regardless, the instantiation happens in C++ before the program runs. That's difference number one. Difference number two is C# does strong type checking when you compile the generic type. For an unconstrained type parameter, like List<T>, the only methods available on values of type T are those that are found on type Object, because those are the only methods we can generally guarantee will exist. So in C# generics, we guarantee that any operation you do on a type parameter will succeed.

C++ is the opposite. In C++, you can do anything you damn well please on a variable of a type parameter type. But then once you instantiate it, it may not work, and you'll get some cryptic error messages. For example, if you have a type parameter T, and variables x and y of type T, and you say x + y, well you had better have an operator+ defined for + of two Ts, or you'll get some cryptic error message. So in a sense, C++ templates are actually untyped, or loosely typed. Whereas C# generics are strongly typed.

References

1. http://www.artima.com/intv/genericsP.html
2. http://www.generic-programming.org
3. Generics in the Java Programming Language
4. http://www.cplusplus.com/doc/tutorial/templates
5. http://osl.iu.edu/publications/prints/2003/comparing_generic_programming03.pdf
6. http://msdn.microsoft.com/en-us/library/ms172192.aspx#Y1524
7. http://www.justsoftwaresolutions.co.uk/articles/intrototemplates.pdf
8. http://www.sgi.com/tech/stl/stl_introduction.html