模板与泛型的区别

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

译文

VISUAL C + +语言参考
泛型和模板
泛型和模板都提供支持参数化类型的语言功能。但是,它们是不同的,有不同的用途。本主题提供了一个概述的许多差异。


泛型和C + +模板之间的主要差异:

  • 泛型是通用的,直到它们在运行时类型取代。模板是专门在编译的时候,所以他们不仍然
  • 特别是公共语言运行库支持仿制药在MSIL。由于运行时知道仿制药,具体类型可以被取代时,泛型类型的引用程序集包含一个泛型类型。相比之下,模板,解决成普通类型在编译时产生的类型可能不会专门在其他组件中。
  • 专门在两个不同的组件与同类型参数的泛型是相同的类型。专门在两个不同的组件与同类型参数的模板被认为是由运行时是不同的类型。
  • 泛型一段可执行代码,用于所有的引用类型参数(这是不正确的值类型,它有一个独特的值类型实施每)作为一个单一的生成。 JIT编译器知道泛型和引用或值类型作为类型参数,能够对代码进行优化。模板生成单独的运行时代码为每个专业化。
  • 泛型不允许非类型的模板参数,,如模板<int I】C {}。模板允许他们。
  • 泛型不允许显式特例(即一个特定类型的模板的自定义实现)。模板做。
  • 泛型不允许部分专业化(类型参数)的一个子集的自定义实现。模板做。
  • 泛型不允许被用作类型参数的泛型类型的基类。模板做。
  •  不允许泛型类型参数的默认值。模板做。
  • 模板支持模板的模板参数(如模板<模板<class T>类X>类MyClass),但仿制药没有。

结合模板和泛型
       
在仿制药的基本区别有用于构建应用程序相结合的模板和泛型的影响。例如,假设你有一个模板类,你想创建一个通用的包装,使该模板其他语言作为一种通用的。你不能有泛型类型参数,然后通过虽然模板,因为模板在编​​译的时候,需要有该类型的参数,但通用将无法解决,直到运行时的类型参数。嵌套一个通用的模板里面也不会奏效,因为有没有办法扩大模板在编译的时候可以在运行时实例任意泛型类型。

例子:下面的例子显示了一个简单的例子,使用模板和泛型。在这个例子中,模板类通过它的参数通过通用类型。相反的是不可能的。

       这个成语可能被用来当你想建立的现有通用的API与模板代码,是当地一个Visual C + +组装,或当你需要来添加额外的参数化层泛型类型,以利用某些功能时,模板不支持仿制药。

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值