【本文链接】

http://www.cnblogs.com/hellogiser/p/cplusplus-traits.html

【分析】

什么是traits?其实它并不是一个新的概念,上个世纪90年代中期就已经被提出,只是到了这个世纪才在各个C++库中被广泛使用,而我也是在这个概念诞生十多年后才接触到它。C++之父Bjarne Stroustrup对traits有如下的描述:

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".

我不知道官方或一些书上是如何去解释traits的,我的理解是:
当函数,类或者一些封装的通用算法中的某些部分会因为数据类型不同而导致处理或逻辑不同(而我们又不希望因为数据类型的差异而修改算法本身的封装时),traits会是一种很好的解决方案。

【is_void is_pointer代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/9/22
*/

#include "stdafx.h"
#include "iostream"
using namespace std;

// generic template
template<typename T>
struct is_void_x
{
    // enum {value = false} ;
    static const bool value = false;
};

//specialization for void
template<>
struct is_void_x<void>
{
    static const bool value = true;
};

void test_is_void()
{
    cout << "is void...\n";
    cout << is_void_x<int>::value << endl;
    cout << is_void_x<void>::value << endl;
}

// generic template
template<typename T>
struct is_pointer_x
{
    static const bool value = false;
};

// partial specialization for pointer
template<typename T>
struct is_pointer_x<T *>
{
    static const bool value = true;
};

void test_is_pointer()
{
    cout << "is pointer...\n";
    cout << is_pointer_x<int>::value << endl;
    cout << is_pointer_x<int *>::value << endl;
}

int main()
{
    test_is_void();
    test_is_pointer();
}

再看一个例子。

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/9/22
*/

#include "stdafx.h"
#include "iostream"
using namespace std;

template<typename T>
class TypeTraits
{
public:
    typedef double ReturnType;
};

template<>
class TypeTraits<char>
{
public:
    typedef int ReturnType;
};

template<>
class TypeTraits<short>
{
public:
    typedef int ReturnType;
};

template<>
class TypeTraits<int>
{
public:
    typedef int ReturnType;
};

template<>
class TypeTraits<float>
{
public:
    typedef double ReturnType;
};

template<>
class TypeTraits<double>
{
public:
    typedef double ReturnType;
};

template <typename T, typename TypeTraits>
typename TypeTraits::ReturnType average_x(const T const *begin, const T const *end)
{
    TypeTraits::ReturnType total = TypeTraits::ReturnType();
    //T total = T();
;
    while (begin != end)
    {
        total += * begin;
        ++begin;
        ++count;
    }
    return total / count;
}

void test_average()
{
    };
    cout << average_x<) << endl; // 3

char str[] = "traits";
    cout << average_x<) << endl; // -17 ????  ===>110
}

int main()
{
    test_average();
}

【参考】

http://accu.org/index.php/journals/442

http://blog.csdn.net/my_business/article/details/7891687

http://www.cppblog.com/youxia/archive/2008/08/30/60443.html

http://stackoverflow.com/questions/3979766/how-do-traits-classes-work

http://en.cppreference.com/w/cpp/language/partial_specialization

C++ traits的更多相关文章

  1. 仿SGI STL的traits技法

    首先是iterator traits,这个是用来萃取迭代器的特性的 #ifndef _STL_ITERATOR_H_ #define _STL_ITERATOR_H_ #include <cst ...

  2. PHP的学习--Traits新特性

    在阅读yii2源码的时候接触到了trait,就学习了一下,写下博客记录一下. 自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 traits. Traits 是一种为类似 PHP 的 ...

  3. Effective C++ -----条款47:请使用traits classes表现类型信息

    Traits classes使得“类型相关信息”在编译期可用.它们以template和“templates特化”完成实现. 整合重载技术(overloading)后,traits classes有可能 ...

  4. traits的使用

    trait的作用是可以在任何地方使用trait中的方法. trait的定义与定义类相同,定义实例如下: trait tSoneTrait{ //定义一些属性 function someFunction ...

  5. STL源码--iterator和traits编程技法

    第一部分 iterator学习 STL iterators定义: 提供一种方法,使之能够依序巡访某个聚合物(容器)所含的各个元素,而又无需暴露该聚合物的内部表达方式. 任何iteartor都应该提供5 ...

  6. PHP系列之一traits的应用

    Traits 在PHP中实现在方法的重复使用:Traits与Class相似,但是它能够在Class中使用自己的方法而不用继承: Traits在Class中优先于原Class中的方法,引用PHP Doc ...

  7. STL源码分析《4》----Traits技术

    在 STL 源码中,到处可见 Traits 的身影,其实 Traits 不是一种语法,更确切地说是一种技术. STL库中,有一个函数叫做 advance, 用来将某个迭代器(具有指针行为的一种 cla ...

  8. C++设计新思维的traits和policy

    http://blog.csdn.net/zhoudaxia/article/details/4486487 这篇博客讲得挺清楚的,本来想自己写写看总结下的,不过看了下这个文章已经写得很清楚了,倒没有 ...

  9. STL源码分析读书笔记--第三章--迭代器(iterator)概念与traits编程技法

    1.准备知识 typename用法 用法1:等效于模板编程中的class 用法2:用于显式地告诉编译器接下来的名称是类型名,对于这个区分,下面的参考链接中说得好,如果编译器不知道 T::bar 是类型 ...

随机推荐

  1. poj3522 kruskal+枚举

    题目的意思是求构成生成树的边的最大边和最小边的差最小.枚举即可 #include<stdio.h> #include<string.h> #include<algorit ...

  2. Java基础-多线程

    介绍 操作系统能同时运行几个程序,每个独立运行的程序又称之为进程. 对于同一个程序,它又可以分成若干个独立的执行流,我们称之为线程.线程提供了多任务处理的能力 用进程和线程的观点来研究软件是当今普遍采 ...

  3. BIEE 维表

    (1)       在物理层给表创建别名(表——>新建对象——>别名) (1)       在业务层创建维度(表——>创建逻辑维——>基于级别层次的维) 钻取是维本身的功能 一 ...

  4. maven加载spring包

    <dependencies> <dependency> <groupId>org.springframework</groupId> <artif ...

  5. POJ1745Divisibility(01背包思想)

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11151   Accepted: 3993 Des ...

  6. User表格式

    "_id":基本是700多 "name":"xx01" "pwd":"123"

  7. AspectJ AOP学习基础

    一.切入点表达式 1.execution:匹配方法的执行 格式:execution(修饰符 返回值类型 包.类.方法(参数) throw 异常) 1.1修饰符,表示方法的修饰符,一般省略. 1.2返回 ...

  8. spring属性依赖注入

    一.构造方法方式注入 1.项目结构如下: 2.新建Customer类 package hjp.spring.attributeinject; public class Customer { priva ...

  9. javascript学习随笔(二)原型prototype

    JavaScript三类方法: 1.类方法:2.对象方法:3.原型方法;注意三者异同 例: function People(name){ this.name=name; //对象方法 this.Int ...

  10. P、NP、NPC、NP-Hard问题

    转自:http://www.matrix67.com/blog/archives/105 总结 P:能用多项式时间求解的问题NP:能用多项式时间验证的问题(但不知道能不能用多项式时间求解).存在不属于 ...