template <class elemType, int size>
public ref class tStack
{
  array<elemType> ^m_stack;
  int top;
  public:
   tStack() : top( 0 ) 
   { 
    m_stack = gcnew array<elemType>( size );
   }
};// 帶有默認值的模板聲明
template <class elemType, int size = 1024>
public ref class FixedSizeStack {};// 最多128個字符串實例的堆棧
FixedSizeState<String^, 128> ^tbs = gcnew FixedSizeStack<String^, 128>;// 最多1024個字符串實例的堆棧
FixedSizeStack<String^> ^tbs = gcnew FixedSizeStack<String^>;// ISO-C++名字空間std中的默認類型參數值示例 
{
  template <class T, class Container = deque<T> > 
  class queue;
  template <class T, class Allocator = allocator<T> > 
  class vector;
  // ...
}// 帶有默認的元素類型的模板聲明 
template <class elemType=String^, int size=1024>
public ref class tStack {};typedef void (*handler)( ... array<Object^>^ );
template <class elemType, int size, handler cback >
public ref class tStack {};void defaultHandler( ... array<Object^>^ ){ ... }
                         
template < class elemType, 
int size = 1024, 
handler cback = &defaultHandler >
public ref class tStack {};void demonstration() 
{
  // 默認的大小和處理程序
  tStack<String^> ^ts1 = nullptr; 
  // 默認的處理程序
  tStack<String^, 128> ^ts2 = gcnew tStack<String^, 128>;
  // 重載所有的三個參數
  tStack<String^, 512, &yourHandler> ^ts3;
}// template模板參數
template <template <class T> class arena, class arenaType>
class Editor { 
  arena<arenaType> m_arena; 
  // ...
};// 模板緩沖區類
template <class elemType>
public ref class tBuffer {};
void f()
{
  Editor<tBuffer,String^> ^textEditor;
  Editor<tBuffer,char> ^blitEditor;
  // ...
}  	   類型參數約束template <class T>
ref class Demonstration {
  int method() {
   typename T::A *aObj; 
   // ...
  }
};int demoMethod()
{
  Demonstration<int> ^demi = 
  gcnew Demonstration<int>( 1024 );
  return dm->method();
}error C2825: ’T’: must be a class or namespace when followed by ’::’template <class elemType, int size=1024>
ref class Container 
{
  array<elemType> ^m_buf;
  int next;
  public:
   bool search( elemType et )
   {
    for each ( elemType e in m_buf )
     if ( et == e )
      return true;
    return false;
   }
  Container() 
  { 
   m_buf = gcnew array<elemType>(size); 
   next = 0; 
  }
void add( elemType et )
{ 
  if ( next >= size ) 
  throw gcnew Exception; 
  m_buf[ next++ ] = et; 
}
elemType get( int ix )
{
  if ( ix < next ) 
   return m_buf[ ix ]; 
  throw gcnew Exception; 
}
// ...
};template <class elemType, int size=1024>
ref class Container 
{
  // 其它的都相同 ...
  // 這是一個函數成員模板...
  // 它可以同時引用包含的類參數和自有參數...
  template <class Comparer>
  bool search( elemType et, Comparer comp )
  {
   for each ( elemType e in m_buf )
    if ( comp( et, e ) )
     return true;
    return false;
  }
  // ...
};class EqualGuy {
public: 
  bool Operator()( String^ s1, String^ s2 )
  {
      ![]() 
    ![]() 
    ![]() 
        ![]() 
                                                    ![]() 
        ![]() 
    ![]() 
    ![]() return s1->CompareTo( s2 ) == 0;
     return s1->CompareTo( s2 ) == 0;
  }
};int main()
{
  Container<String^> ^sxc = gcnew Container<String^>;
  sxc->add( "Pooh" );
  sxc->add( "Piglet" );
  // 成員模板搜索 ...
  if ( sxc->search( "Pooh", EqualGuy() ) )
   Console::WriteLine( "found" );
  else Console::WriteLine( "not found" );
  // 傳統的等于搜索 ...
  if ( sxc->search( "Pooh" ) )
   Console::WriteLine( "found" );
  else Console::WriteLine( "not found" );
} generic <class elemType>
public ref class Container 
{
  array<elemType> ^m_buf;
  int next;
  int size;
  public:
   bool search( elemType et )
   {
    for each ( elemType e in m_buf )
     if ( et->CompareTo( e ))
      return true;
     return false;
   }
   Container( int sz ) 
   { 
    m_buf = gcnew array<elemType>(size = sz); 
    next = 0; 
   }
   // add() 和 get() 是相同的 ... 
};error C2039: ’CompareTo’ : is not a member of ’System::Object’error C2676: binary ’==’ : ’elemType’ does not define this operator
or a conversion to a type acceptable to the PRedefined operatorgeneric <class elemType>
where elemType : IComparable
public ref class Container 
{
// 類的主體沒有改變 ... 
};int main()
{
  // 正確的:String和int實現了IComparable
  Container<String^> ^sc; 
  Container<int> ^ic; 
  //錯誤的:StringBuilder沒有實現IComparable 
  Container<StringBuilder^> ^sbc; 
}generic <class T1, class T2, class T3>
where T1 : IComparable, ICloneable, Image
where T2 : IComparable, ICloneable, Image
where T3 : ISerializable, CompositeImage
public ref class Compositor 
{
// ...
};generic <class T1, class T2, class T3>
// 錯誤的:同一個參數不答應有兩個條目
      ![]() 
    ![]() 
    ![]() 
        ![]() 
                                                    ![]() 
        ![]() 
    ![]() 
    ![]() where T1 : IComparable, ICloneable
  where T1 : IComparable, ICloneable
where T1 : Image
public ref class Compositor 
{
// ...
};generic <class T1, class T2>
where T1 : IComparable<T1>
where T2 : IComparable<T2>
public ref class Compositor 
{
// ... 
};generic <class T1, class T2>
public ref class BlackWhite_Compositor : Compositor
{
// ... 
};generic <class T1, class T2>
where T1 : IComparable<T1>
where T2 : IComparable<T2>
public ref class BlackWhite_Compositor : Compositor
{
// ...
};新聞熱點
疑難解答