Generics vs Templates

  • Thread starter Thread starter Mr.Tickle
  • Start date Start date
M

Mr.Tickle

So whats the deal here regarding Generics in the 2004 release and templates
currently in C++?
 
Mr.Tickle said:
So whats the deal here regarding Generics in the 2004 release and
templates currently in C++?

Of course, details about exactly what will be released in the next major
Visual Studio release have not been made public yet, but we can speculate a
bit.

If you compare the CLR generics proposed by Microsoft Research (see
http://research.microsoft.com/projects/clrgen/generics.pdf) to C++
templates, you'll find some differences:

* CLR Generics are supported in the CLR, with instantiation occuring at
runtime. Full metadata exists for the generic type, and generic
instantiations can be reflected upon through meta-data.

* C++ Templates are a compile-time only mechanism. C++ template
instantiations are ordinary classes with extraordinary names. When compiled
to managed code, each template is a distinct .NET class, and the
meta-relationship between those classes is not represented in the metadata
anywhere.

* C++ templates support partial specialization. There's nothing comparable
in CLR Generics.

* CLR Generics support type constraints on generic parameters. There's
nothing comparable in C++ templates (although it's possible to synthesize
similar mechanisms in some cases).

* C++ Templates are a Turing-complete functional language that's executed by
the compiler. CLR Generics are simply generic types.

-cd
 
Carl Daniel said:
If you compare the CLR generics proposed by Microsoft Research (see
http://research.microsoft.com/projects/clrgen/generics.pdf) to C++
templates, you'll find some differences:

* CLR Generics are supported in the CLR, with instantiation occuring at
runtime. Full metadata exists for the generic type, and generic
instantiations can be reflected upon through meta-data.

Another difference (going by the MSR paper) is that you cannot create
instances of the type parameter. This prevents idioms like:

stack<vector<T> >

where vector<T> is the underlying implementation used by the stack facade
class. Or:

pool<T>

where the pool class creates and maintains a cache of T objects.

Ken
 
Back
Top