yys

Maya插件开发,(多多练习英文吧~)

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

文章出处:http://www.diybl.com/course/3_program/c++/cppjs/2007918/71936_3.html

 

今天看"modern c++ design"的时候发现自己竟然又把以前好不容易弄懂的Traits技术给忘记了,真是...又重新学习了一下,赶紧记下来。
Traits技术可以用来获得一个 类型 的相关信息的。 首先假如有以下一个泛型的迭代器类,其中类型参数 T 为迭代器所指向的类型:

template
<typename T>
class myIterator
{
 ...
};

当我们使用myIterator时,怎样才能获知它所指向的元素的类型呢?我们可以为这个类加入一个内嵌类型,像这样:
template <typename T>
class myIterator
{
      typedef  T value_type; 
...
};
这样当我们使用myIterator类型时,可以通过 myIterator::value_type来获得相应的myIterator所指向的类型。

现在我们来设计一个算法,使用这个信息。
template <typename T>
typename
myIterator<T>::value_type Foo(myIterator<T> i)
{
 ...
}
这里我们定义了一个函数Foo,它的返回为为  参数i 所指向的类型,也就是T,那么我们为什么还要兴师动众的使用那个value_type呢? 那是因为,当我们希望修改Foo函数,使它能够适应所有类型的迭代器时,我们可以这样写:
template <typename I> //这里的I可以是任意类型的迭代器
typename I::value_type Foo(I i)
{
 ...
}
现在,任意定义了 value_type内嵌类型的迭代器都可以做为Foo的参数了,并且Foo的返回值的类型将与相应迭代器所指的元素的类型一致。至此一切问题似乎都已解决,我们并没有使用任何特殊的技术。然而当考虑到以下情况时,新的问题便显现出来了:

原 生指针也完全可以做为迭代器来使用,然而我们显然没有办法为原生指针添加一个value_type的内嵌类型,如此一来我们的Foo()函数就不能适用原 生指针了,这不能不说是一大缺憾。那么有什么办法可以解决这个问题呢? 此时便是我们的主角:类型信息榨取机 Traits 登场的时候了

....drum roll......

我们可以不直接使用myIterator的value_type,而是通过另一个类来把这个信息提取出来:
template <typename T>
class Traits
{
      typedef typename T::value_type value_type;
};
这样,我们可以通过 Traits<myIterator>::value_type 来获得myIterator的value_type,于是我们把Foo函数改写成:
template <typename I> //这里的I可以是任意类型的迭代器
typename Traits<I>::value_type Foo(I i)
{
 ...
}
然而,即使这样,那个原生指针的问题仍然没有解决,因为Trait类一样没办法获得原生指针的相关信息。于是我们祭出C++的又一件利器--偏特化(partial specialization):
template <typename T>
class Traits<T*> //注意 这里针对原生指针进行了偏特化
{
      typedef typename T value_type;
};
通过上面这个 Traits的偏特化版本,我们陈述了这样一个事实:一个 T* 类型的指针所指向的元素的类型为 T。

如此一来,我们的 Foo函数就完全可以适用于原生指针了。比如:
int * p;
....
int i = Foo(p);
Traits会自动推导出 p 所指元素的类型为 int,从而Foo正确返回。

=====================================================================================

from http://yaojingguo.javaeye.com/blog/748191

http://www.cantrip.org/traits.html is a good explanation of C++ traits. But the example are not ready to be compiled. So I make all the example code 
pass compilation and post them here. To compile the code with g++, -std=c++0x option is needed. All the code is just to show how traits works.
Some implementations may be of a bad quality
 

代码
 

Cpp代码

