C++ 编写泛型编程
来源:互联网 发布:时间序列数据例子十年 编辑:程序博客网 时间:2024/06/15 23:27
16.1.6. Writing Generic Programs
Writing Type-Independent Code
The art of writing good generic code is beyond the scope of this language primer. However, there is one overall guideline that is worth noting.
Good Practices : When writing template code, it is useful to keep the number of requirements placed on the argument types as small as possible.
Simple though it is, our comparefunction illustrates two important principles for writing generic
code:
The parameters to the template are constreferences.
The tests in the body use only <comparisons.
By making the parameters constreferences, we allow types that do not allow copying. Most typesincluding the built-in types and, except for the IO types, all the library types we've useddo allow copying. However, there can be class types that do not allow copying. By making our parameters constreferences, we ensure that such types can be used with our compare function. Moreover, if compareis called with large objects, then this design will also make the function run faster.
Some readers might think it would be more natural for the comparisons to be done using both the <and >operators:
// expected comparison
if (v1 < v2) return -1;
if (v1 > v2) return 1;
return 0;
However, by writing the code as
// expected comparison
if (v1 < v2) return -1;
if (v2 < v1) return 1; // equivalent to v1 > v2
return 0;
we reduce the requirements on types that can be used with our comparefunction. Those types
must support <, but they need not also support >.
- C++ 编写泛型编程
- Linux C编程Makefile编写初步
- C语言编程函数编写一般原则
- Linux C编程Makefile编写初步-转
- 《Essential C++》泛型编程
- c++stl泛型编程
- C++i泛型编程
- c泛型编程--栈
- 编程精粹 --Microsoft编写优质无错C程序秘诀
- c编程中关于字符串的函数编写
- 编程精粹 Microsoft编写优质无错C程序秘诀
- linux c 编写php扩展 编程环境设定
- linux下c/c++编程中的makefile文件编写
- 【混合编程实例】C/C++调用FORTRAN编写的DLL
- C和C++混合编程的Makefile的编写!
- matlab/C混合编程--mex文件的编写、编译、调用
- 编写Hello world网络编程(linux----C)
- Matab与C混合编程 C_MEX函数编写
- RFID与物联网的关系
- 十三、Linux系统编程-进程(六)进程组、会话期、守护进程
- Uva 11426 GCD - Extreme (II)(基本数论)
- matlab求解线性规划问题
- hough transform
- C++ 编写泛型编程
- Web攻防之XSS,CSRF,SQL注入
- p51 1、 3 、4、6
- json中的date数据在js中的显示
- iBatis简单入门
- Ubuuntu 环境变量
- Matlab学习笔记:读取txt的数据
- 创建angular js指令
- Linux网络编程——原始套接字能干什么?