Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Difference between c# generics and c++ templates
1. Difference between C# generics and C++ templates
S.No C# Generics C++ Templates
1 Strongly / Loosely typed: Strongly / Loosely typed:
C# Generic types are strong C++ Templates are loosely typed.
typed.
2 Instantiated at Compile Instantiated at Compile time /
time / Runtime: Runtime:
C# Generic types are C++ templates are instantiated at
instantiated at the runtime. the compile time.
3 Whether it permits the type Whether it permits the type
parameters to have default parameters to have default
values ? values ?
C# Generic types do not permit C++ templates permit the type
the type parameters to have parameters to have default values.
default values.
4 Whether DLL can be created Whether DLL can be created
using C# Generics ? using C++ Templates ?
Libraries or DLLs can be C++ templates are always
created using C# Generics. 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 Whether it is possible to call
arithmetic operators in a C# arithmetic operators in a C++
generic class ? template class ?
C# generics do not provide the With C++ template class, it is
same amount of flexibility as possible to call arithmetic
C++ templates. For example, it operators.
is not possible to call arithmetic
operators in a C# generic
class, although it is possible to
call user defined operators.
6 Whether explicit Whether explicit specialization is
2. specialization is supported supported by C++ templates ?
by C# generics ? C++ template support explicit
C# generics does not support specialization.
explicit specialization; that is, a
custom implementation of a
template for a specific type.
7 Whether partial Whether partial specialization is
specialization is supported supported by C++ templates ?
by C# generics ? C++ template support partial
C# generics does not support specialization.
partial specialization: a custom
implementation for a subset of
the type arguments.
8 Whether type parameter can Whether type parameter can be
be used as the base class used as the base class for the
for the generic type ? template type ?
C# does not allow the type C++ allows the type parameter to
parameter to be used as the be used as the base class for the
base class for the generic type. template type.
9 Whether generic type Whether template type
parameter can itself be a parameter can itself be a
generic ? template?
In C#, a generic type C++ does allow template
parameter cannot itself be a parameters.
generic, although constructed
types can be used as generics.
10 What type of code it allows ? What type of code it allows ?
C# requires code in a class to C++ allows code that might not be
be written in such a way that it valid for all type parameters in the
will work with any type that template, which is then checked for
satisfies the constraints.C# the specific type used as the type
disallows as allowed in C++ parameter.
(Please look at example from For example, in C++ it is possible
R.H.S column); the only to write a function that uses the
language constructs allowed arithmetic operators + and - on
are those that can be deduced objects of the type parameter,
from the constraints. 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 Which language supports
generics? templates ?
Generics can be supported by Templates are a C++ only
any .NET language that wishes language feature.
to do so.
12 Whether it allows non- Whether it allows non-template
3. template parameters ? parameters ?
C# does not allow non-type C++ allows non-type template
template parameters. parameters.
References:
http://webdesignpluscode.blogspot.in/2012/07/compare-c-net-generics-and-c-
templates.html
http://my.safaribooksonline.com/book/programming/csharp/9781430225379/generics/differ
ence_between_generics_and_c_plus_p
http://stackoverflow.com/questions/63694/creating-a-math-library-using-generics-in-c-
sharp
http://msdn.microsoft.com/en-us/library/c6cyy67b%28v=vs.80%29.aspx
http://www.codeguru.com/csharp/.net/cpp_managed/windowsservices/article.php/c7423/C
omparing-NET-Generics-and-C-Templates.htm
And, further updates on difference between questions and answers, please visit my
blog @ http://onlydifferencefaqs.blogspot.in/