1. #include <iostream>
2. #include <cstdio>
3. using namespace std;
4.
5. template <class charT>
6. struct ios_char_traits {};
7.
8. template <>
9. struct ios_char_traits<char> {
10. typedef char char_type;
11. typedef int int_type;
12. static inline int_type sgetn() {
13. return 1;
14. }
15. static inline int_type eof() {
16. return EOF;
17. }
18. };
19.
20. template <>
21. struct ios_char_traits<wchar_t> {
22. typedef wchar_t char_type;
23. typedef wint_t int_type;
24. static inline int_type sgetn() {
25. wint_t wt;
26. return wt;
27. }
28. static inline int_type eof() {
29. return WEOF;
30. }
31. };
32.
33. template<class charT>
34. class my_basic_streambuf {
35. public:
36. typedef ios_char_traits<charT> traits_type;
37. typedef typename traits_type::int_type int_type;
38.
39. int_type eof() {
40. return traits_type::eof();
41. }
42. int_type sgetc() {
43. return traits_type::sgetc();
44. }
45. int sgetn(charT*, int N) {
46. return 1;
47. }
48. };
49.
50. int main(int argc, const char *argv[]) {
51. my_basic_streambuf<char> char_buf;
52. my_basic_streambuf<wchar_t> w_char_buf;
53. return 0;
54. }
//---------------------------------------------------------------------

#include <iostream>
#include
<cstdio>
using namespace std;

template
<class charT>
struct ios_char_traits {};

template
<>
struct ios_char_traits<char> {
typedef
char char_type;
typedef
int int_type;
static inline int_type sgetn() {
return 1;
}
static inline int_type eof() {
return EOF;
}
};

template
<>
struct ios_char_traits<wchar_t> {
typedef wchar_t char_type;
typedef wint_t int_type;
static inline int_type sgetn() {
wint_t wt;
return wt;
}
static inline int_type eof() {
return WEOF;
}
};

template
<class charT>
class my_basic_streambuf {
public:
typedef ios_char_traits
<charT> traits_type;
typedef typename traits_type::int_type int_type;

int_type eof() {
return traits_type::eof();
}
int_type sgetc() {
return traits_type::sgetc();
}
int sgetn(charT*, int N) {
return 1;
}
};

int main(int argc, const char *argv[]) {
my_basic_streambuf
<char> char_buf;
my_basic_streambuf
<wchar_t> w_char_buf;
return 0;
}

//---------------------------------------------------------------------
Cpp代码

1. #include <cfloat>
2. #include <iostream>
3. using namespace std;
4.
5. template <class numT>
6. struct num_traits { };
7.
8. template<>
9. struct num_traits<float> {
10. typedef float float_type;
11. enum { max_exponent = FLT_MAX_EXP };
12. static inline float_type epsilon() { return FLT_EPSILON; }
13. };
14.
15. template<>
16. struct num_traits<double> {
17. typedef double float_type;
18. enum { max_exponent = DBL_MAX_EXP };
19. static inline float_type epsilon() { return DBL_EPSILON; }
20. };
21.
22. template <class numT>
23. class matrix {
24. public:
25. typedef numT num_type;
26. typedef num_traits<num_type> traits_type;
27. inline num_type epsilon() { return traits_type::epsilon(); }
28. };
29.
30. int main(int argc, const char *argv[]) {
31. matrix<float> f_m;
32. matrix<double> d_m;
33. cout << "float epsilon: " << f_m.epsilon() << endl;
34. cout << "double epsilon: " << d_m.epsilon() << endl;
35. return 0;
36. }
//---------------------------------------------------------------------

#include <cfloat>
#include
<iostream>
using namespace std;

template
<class numT>
struct num_traits { };

template
<>
struct num_traits<float> {
typedef
float float_type;
enum { max_exponent = FLT_MAX_EXP };
static inline float_type epsilon() { return FLT_EPSILON; }
};

template
<>
struct num_traits<double> {
typedef
double float_type;
enum { max_exponent = DBL_MAX_EXP };
static inline float_type epsilon() { return DBL_EPSILON; }
};

template
<class numT>
class matrix {
public:
typedef numT num_type;
typedef num_traits
<num_type> traits_type;
inline num_type epsilon() {
return traits_type::epsilon(); }
};

int main(int argc, const char *argv[]) {
matrix
<float> f_m;
matrix
<double> d_m;
cout
<< "float epsilon: " << f_m.epsilon() << endl;
cout
<< "double epsilon: " << d_m.epsilon() << endl;
return 0;
}
//---------------------------------------------------------------------

