泛型和模板的比较----源自MSDN

2023-11-29 02:58
文章标签 模板 比较 泛型 源自 msdn

本文主要是介绍泛型和模板的比较----源自MSDN,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

                泛型和模板的比较----源自MSDN

 

在我的《主流编程语言优劣考》一文中,有不少朋友对我把“模板”和“泛型”这2个概念作为2个不同的事务来看待有异议。

我告诉他们,在MSDNC++/CLI中就有这样的定义。他们不信。

唉!我就搞不懂了。为什么有些人会这么在意观点、定义的出处呢?

难道不是名人说的,就肯定不是真理吗?难道权威就一定正确吗?

    在这里我把MSDN的原文拿出来给那些朋友看。

 

出自

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

 

MSDN

  • MSDN 主页
  • MSDN 技术资源库
  • MSDN 学习
  • MSDN 下载
  • MSDN 支持
  • MSDN 社区

MSDN Library

  • 开发工具和语言
  • .NET 开发
  • Office Development
  • SQL Server
  • Windows Live
  • 技术文章

<script type=text/Javascript> </script> 语言筛选器 : 全部

Visual Basic

C#

C++

J#

JScript

XAML

<script type=text/Javascript> var ArrowOffPath="http://i.msdn.microsoft.com/Platform/Controls/DropDownFilter/resources/arrow-off.gif"; var ArrowOnPath="http://i.msdn.microsoft.com/Platform/Controls/DropDownFilter/resources/arrow-on.gif"; var strConstLangFilterAll ="全部"; var strConstLangFilterMulti ="多个"; var strConstLangFilterNone ="无"; var strConstLangFilterText ="语言筛选器"; var oMTPS_DD_ImgArrow = document.getElementById("ctl00_rs1_DropDownFilter_MTPS_DD_ImageArrow"); var oMTPS_DD_PanelLink = document.getElementById("ctl00_rs1_DropDownFilter_Mtps_DropDownFilterText"); var oMTPS_DD_Div = document.getElementById("ctl00_rs1_DropDownFilter_DropDownFilterMain"); var oMTPS_DD_PopUpDiv = document.getElementById("ctl00_rs1_DropDownFilter_Mtps_DropDownPopUp"); </script> This page is specific to

Microsoft Visual Studio 2005/.NET Framework 2.0

Other 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 support for parameterized types. However, they are different and have different uses. This topic provides an overview of the many differences.

For more information, see Managed Templates and Templates Overview.

Comparing Templates and Generics

Key differences between generics and C++ templates:

·         Generics are generic until the types are substituted for them at runtime. Templates are specialized at compile time so they are not still parameterized types at runtime

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

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

·         Generics are generated as a single piece of executable code which is used for all reference type arguments (this is not true 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 the reference or value types that are used as type 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 (that is, a custom implementation of 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 not allow the type parameter to be 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 generics do not.

Combining Templates and Generics

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

Example

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

This idiom could be used when you want to build on an existing generic API with template code that is local to a Visual C++ assembly, or when you need to add an extra layer of parameterization to a generic type, to take advantage of certain features of templates not supported by generics.

复制代码

// templates_and_generics.cpp
// compile with: /clr
using namespace System;
 
generic <class ItemType>
ref class MyGeneric {
   ItemType m_item;
 
public:
   MyGeneric(ItemType item) : m_item(item) {}
   void F() { 
      Console::WriteLine("F"); 
   }
};
 
template <class T>
public ref class MyRef {
MyGeneric<T>^ ig;
 
public:
   MyRef(T t) {
      ig = gcnew MyGeneric<T>(t);
      ig->F();
    }    
};
 
int main() {
   // instantiate the template
   MyRef<int>^ mref = gcnew MyRef<int>(11);
}

Output

F

See Also

Other Resources

Generics (Visual C++)

标记 : 添加标记 添加   取消

标记为 ContentBug

社区内容  

添加新内容

  批注

Using templates to implement generic interfaces

    

Rob Grainger   |   编辑   |   显示历史记录

请稍候  

As a warning, techniques like the one following won't work either...

generic <typename T> public interface class ICollection { void Add(T item); };
class Immutable { bool IsReadOnly() { return true; } };
class Mutable { bool IsReadOnly() { return false; } };
template <typename T, typename M>
ref class MyCollection : public ICollection<T>
{
    typedef M MutType;
public: 
    void Add(T item) { 
        if (MutType::IsReadOnly() throw gcnew NotSupportedException(); 
        internal.Add(item);
    }
    System::Collections::Generic::List<T> internal;
};
public ref class Element { ... };
public ref class FactoryClass
{
    public:
        static ICollection<Element^> CreateMutableCollection() { return gcnew MyCollection<Element^, Mutable>(); }
        static ICollection<Element^> CreateImmutableCollection() { return gcnew MyCollection<Element^, Immutable>(); }
};

Compiling the above, and calling CreateMutableCollection and CreateImmutableCollection from another assembly, always (in my case - I'm not sure the heuristics the compiler used) chose MyCollection<Element^, Mutable>, so it appears that the compiler only instantiates the template once, and uses that instantiation for all references. Shame, as this would be a useful combination of the two techniques, allowing (internally) manufacturing implementations of a generic interface using templates.

 

 

 

 

 

 

 

 

 

 

 

这篇关于泛型和模板的比较----源自MSDN的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/431189

相关文章

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

C# Where 泛型约束的实现

《C#Where泛型约束的实现》本文主要介绍了C#Where泛型约束的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用的对象约束分类where T : structwhere T : classwhere T : ne

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

对postgresql日期和时间的比较

《对postgresql日期和时间的比较》文章介绍了在数据库中处理日期和时间类型时的一些注意事项,包括如何将字符串转换为日期或时间类型,以及在比较时自动转换的情况,作者建议在使用数据库时,根据具体情况... 目录PostgreSQL日期和时间比较DB里保存到时分秒,需要和年月日比较db里存储date或者ti

基于Java实现模板填充Word

《基于Java实现模板填充Word》这篇文章主要为大家详细介绍了如何用Java实现按产品经理提供的Word模板填充数据,并以word或pdf形式导出,有需要的小伙伴可以参考一下... Java实现按模板填充wor编程d本文讲解的需求是:我们需要把数据库中的某些数据按照 产品经理提供的 word模板,把数据