Comparison of generics and templates----originating from MSDN

Source: Internet
Author: User

Comparison of generics and templates----originating from msdn<?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

In my essay on the pros and cons of the mainstream programming language, I have many friends who disagree with the 2 concepts of "template" and "generics" as 2 different issues.

I told them that there was such a definition in the MSDN c++/cli. They don't believe it.

Alas. I don't get it. Why do some people care so much about the source of ideas and definitions?

Is it not the truth that is not what the celebrity says? is authority necessarily correct?

Here I take the original of MSDN and show it to the friends.

From

Http://msdn.microsoft.com/zh-cn/library/sbh15dya (en-us,vs.80). aspx

Msdn

<?xml:namespace prefix = v ns = "URN:SCHEMAS-MICROSOFT-COM:VML"/>MSDN homepage

MSDN Technology Resource Pool

MSDN Learning

MSDN Downloads

MSDN Support

MSDN Community

MSDN Library

Development tools and languages

. NET Development

Office Development

SQL Server

Windows Live

Technical Articles

Language filters: All

Visual Basic

C#

C++

J #

Jscript

Xaml

This page was specific to

Microsoft Visual Studio 2005/.net Framework 2.0

Versions are also available for the following:

. NET Framework 3.0

Microsoft Visual Studio 2008/.net Framework 3.5

Visual C + + Language Reference

Generics and Templates

Generics and templates are both language features that provide the for support parameterized. However, they are different and have different. This is topic provides an overview of the many differences.

For more information, Managed templates and Templates Overview.

comparing Templates and generics

Key differences between generics and C + + templates:

· Generics are generic until the types are for substituted in them. Templates are specialized at compile when they are not still parameterized at types

· The common language runtime specifically supports in MSIL. Because the runtime knows about generics, specific types can be substituted for generic types when referencing a assembly containing a generic type. Templates, in contrast, resolve to ordinary types at compile time and the resulting of May is types in specialized R assemblies.

· Generics specialized in two different assemblies with the same type arguments the are type. Templates specialized in two different assemblies with the same type arguments are considered by the runtime NT types.

· Generics are generated as a single piece the executable code which is used to all reference type arguments (this isn't TR UE for value types, which have a-unique implementation per value type). The JIT compiler knows about generics and is able to optimize the "code for" reference or value types that are used as T ype arguments. Templates generate separate runtime code for each specialization.

· Generics do not allow Non-type template parameters, such as template <int i> C {}. Templates allow them.

· Generics do not allow explicit specialization (which is, a custom implementation to a template for a specific type). Templates do.

· Generics do not allow partial specialization (a custom implementation for a subset of the type arguments). Templates do.

· Generics do is allow the type parameter to is used as the base class for the generic type. Templates do.

· Generics do not allow type parameters to have default values. Templates do.

· Templates support template-template parameters (e.g. Template<template<class t> class x> class MyClass), but G Enerics does not.

combining Templates and generics

· The basic difference in generics has is implications for building applications, that combine templates and generics. For example, suppose your have a template class that you want to create a generic the for to wrapper this expose to OT Her languages as a generic. You cannot have the generic take a type parameter this it then passes though to the template since the template to Have that type parameter at compile time, but the generic won ' t resolve the type parameter until. Nesting a template inside a generic won ' t work either because there ' s no way to expand the templates at compile time for a Rbitrary generic types that could is instantiated at runtime.

Example

The following example shows a simple example of the using templates and generics together. In this example, the template class passes it parameter through to the generic type. The reverse is not possible.

This idiom could are used when you are want to builds on a existing generic APIs with template code, is, local to a Visual c+ + assembly, or when you are need to add a extra layer of parameterization to a generic type, to take advantage of certain Tures of templates not supported by generics.

Copy Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.