随笔分类 - stl & g.p
摘要:这是德问技术社区的一个问题,测试了一下,使用typeid等方法都无法正确区分二者,不讨论二者是不是有必要进行区分;下面给出一个方案吧,其实就是利用了Traits,模板的匹配规则。 1: template<typename T> class _ischararray_; 2: 3: template<typename T, int N> 4: class _ischararray_<T[N]> { public: static bool _ischararray(){return true;}}; 5: 6: template<typename T...
阅读全文
摘要:一篇介绍C++ Traits的文章:An introduction to C++ Traits,分享一下,后面会针对Traits做一点总结。It is not uncommon to see different pieces of code that have basically the same structure, but contain variation in the details. Ideally we would be able to reuse the structure, and factor out the variations. In 'C' this m
阅读全文
摘要:摘要: STL里有仿函数的概念,而在应用仿函数的时候,仿函数与仿函数之间的适配引出了约束器的概念。这一节主要叙述一下一元函数对象基类unary_function、二元函数对象基类binary_function,以及两个约束器binder1st与binder2nd,同时给出一个场景,分析实现原理。 1:template <class Arg, class Result> struct unary_function;2:template <class Arg1, class Arg2, class Result> struct binary_function; 3: tem
阅读全文
摘要:面向过程、面向对象、泛型编程(Generic Programming,简称GP)应该是三种重用的编程方法。传统的C++语言中,泛型编程思想仅仅体现于简单的模板技术。而之后引入的标准模板库STL(Standard Template Library)是泛型编程思想的实际体现和具体实现。1. 问题引入 面向过程的方法,可以将常用代码段封装在一个函数中,然后通过函数调用来达到目标代码重用的目的。 面向对象的方法,可以通过类的继承来实现(对象的目标)代码的重用。 如果需要编写一个可用于不同数据类型的算法,可以采用的方法有: 1). 面向过程的方法,对源代码进行复制和修改,生成不同数据类型版本的算法...
阅读全文
浙公网安备 33010602011771号