减少编译时依赖的意义和一些惯用法
来源:互联网 发布:金融软件开发商 编辑:程序博客网 时间:2024/05/13 23:28
原文参见:
GotW #7a Solution: Minimizing Compile-Time Dependencies, Part 12013-08-19 by Herb Sutter
http://herbsutter.com/2013/08/19/gotw-7a-solution-minimizing-compile-time-dependencies-part-1/
GotW #7a Solution: Minimizing Compile-Time Dependencies, Part 1
2013-08-19 by Herb Sutter
减少编译时依赖的意义和一些惯用法
1. For a function or a class, what is the difference between a forward declaration and a definition?
A forward declaration of a (possibly templated) function or class simply introduces a name。
A class definition has a body and lets you know the class’s size and know the names and types of its members
前置声明(forward declaration):只是引入了一个名字符号;
包含引用类的定义头文件:引入一个类型的完整定义(definition),如:需要了解有哪些构造函数,成员函数,类的大小等。
class widget; // "widget" names a class
widget* p; // ok: allocates sizeof(*) space typed as widget*
widget w; // error: wait, what? how big is that? does it have a
// default constructor?
===================================
// x.h: original header
//
#include <iostream>
#include <ostream>
#include <list>
// None of A, B, C, D or E are templates.
// Only A and C have virtual functions.
#include "a.h" // class A
#include "b.h" // class B
#include "c.h" // class C
#include "d.h" // class D
#include "e.h" // class E
class X : public A, private B {
public:
X( const C& );
B f( int, char* );
C f( int, C );
C& g( B );
E h( E );
virtual std::ostream& print( std::ostream& ) const;
private:
std::list<C> clist;
D d_;
};
==========================
std::ostream& operator<<( std::ostream& os, const X& x ) {
return x.print(os);
}
1. Remove iostream.
Guideline: Never #include unnecessary header files.
永远不要#include多余的头文件。
2. Replace ostream with iosfwd.
Parameter and return types only need to be forward-declared, so instead of the full definition of ostream we really only need its forward declaration.
Guideline: Prefer to #include <iosfwd> when a forward declaration of a stream will suffice.
优先使用 前置声明 代替 #include定义头文件。如,类型如果只用于函数参数,返回值类型,只需前置声明。
std::ostream& operator<<( std::ostream& os, const X& x ) {
return x.print(os);
}
This function mentions an ostream& as both a parameter and a return type, which most people know doesn’t require a definition. And it passes its ostream& parameter in turn as a parameter to another function, which many people don’t know doesn’t require a definition either—it’s the same as if it were a pointer, ostream*, discussed above. As long as that’s all we’re doing with the ostream&, there’s no need for a full ostream definition—we’re not really using an ostream itself at all, such as by calling functions on it, we’re only using a reference to type for which we only need to know the name. Of course, we would need the full definition if we tried to call any member functions, for example, but we’re not doing anything like that here.
当仅需一个类型的引用时,只需前置声明,因为只是需要知道类型的名字。但如果通过类型引用访问其成员,则需要完整定义。
Guideline: Never #include a header when a forward declaration will suffice.
使用前置声明只允许的声明是指针或引用的一个原因是只要这个声明没有执行需要了解类A的大小或者成员的操作就可以了,所以声明成指针或引用是没有执行需要了解类A的大小或者成员的操作的。
Phase 2: How can you limit dependencies on the internals of a class?
- 减少编译时依赖的意义和一些惯用法
- C++的前置声明与Exceptional C++中减少编译时依赖的意义和一些惯用法
- 减少编译时源文件之间的依赖
- 一些Python的惯用法和小技巧:Pythonic
- 减少文件之间的编译依赖
- 减少文件间的编译依赖
- 一些Ruby惯用法
- template method pattern 和一些C++惯用法
- maven的一些惯用Repository
- 一些惯用的编码手法
- 编译防火墙——C++的Pimpl惯用法解析
- 编译防火墙——C++的Pimpl惯用法解析
- [Qt5] 减少dll依赖和大小(特别是webkit的大小和依赖) - 一 水
- うわさ的一些惯用搭配
- Python的惯用法技巧和创建型设计模式
- 编写高质量的C++代码--01 减少文件之间的编译依赖
- extern "C"的惯用法
- extern "C"的惯用法
- hdu4720 三角形的最小圆覆盖
- Linux下使用GDAL进行开发(automake使用)
- char vchar nchar nvchar的区别
- java oracle timestamp
- 深入学习Django源码基础15 - views简要分析学习
- 减少编译时依赖的意义和一些惯用法
- 闪加查询、闪回版本查询、闪回事务查询
- 网络编程经验
- View 5部署之一:vSphere 5安装及基本设置
- 黑马程序员------面向对象(No.7)(内部类、异常概述、异常try-catch、异常声明throws、多异常处理)
- 测试过程中,如何改变python的工作目录,并引用新目录下的其他模块或文件
- HDU4720
- 次小生成树 POJ 1679
- Shoemaker's Problem