Sei sulla pagina 1di 3

Difference between C# generics and C++ templates S.

No 1 C# Generics Strongly / Loosely typed: C# Generic types are strong typed. Instantiated at Compile time / Runtime: C# Generic types are instantiated at the runtime. Whether it permits the type parameters to have default values ? C# Generic types do not permit the type parameters to have default values. Whether DLL can be created using C# Generics ? Libraries or DLLs can be created using C# Generics. C++ Templates Strongly / Loosely typed: C++ Templates are loosely typed. Instantiated at Compile time / Runtime: C++ templates are instantiated at the compile time. Whether it permits the type parameters to have default values ? C++ templates permit the type parameters to have default values. 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 explicit Whether it is possible to call arithmetic operators in a C++ template class ? With C++ template class, it is possible to call arithmetic operators.

Whether explicit specialization is

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. 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 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 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. 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.

supported by C++ templates ? C++ template support explicit specialization.

Whether partial specialization is supported by C++ templates ? C++ template support partial specialization.

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. Whether template type parameter can itself be a template? C++ does allow template parameters.

10

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. Which language supports templates ? Templates are a C++ only language feature. Whether it allows non-template

11

Which language supports generics? Generics can be supported by any .NET language that wishes to do so. Whether it allows non-

12

template parameters ? C# does not allow non-type template parameters.

parameters ? C++ allows non-type template parameters.

References: http://webdesignpluscode.blogspot.in/2012/07/compare-c-net-generics-and-ctemplates.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-csharp 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/

Potrebbero piacerti anche