C++模板实例掌握

简介: 前段时间重新学习C++,主要看C++编程思想和C++设计新思维。对模版的使用有了更进一层的了解,特总结如下:下面列出了模版的常用情况: //1. 模板类静态成员template  struct testClass {     static int _data; }; template int t...

前段时间重新学习C++,主要看C++编程思想和C++设计新思维。对模版的使用有了更进一层的了解,特总结如下:

下面列出了模版的常用情况:

// 1. 模板类静态成员
template < typename T >   struct testClass
{
   
static int _data;
}
;
template
<>   int testClass < char > ::_data =   1 ;
template
<>   int testClass < long > ::_data =   2 ;
int main( void ) {
    cout
<< boolalpha << (1==testClass<char>::_data) << endl;
    cout
<< boolalpha << (2==testClass<long>::_data) << endl;
}
 

// 2. 模板类偏特化
template < class I, class O >   struct testClass
{
    testClass()
{ cout << "I, O" << endl; } 
}
;
template
< class T >   struct testClass < T * , T *>  
{
    testClass()
{ cout << "T*, T*" << endl; } 
}
;
template
< class T >   struct testClass < const T * , T *>  
{
    testClass()
{ cout << "const T*, T*" << endl; } 
}
;
int main( void )
{
    testClass
<int, char> obj1;
    testClass
<int*, int*> obj2;
    testClass
<const int*, int*> obj3;
}
 

// 3.类模版+函数模版
template < class T >   struct testClass
{
   
void swap( testClass<T>& ) { cout << "swap()" << endl; } 
}
;
template
< class T > inline void swap( testClass < T >& x, testClass < T >& y )
{
    x.swap( y );
}
 
int main( void )
{
    testClass
<int> obj1;
    testClass
<int> obj2;
    swap( obj1, obj2 );
}
 


// 4. 类成员函数模板
struct testClass
{
    template
<class T> void mfun( const T& t )
   
{
        cout
<< t << endl;
    }
 
    template
<class T> operator T()
   
{
       
return T();
    }
 
}
;
int main( void )
{
    testClass obj;
    obj.mfun(
1 );
   
int i = obj;
    cout
<< i << endl;
}
 

// 5. 缺省模板参数推导
template < class T >   struct test
{
    T a;
}
;
template
< class I, class O = test < I >   >   struct testClass
{
    I b;
    O c;
}
;

void main()
{
}



// 6. 非类型模板参数
template < class T, int n >   struct testClass {
    T _t;
    testClass() : _t(n)
{
    }
 
}
;
int main( void ) {
    testClass
<int,1> obj1;
    testClass
<int,2> obj2;
}
 


// 7. 空模板参数
template < class T >   struct testClass;
template
< class T >   bool   operator == ( const testClass < T >& , const testClass < T >& )
{
   
return false;
}
;
template
< class T >   struct testClass
{
    friend
bool operator== <>( const testClass&, const testClass& );
}
;
void main()
{
}


// 8. template template 类
struct Widget1
{
template
<typename T> 
    T foo()
{} 
}
;

template
< template < class T > class X >  
struct Widget2
{
}
;
void main()
{
    cout
<< 3 << '\n';
}




//参考:http://www.cnblogs.com/dayouluo/archive/2005/05/14/155092.html

特别注意:类,全局函数,类的成员函数都可以特化,但是只有类可以半特化,全局函数和类的成员函数不可以半特化。

// -------------------------------------------
// 1 类的特化和类成员函数的特化
template < typename T >
class Widget1
{
public:
   
void Fun1()
   
{
       
//generic implementation
    }

   
}
;

template
<>
class Widget1 < int >
{
public:
   
void Fun1()
   
{
    }

}
;
template
<>  
void Widget1 < char > ::Fun1()
{
   
//specialization
}


void main()
{

  Widget1
<char> w;
  w.Fun1();
  Widget1
<int> w2;
  w2.Fun1();
 
}

// -------------------------------------------
// 2 全局函数的特化和重载
template < typename T1, typename T2 >
T1 Fun2(T2)
{
}


// 下面2个应该是属于重载
template < typename T2 >
char Fun2(T2)
{
   
char c;
   
return c;
}


template
< typename T1 >
T1 Fun2(
char )
{
}


// 全局函数的特化
template <>
char Fun2 < char , int > ( int )
{
   
char c;
   
return c;
}

int main()
{
}

// -------------------------------------------
// 3 全局函数不能半特化,以下编译失败
template < typename T1,typename T2 >   // 原型1
void Test(T1,T2)
{
}


template
< typename T1 >
void Test < T1,T1 > (T1,T1)
{
}