1. #include <iostream>
2. #include <cstring>
3. #include <cmath>
4. using namespace std;
5.
6. template <class T>
7. class CMP {
8. public:
9. static bool eq(T a, T b) { return a == b; }
10. static bool lt(T a, T b) { return a < b; }
11. };
12.
13. template<class charT>
14. class my_basic_string {
15. public:
16. my_basic_string(charT* ptr) : _ptr(ptr) {
17. }
18. charT* chars() {
19. return _ptr;
20. }
21. private:
22. charT* _ptr;
23. };
24.
25. template <class charT, class C = CMP<charT> >
26. int compare( my_basic_string<charT>& lhs,
27. my_basic_string<charT>& rhs) {
28. charT* c_ptr1 = lhs.chars();
29. charT* c_ptr2 = rhs.chars();
30. int len1 = strlen(c_ptr1);
31. int len2 = strlen(c_ptr2);
32. int min_len = min(len1, len2);
33. for (int i = 0; i < min_len; i++) {
34. char l = *(c_ptr1 + i);
35. char r = *(c_ptr2 + i);
36. if (C::lt(l, r))
37. return -1;
38. else if (C::eq(l, r))
39. continue;
40. else
41. return 1;
42. }
43. return len1 - len2;
44. }
45.
46. int main(int argc, char *argv[]) {
47. my_basic_string<char> m1("ybc");
48. my_basic_string<char> m2("axc");
49. int result = compare(m1, m2);
50. cout << "result: " << result << endl;
51. return 0;
52. }
//---------------------------------------------------------------------

#include <iostream>
#include
<cstring>
#include
<cmath>
using namespace std;

template
<class T>
class CMP {
public:
static bool eq(T a, T b) { return a == b; }
static bool lt(T a, T b) { return a < b; }
};

template
<class charT>
class my_basic_string {
public:
my_basic_string(charT
* ptr) : _ptr(ptr) {
}
charT
* chars() {
return _ptr;
}
private:
charT
* _ptr;
};

template
<class charT, class C = CMP<charT> >
int compare( my_basic_string<charT>& lhs,
my_basic_string
<charT>& rhs) {
charT
* c_ptr1 = lhs.chars();
charT
* c_ptr2 = rhs.chars();
int len1 = strlen(c_ptr1);
int len2 = strlen(c_ptr2);
int min_len = min(len1, len2);
for (int i = 0; i < min_len; i++) {
char l = *(c_ptr1 + i);
char r = *(c_ptr2 + i);
if (C::lt(l, r))
return -1;
else if (C::eq(l, r))
continue;
else
return 1;
}
return len1 - len2;
}

int main(int argc, char *argv[]) {
my_basic_string
<char> m1("ybc");
my_basic_string
<char> m2("axc");
int result = compare(m1, m2);
cout
<< "result: " << result << endl;
return 0;
}
//---------------------------------------------------------------------


1. #include <iostream>
2. #include <cstdio>
3. using namespace std;
4.
5. template <class charT>
6. struct ios_char_traits {};
7.
8. template <>
9. struct ios_char_traits<char> {
10. typedef char char_type;
11. typedef int int_type;
12. static inline int_type eof() {
13. return EOF;
14. }
15. void customize() {
16. }
17. };
18.
19. template <>
20. struct ios_char_traits<wchar_t> {
21. typedef wchar_t char_type;
22. typedef wint_t int_type;
23. static inline int_type eof() {
24. return WEOF;
25. }
26. void customize() {
27. }
28. };
29.
30. template<class charT, class traits = ios_char_traits<charT> >
31. class my_basic_streambuf {
32. public:
33. typedef ios_char_traits<charT> traits_type;
34. typedef typename traits_type::int_type int_type;
35.
36. my_basic_streambuf(const traits& b = traits())
37. : _traits(b) {
38. }
39.
40. int_type eof() {
41. return _traits.eof();
42. }
43.
44. int_type sgetc() {
45. return traits_type::eof();
46. }
47.
48. int sgetn(charT*, int N) {
49. return 1;
50. }
51. private:
52. traits _traits;
53. };
54.
55. int main(int argc, const char *argv[]) {
56. my_basic_streambuf<char> char_buf;
57. my_basic_streambuf<wchar_t> w_char_buf;
58.
59. ios_char_traits<char> m1;
60. m1.customize();
61. my_basic_streambuf<char> d(m1);
62. return 0;
63. }

 

posted on 2011-01-22 10:58  yys  阅读(462)  评论(0编辑  收藏  举报