C++的Traits

来源:互联网 发布:python 图像特征提取 编辑:程序博客网 时间:2024/06/07 05:14

1. Think of a trait as a small object whose main purpose is to carry information used by another object or algorithm to determine "policy" or "implementation details"  ------ Bjarne Stroustrup


2. The traits class is used in template code to reflect properties (traits) of the actual template argument.


  1. template <typename T>  
  2. struct TraitsHelper {  
  3.      typedef T ret_type;  
  4.      typedef T par_type;  
  5. };  
  6. template <>  
  7. struct TraitsHelper<int> {  
  8.      typedef int ret_type;  
  9.      typedef int par_type;  
  10. };  
  11. template <>  
  12. struct TraitsHelper<float> {  
  13.      typedef float ret_type;  
  14.      typedef int par_type;  
  15. };  

  1. template <typename T>  
  2. class Test {  
  3. public:  
  4.      TraitsHelper<T>::ret_type Compute(TraitsHelper<T>::par_type d);  
  5. private:  
  6.      T mData;  
  7. };  

0 0
原创粉丝点击