寻找C++实用的内联模板函数,纪念!

原文地址:http://bbs.gimoo.net/thread/166549-1.html

引注:作者太多,没法一一署名,都是常用的。也欢迎大家共享,我会整理进去。


《Effective C++》条款02指出:你可以获得宏带来的效率以及一般函数的所有可预料行为和类型安全性(type safety)——只要你写出template inline函数。
那么,有哪些template inline值得我们在实际工程中运用呢?
希望借集体的力量,能收集到大量实用的内联函数模板函数。
这是目前我暂时能想到的,抛砖引玉,请大家不吝赐教!

 
 
  1. C/C++ code 
  2. templateinline void SafeDelete(T*& p) 
  3. delete p; p = 0; } 
  4. templateinline void SafeDeleteArray(T*& p) 
  5. delete[] p; p = 0; } 
  6. template inline void UnusedVar(const T&) 
  7. {} 
  8. template inline size_t CountOf(const T& array) 
  9. { UnusedVar(array); return sizeof(array) / sizeof(array[0]); } 
  10. template inline const T& Max(const T& a, const T& b) 
  11. return a > b ? a : b; } 
  12. template inline const T& Min(const T& a, const T& b) 
  13. return a < b ? a : b; } 
  14. template inline void Swap(T& a, T& b) 
  15. { T t(a); a = b; b = t; } 
 
C/C++ code
//按内存方式强制类型转换,如将type (CLS::*pf)(type par)强制转换为void *: void 

 
 
  1. *p = GetCast(pf); 
  2. template 
  3. DEST GetCast(const SRC& src) 
  4. union 
  5. SRC src; 
  6. DEST dest; 
  7. }myunion = {src}; 
  8. return myunion.dest; 
 
发个我常用的单键类吧.
 
 
  1. C/C++ code 
  2. #ifndef __SINGLE_H__ 
  3. #define __SINGLE_H__ 
  4. #include  
  5. template class TSingle 
  6. public
  7. inline static T* Create() 
  8. if (!spT_) 
  9. spT_ = new(&sMemory_) T; 
  10. return spT_; 
  11. inline static T* Instance() 
  12. return spT_; 
  13. inline static void Destroy() 
  14. if (spT_) 
  15. spT_->~T(); 
  16. spT_ = 0; 
  17. private
  18. typedef union 
  19. char t_[sizeof(T)]; 
  20. short int shortInt_; 
  21. int int_; 
  22. long int longInt_; 
  23. float float_; 
  24. double double_; 
  25. long double longDouble_; 
  26. }UNMaxAlign; 
  27. static UNMaxAlign sMemory_; 
  28. static T* spT_; 
  29. }; 
  30. template typename 
  31. TSingle::UNMaxAlign TSingle::sMemory_; 
  32. template typename 
  33. T* TSingle::spT_ = 0; 
  34. #endif // __SINGLE_H__ 
 
 
C/C++ code
//注意:请不要用于指针之间的转换,特别是BSTR
 
 
 
  1. template 
  2. lType TranslateType(rType& rValue, IsDiff& isDiff) 
  3. stringstream is; 
  4. lType lValue; 
  5. is > lValue; 
  6. return lValue; 
  7. }; 
 
 
C/C++ code
 
 
 
  1. template  
  2. struct is_a_ptr  
  3. {  
  4. enum{ yes = FALSE };  
  5. };  
  6. template  
  7. struct is_a_ptr  
  8. {  
  9. enum{ yes = TRUE };  
  10. };  
  11. template  
  12. inline void UnusedVar(const T&) 
  13. if(is_a_ptr::yes) 
  14. throw std:: invalid_argument("Not a array!"); 
  15. template  
  16. inline size_t CountOf(const T& array) 
  17. UnusedVar(array); 
  18. return sizeof(array) / sizeof(array[0]); 
  19. /// 
  20. int main() 
  21. try 
  22. int a[10]; 
  23. int b[11][12]; 
  24. int *c; 
  25. cout 
 
CountOf那个函数模板是不是这么来实现更好点呢?
 
 
 
  1. C/C++ code 
  2. template 
  3. inline size_t CountOf(const T(&)[N]) 
  4. return N; 
 
C/C++ code
//判断参数类型是否是无符号类型,当然只限于有序类型
 
 
 
  1. template 
  2. inline bool IsUnsigned(T) 
  3. return ((T)-1 > 0); 
 
C/C++ code
 
 
 
  1. template 
  2. inline size_t CountOf(const T(&)[N]) 
  3. return N; 
  4. //直接这么用就可以了: 
  5. int a[9]; 
  6. char aa[10][11]; 
  7. CountOf(a);//返回9 
  8. CountOf(aa);//返回10 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值