`
yaojingguo
  • 浏览: 208213 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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 quality

 

#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;
}
分享到:
评论

相关推荐

    c++_traits.pdf

    ### C++ Traits:类型属性封装与偏特化 在C++中,`Traits`类是一种非常重要的设计模式,它主要用于封装类型的各种属性。通过这种方式,我们可以更方便地处理不同类型的数据结构,实现代码的复用,并优化性能。本文...

    C++ STL Template Traits使用代码

    设计一个Measurement计量单位类型,满足如下要求, 1.当为距离单位,当构造米或者千米等不同距离单位的实例时,统一以米为基本单位,实例调用description函数返回单位...注意:代码实现中运用了模板中的traits技术。

    C++ for quantitative finance

    **标题**:“C++ for quantitative finance” **描述**:“用C++语言入门量化编程” 本章节旨在介绍如何利用C++这一强大的编程语言进行量化金融分析。通过本书的学习,读者将能够掌握C++在量化交易中的核心应用...

    C++ Templates The Complete Guide, 2nd Edition

    Now extensively updated for the C++11, C++14, and C++17 standards, this new edition presents state-of-the-art techniques for a wider spectrum of applications. The authors provide authoritative ...

    C++标准(Standard for Programming Language C++)

    ### C++标准(Standard for Programming Language C++) #### 概述 C++作为一种广泛使用的编程语言,其标准定义了语言的特性和库的功能,确保不同实现之间的一致性与兼容性。本文档提供了C++标准的概述,尤其针对...

    The C++ Standard Library 2nd Edition(高清)pdf

    The Best-Selling Programmer Resource–Now Updated for C++11 The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is...

    《C++现代化程序设计》code

    《C++现代化程序设计》是一本深入探讨C++现代编程技术的书籍,其"real_sample_code"压缩包包含了书中各个章节的实际示例代码。这些代码旨在帮助读者理解并实践C++的最新特性和最佳实践,从而提升编程技能。在本文中...

    C++ Templates The Complete Guide, 2nd Edition epub

    Now extensively updated for the C++11, C++14, and C++17 standards, this new edition presents state-of-the-art techniques for a wider spectrum of applications. The authors provide authoritative ...

    C++ Templates The Complete Guide 2nd Edition

    Now extensively updated for the C++11, C++14, and C++17 standards, this new edition presents state-of-the-art techniques for a wider spectrum of applications. The authors provide authoritative ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...

    Mastering the C++17 .pdf

    C++11 practically doubled the size of the standard library, adding such headers as &lt;tuple&gt; , &lt;type_traits&gt; , and &lt;regex&gt; . C++17 doubles the library again, with additions such as &lt;optional&gt; , &lt;any&gt; , ...

    The.C++.Standard.Library.A.Tutorial.and.Reference.2nd.Edition

    Now Updated for C++11 The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full ...

    c++ 深入探秘 之 c++ viewer-1

    了解Traits可以帮助开发者更好地掌握C++模板编程,并应用于泛型编程实践中。 - **C++常用小技巧**:介绍了一系列在实际开发过程中经常用到的小技巧,例如如何更有效地管理内存、优化性能等,这些技巧对于提高开发...

    Techniques for Scientific C++ - Todd Veldhuizen

    特性(Traits)技术是C++中一种提供类型信息的模板编程技术。作者给出了一个计算平均值的特性例子,展示了特性如何通过类型推导和模板编程来提高代码的复用性和表达力。 表达式模板是本书中介绍的另一个重要概念。...

    c++20标准手册.rar

    C++20通过`std::coroutine_traits`和`co_await`等关键字支持协程。 4. **范围基础的for循环(Range-based for loop)增强**:现在范围基础的for循环可以用于遍历`std::map`和`std::set`等关联容器的键或值,使得...

    C++11-14教程.pdf

    type_traits库中的decltype可以用于查询表达式的类型,而初始化列表则用于简化复杂类型的初始化。模板编程也得到了加强,包括外部模板、类型别名模板、默认模板参数和变长参数模板等特性。面向对象编程在C++11中得到...

    C++ Templates 第二版

    C++ Templates - The Complete Guide, 2nd Edition by David Vandevoorde, Nicolai M....Explaining all type traits of the C++ Standard Library in depth On 822 pages in 33 Chapters (12 Chapters are brand new)

    C++ Templates The Complete Guide (2nd Edition)

    C++ Templates - The Complete Guide, 2nd Edition by David Vandevoorde, Nicolai M...-Explaining all type traits of the C++ Standard Library in depth -On 822 pages in 33 Chapters (12 Chapters are brand new)

    C++ lua Kaguya 应用

    在IT行业中,C++是一种强大的系统级编程语言,而Lua则是一种轻量级的脚本语言,常用于游戏开发、嵌入式系统以及各种应用程序的扩展。Kaguya是C++的一个lua绑定库,它使得C++程序可以方便地集成lua代码,实现两者之间...

Global site tag (gtag.js) - Google Analytics