arm-linux

http://armboard.taobao.com/

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

 

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

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

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

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

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

 

出自

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

 

MSDN

MSDN Library

语言筛选器 : 全部

Visual Basic

C#

C++

J#

JScript

XAML

This page is specific to

Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:

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.

 

 

 

 

 

 

 

 

 

 

 

posted on 2008-08-04 00:09  arm-linux  阅读(9374)  评论(0编辑  收藏  举报