yaojingguo 发表于 2013-1-30 20:49:31

Example code for C++ traits

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 qualityhttp://www.agoit.com/images/smiles/icon_smile.gif
 
#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;} 
#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;}  
#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;} #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 eof() {    return EOF;}void customize() {}};template <>struct ios_char_traits<wchar_t> {typedef wchar_t char_type;typedef wint_t int_type;static inline int_type eof() {    return WEOF;}void customize() {}};template<class charT, class traits = ios_char_traits<charT> >class my_basic_streambuf {public:    typedef ios_char_traits<charT> traits_type;    typedef typename traits_type::int_type int_type;    my_basic_streambuf(const traits& b = traits())      : _traits(b) {    }    int_type eof() {      return _traits.eof();    }    int_type sgetc() {      return traits_type::eof();    }    int sgetn(charT*, int N) {      return 1;    }private:    traits _traits;};int main(int argc, const char *argv[]) {my_basic_streambuf<char> char_buf;my_basic_streambuf<wchar_t> w_char_buf;ios_char_traits<char> m1;m1.customize();my_basic_streambuf<char> d(m1);return 0;}
页: [1]
查看完整版本: Example code for C++ traits