template
< typename T1, typename T2 >   // 原型2
T1 Fun2(T2)
{
}

//
template < typename T2 >
int Fun2 < int ,T2 > (T2)
{
}

template
< typename T1 >
T1 Fun2
< T1, int > ( int )
{
}

template
< typename T >
T Fun2
< T,T > (T)
{
}

int main()
{
}



////-------------------------------------------
////4 类可以特化和半特化,但是特的成员函数像全局函数一样,只能特化,不能半特化,

template < typename T1, typename T2 >   struct Widget2
{
 
void Fun2()
 
{
     
//generic implementation
  }

}
;

template
< typename T2 >  
struct Widget2 < char ,T2 >
{
   
void Fun2()
   
{
    }

}
;

template
< typename T2 >
struct widget2
{
   
void Fun2()
   
{
       
// class partial specialization
    }

}
;



// the class member funtion can not be partial specialization
// 以下的成员函数半特化,编译失败
template < typename T2 >
void Widget2 < char , T2 > ::Fun2()
{
   
//class member function partial specialization
}

int main()
{
}

 

参考:C++编程思想2

http://www.cnblogs.com/feisky/archive/2009/11/04/1596203.html

img_e00999465d1c2c1b02df587a3ec9c13d.jpg
微信公众号: 猿人谷
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
如果您希望与我交流互动,欢迎关注微信公众号
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

目录
相关文章
|
7天前
|
安全 编译器 C++
C++一分钟之-编译时计算:constexpr与模板元编程
【6月更文挑战第28天】在C++中,`constexpr`和模板元编程用于编译时计算,提升性能和类型安全。`constexpr`指示编译器在编译时计算函数或对象,而模板元编程通过模板生成类型依赖代码。常见问题包括误解constexpr函数限制和模板递归深度。解决策略包括理解规则、编写清晰代码、测试验证和适度使用。通过实战示例展示了如何使用`constexpr`计算阶乘和模板元编程计算平方。
30 13
|
4天前
|
存储 编译器 C++
【C++】详解C++的模板
【C++】详解C++的模板
|
3天前
|
C++ 开发者
C++一分钟之-编译时计算:constexpr与模板元编程
【7月更文挑战第2天】C++的`constexpr`和模板元编程(TMP)实现了编译时计算,增强代码效率。`constexpr`用于声明编译时常量表达式,适用于数组大小等。模板元编程则利用模板进行复杂计算。常见问题包括编译时间过长、可读性差。避免方法包括限制TMP使用,保持代码清晰。结合两者可以解决复杂问题,但需明确各自适用场景。正确使用能提升代码性能,但需平衡复杂性和编译成本。
13 3
|
2天前
|
编译器 C语言 C++
【C++】模板初阶(下)
C++的函数模板实例化分为隐式和显式。隐式实例化由编译器根据实参推断类型,如`Add(a1, a2)`,但`Add(a1, d1)`因类型不一致而失败。显式实例化如`Add&lt;double&gt;(a1, d1)`则直接指定类型。模板函数不支持自动类型转换,优先调用非模板函数。类模板类似,用于创建处理多种数据类型的类,如`Vector&lt;T&gt;`。实例化类模板如`Vector&lt;int&gt;`和`Vector&lt;double&gt;`创建具体类型对象。模板使用时,函数模板定义可分头文件和实现文件,但类模板通常全部放头文件以避免链接错误。
|
2天前
|
机器学习/深度学习 算法 编译器
【C++】模板初阶(上)
**C++模板简介** 探索C++泛型编程,通过模板提升代码复用。模板作为泛型编程基础,允许编写类型无关的通用代码。以`Swap`函数为例,传统方式需为每种类型编写单独函数,如`Swap(int&)`、`Swap(double&)`等,造成代码冗余。函数模板解决此问题,如`template&lt;typename T&gt; void Swap(T&, T&)`,编译器根据实参类型推导生成特定函数,减少重复代码,增强可维护性。模板分函数模板和类模板,提供处理不同数据类型但逻辑相似的功能。
|
2天前
|
算法 编译器 程序员
|
2天前
|
存储 编译器 程序员
|
5天前
|
安全 C++
详细解读c++异常模板复习
详细解读c++异常模板复习
|
8天前
|
存储 算法 编译器
程序与技术分享:C++模板元编程简介
程序与技术分享:C++模板元编程简介
|
8天前
|
SQL 人工智能 算法
技术心得记录:模板函数函数模板FunctionTemplate(C++Primer
技术心得记录:模板函数函数模板FunctionTemplate(C++Primer