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 ...
随机推荐
- C/C++知识补充 (1)
● C++的圆括号运算符() 下列关于圆括号运算符的功能说法不正确的是(C) . A. 可用于强制类型转换 B 可用于类型构造 C 可用于类型声明 D 可用于函数调用 对大部分可重载的运算符来说,它既 ...
- 特殊权限set_uid
set_uid:该权限针对二进制可执行文件,使文件执行阶段具有文件所有者的权限.比如passwd这个命令就具有该权限.当普通用户执行passwd命令时,可以临时获得root权限,从而可以更改密码. 作 ...
- php优秀框架codeigniter学习系列——CodeIgniter.php概览
CodeIgniter.php是CI框架的核心文件.它在前端控制器index.php之后运行,加载各类基础组件,执行请求.文件执行完成后,这次请求也就结束了.所以,该文只能对CodeIgniter.p ...
- 微软Power BI 每月功能更新系列——5月Power BI 新功能学习
Power BI Desktop 5月份功能摘要 本月Power BI Desktop除了许多报表功能的更新,Power BI对条件格式进行了重大改进,可以对报表的任何字段(包括字符串和日期)进行条件 ...
- CPU 架构 —— ARM 架构
linux 系统查看 CPU 架构命令: $ arch armv7l $ uname -m armv7l # -m:--machine # 进一步查看处理器信息 $ cat /proc/cpuinfo ...
- Pytorch使用多GPU
在caffe中训练的时候如果使用多GPU则直接在运行程序的时候指定GPU的index即可,但是在Pytorch中则需要在声明模型之后,对声明的模型进行初始化,如: cnn = DataParallel ...
- python三大框架之一flask中cookie和session的相关操作
状态保持 Cookie cookie 是指某些网站为了 辨别 用户身份,进行会话跟踪而储存在用户本地的数据(通常会经过加密),复数形式是 coolies. cookie是由服务器端生成,发送给客户端 ...
- 配置apache-maven-3.6.0时所遇到的坑(二)
在命令行窗口中输入:mvn -v 或 直接 mvn 时出现如下问题: The JAVA_HOME environment variable is not defined corre ...
- Hive错误:Permission denied: user=anonymous, access=EXECUTE, inode=”/tmp”
由于Hive没有hdfs:/tmp目录的权限,赋权限即可: hdfs dfs -chmod -R 777 /tmp
- Washing Text Animation
https://www.youtube.com/watch?v=q0_koJLc0OgBlender Tutorial: Washing Text Animation 需要用到插件, 进入用户设置的插 ...