您的位置:首页 > 编程语言 > C语言/C++

C++的Traits

2015-11-12 17:44 405 查看
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.

template <typename T>  

struct TraitsHelper {  

     typedef T ret_type;  

     typedef T par_type;  

};  

template <>  

struct TraitsHelper<int> {  

     typedef int ret_type;  

     typedef int par_type;  

};  

template <>  

struct TraitsHelper<float> {  

     typedef float ret_type;  

     typedef int par_type;  

};  

template <typename T>  

class Test {  

public:  

     TraitsHelper<T>::ret_type Compute(TraitsHelper<T>::par_type d);  

private:  

     T mData;  

};  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ Traits