9.22.2012

C# generics vs C++ templates

Difference between C# generics and C++ templates
S.No
C# Generics
C++ Templates
1
Strongly / Loosely typed:
C# Generic types are strongly typed.
Strongly / Loosely typed:
C++ Templates are loosely typed.
2
Instantiated at Compile time / Runtime:
C# Generic types are instantiated at the runtime.
Instantiated at Compile time / Runtime:
C++ templates are instantiated at the compile time.
3
Whether it permits the type parameters to have default values ?
C# Generic types do not permit the type parameters to have default values.
Whether it permits the type parameters to have default values ?
C++ templates permit the type parameters to have default values.
4
Whether DLL can be created using C# Generics ?
Libraries or DLLs can be created using C# Generics.
Whether DLL can be created using C++ Templates ?
C++ templates are always expanded at compile time. Therefore, the C++ compiler must have access to all template types—generally through header files—and any types used to create the closed types from the template types at compile time. For this reason alone, it is impossible to package C++ templates into libraries or DLLs.

The STL is a perfect example: notice that almost every bit of your favorite STL implementation exists in header files.
5
Whether it is possible to call arithmetic operators in a C# generic class ?
C# generics do not provide the same amount of flexibility as C++ templates. For example, it is not possible to call arithmetic operators in a C# generic class, although it is possible to call user defined operators.
Whether it is possible to call arithmetic operators in a C++ template class ?
With C++ template class, it is possible to call arithmetic operators.
6
Whether explicit specialization is supported by C# generics ?
C# generics does not support explicit specialization; that is, a custom implementation of a template for a specific type.
Whether explicit specialization is supported by C++ templates ?
C++ template support explicit specialization.
7
Whether partial specialization is supported by C# generics ?
C# generics does not support partial specialization: a custom implementation for a subset of the type arguments.
Whether partial specialization is supported by C++ templates ?
C++ template support partial specialization.
8
Whether type parameter can be used as the base class for the generic type ?
C# does not allow the type parameter to be used as the base class for the generic type.
Whether type parameter can be used as the base class for the template type ?
C++ allows the type parameter to be used as the base class for the template type.
9
Whether generic type parameter can itself be a generic ?
In C#, a generic type parameter cannot itself be a generic, although constructed types can be used as generics.
Whether template type parameter can itself be a template?
C++ does allow template parameters.
10
What type of code it allows ?
C# requires code in a class to be written in such a way that it will work with any type that satisfies the constraints.C# disallows as allowed in C++ (Please look at example from R.H.S column); the only language constructs allowed are those that can be deduced from the constraints.
What type of code it allows ?
C++ allows code that might not be valid for all type parameters in the template, which is then checked for the specific type used as the type parameter.
For example, in C++ it is possible to write a function that uses the arithmetic operators + and - on objects of the type parameter, which will produce an error at the time of instantiation of the template with a type that does not support these operators.
11
Which language supports generics?
Generics can be supported by any .NET language that wishes to do so.
Which language supports templates ?
Templates are a C++ only language feature.
12
Whether it allows non-template parameters ?
C# does not allow non-type template parameters.
Whether it allows non-template parameters ?
C++ allows non-type template parameters.

References:





No comments:

Post a Comment