The dis/advantage of forward declaration
In our projects, in C++ head file, if reference to some classes (reference or pointer), instead of include header file directly, we recommend to use forward declaration. Obviously, it can take some advantage to you.
- Reduce the compile time
- Avoid namespace pollution
- Save unnecessary recompile (maybe it is a disadvantage)
But there are disadvantage (from google C++ style), so google guideline is “Avoid using forward declarations where possible. Just #include the headers you need.” Who is right? I don’t know but we did not meet any problem until now.
I copy the google guidelinebelow.
Avoid using forward declarations where possible. Just #include the headers you need.
A "forward declaration" is a declaration of a class, function, or template without an associated definition.
- Forward declarations can save compile time, as #includes force the compiler to open more files and process more input.
- Forward declarations can save on unnecessary recompilation. #includes can force your code to be recompiled more often, due to unrelated changes in the header.
- Forward declarations can hide a dependency, allowing user code to skip necessary recompilation when headers change.
- A forward declaration may be broken by subsequent changes to the library. Forward declarations of functions and templates can prevent the header owners from making otherwise-compatible changes to their APIs, such as widening a parameter type, adding a template parameter with a default value, or migrating to a new namespace.
- Forward declaring symbols from namespace std:: yields undefined behavior.
- It can be difficult to determine whether a forward declaration or a full #include is needed. Replacing an #include with a forward declaration can silently change the meaning of code:
// b.h:
struct B {};
struct D : B {};
// good_user.cc:
#include "b.h"
void f(B*);
void f(void*);
void test(D* x) { f(x); } // calls f(B*)
If the #include was replaced with forward decls for B and D, test() would call f(void*).
- Forward declaring multiple symbols from a header can be more verbose than simply #includeing the header.
- Structuring code to enable forward declarations (e.g. using pointer members instead of object members) can make the code slower and more complex.
- Try to avoid forward declarations of entities defined in another project.
- When using a function declared in a header file, always #include that header.
- When using a class template, prefer to #include its header file.
The dis/advantage of forward declaration的更多相关文章
- Forward reference vs. forward declaration
Q:Im a bit confused. What is the difference between forward declaration and forward reference? Forwa ...
- forward declaration of class 错误
在使用Qt的时候遇到这个错误,查了一下发现,是因为我没有正确的使用前置声明. #ifndef FIRSTPAGE_H #define FIRSTPAGE_H #include "ui_dia ...
- 解决QT:forward declaration of 'struct Ui::xxx';invalid use of incomplete struct "Ui::Widget" 等莫名奇异错误
今天在进行QT Widget的UI设计时,改了下Widget的对象名,然后在多次成功编译执行后,执行清理,又一次构建,就出现了好多莫名奇异的错误: widget.h:12: 错误:forward de ...
- Receiver type ‘X’ for instance message is a forward declaration
这往往是引用的问题. ARC要求完整的前向引用,也就是说在MRC时代可能仅仅须要在.h中申明@class就能够,可是在ARC中假设调用某个子类中未覆盖的父类中的方法的话.必须对父类.h引用,否则无法编 ...
- 宋宝华: Linux内核编程广泛使用的前向声明(Forward Declaration)
本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:宋宝华 来源: 微信公众号linux阅码场(id: linuxdev) 前向声明 编程定律 先强调一点:在一切可 ...
- #include和前置声明(forward declaration)
#include和前置声明(forward declaration) 1. 当不需要调用类的实现时,包括constructor,copy constructor,assignment opera ...
- Qt中forward declaration of struct Ui::xxx的解决
每当你新键一个 QT设计界面, QT会自动生成yyy.ui文件,如 <?xml version="1.0" encoding="UTF-8"?> & ...
- 正确使用#include和前置声明(forward declaration)
http://blog.csdn.net/SpriteLW/article/details/965702
- receiver type *** for instance message is a forward declaration
转自:http://stackoverflow.com/questions/8815200/receiver-type-for-instance-message-is-a-forward-declar ...
随机推荐
- Android : alsa-lib 移植
一.官网下载lib源码 网址:http://www.alsa-project.org/main/index.php/Download#alsa-lib 左击:Stable Release列表中的[1. ...
- Spring实现Ioc的多种方式--控制反转、依赖注入、xml配置的方式实现IoC、对象作用域
Spring实现Ioc的多种方式 一.IoC基础 1.1.概念: 1.IoC 控制反转(Inversion of Control) IoC是一种设计思想. 2.DI 依赖注入 依赖注入是实现IoC的一 ...
- MySQL存储过程错误No data - zero rows fetched, selected, or processed
原因:游标没有获取到任何内容! 解决方案 : 声明一个 continue handler declare continue handler for not found set V_NotFound = ...
- [转]linux shell 多线程实现
情景 shell脚本的执行效率虽高,但当任务量巨大时仍然需要较长的时间,尤其是需要执行一大批的命令时.因为默认情况下,shell脚本中的命令是串行执行的.如果这些命令相互之间是独立的,则可以使用“并发 ...
- 实力封装:Unity打包AssetBundle(四)
→→前情提要:窗口初现←← 让用户选择要打包的文件 时至今日,我们选择打包文件的方式依然是在Project面板或Hierarchy面板中用鼠标点选.现在既然有了窗口,我们自然希望可以将所有文件罗列在窗 ...
- SS报错的解决
SS报错 AttributeError: /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1: undefined symbol: 解决方案: EVP_CIPHER_ ...
- 性能测试-3.Fiddler进行弱网测试
fiddler模拟限速的原理(原文地址) 我们可以通过fiddler来模拟限速,因为fiddler本来就是个代理,它提供了客户端请求前和服务器响应前的回调接口,我们可以在这些接口里 面自定义一些逻辑. ...
- Java 经典面试题 —— 性能
1. 性能 String.StringBuffer 与 StringBuilder 两个字符串相加,str1+str2,相当于执行: StringBuilder strBuilder1 = new S ...
- centos7 安装mysql--python模块出现EnvironmentError: mysql_config not found和error: command 'gcc' failed with exit status 1
要想使python可以操作mysql 就需要MySQL-python驱动,它是python 操作mysql必不可少的模块. 下载地址:https://pypi.python.org/pypi/MySQ ...
- 2017青岛赛区网络赛 Smallest Minimum Cut 求最小割的最小割边数
先最大流跑一遍 在残存网络上把满流边容量+1 非满流边容量设为无穷大 在进行一次最大流即可 (这里的边都不包括建图时用于反悔的反向边) #include<cstdio> #include& ...