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的更多相关文章

  1. Forward reference vs. forward declaration

    Q:Im a bit confused. What is the difference between forward declaration and forward reference? Forwa ...

  2. forward declaration of class 错误

    在使用Qt的时候遇到这个错误,查了一下发现,是因为我没有正确的使用前置声明. #ifndef FIRSTPAGE_H #define FIRSTPAGE_H #include "ui_dia ...

  3. 解决QT:forward declaration of 'struct Ui::xxx';invalid use of incomplete struct "Ui::Widget" 等莫名奇异错误

    今天在进行QT Widget的UI设计时,改了下Widget的对象名,然后在多次成功编译执行后,执行清理,又一次构建,就出现了好多莫名奇异的错误: widget.h:12: 错误:forward de ...

  4. Receiver type ‘X’ for instance message is a forward declaration

    这往往是引用的问题. ARC要求完整的前向引用,也就是说在MRC时代可能仅仅须要在.h中申明@class就能够,可是在ARC中假设调用某个子类中未覆盖的父类中的方法的话.必须对父类.h引用,否则无法编 ...

  5. 宋宝华: Linux内核编程广泛使用的前向声明(Forward Declaration)

    本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:宋宝华 来源: 微信公众号linux阅码场(id: linuxdev) 前向声明 编程定律 先强调一点:在一切可 ...

  6. #include和前置声明(forward declaration)

    #include和前置声明(forward declaration) 1.    当不需要调用类的实现时,包括constructor,copy constructor,assignment opera ...

  7. Qt中forward declaration of struct Ui::xxx的解决

    每当你新键一个 QT设计界面, QT会自动生成yyy.ui文件,如 <?xml version="1.0" encoding="UTF-8"?> & ...

  8. 正确使用#include和前置声明(forward declaration)

    http://blog.csdn.net/SpriteLW/article/details/965702

  9. receiver type *** for instance message is a forward declaration

    转自:http://stackoverflow.com/questions/8815200/receiver-type-for-instance-message-is-a-forward-declar ...

随机推荐

  1. CKEditor编辑器的使用

    <div class="form-group" id="infolink-edier-div"> <label for="" ...

  2. if-else和while循环

    用户登陆验证: if-else 判断 #!/usr/bin/env python # -*-coding:utf-8 -*- import getpass passwd=' name='sunhao' ...

  3. Spring学习五(JDBC支持)

    Spring的jdbc支持 1配置db.properties,将有关JDBC的信息载入 2bean文件配置数据源,这里用e3p0作为数据源载入db.properties 3配置template的bea ...

  4. L246‘’

    Should English classes be compulsory at the elementary or primary school level in countries where it ...

  5. 七、在U-boot中让LCD显示图片

    1. 增加Nandflash读取代码 因为要显示图片,而图片明显是放在Nandflash中比较合适,因此需要有能够操作Nandflash的函数.在U-boot中已经有能操作Nandflash的函数了, ...

  6. 2017年3月1日09:45:39 css选择器,session数据取不到

    昨天碰到了一个问题,通过输入指定的url进行登录在服务端将url存在session中但是登陆之后因为页面提交的登录请求是ajax请求,在后端提取session时获取不当之前存的url,老司机说不是同一 ...

  7. 【Python】etree方法生成,解析xml

    #练习:另一种遍历xml文件的方式etree,xpathimport systry: import xml.etree.cElementTree as ET #前面带c的都是比较快的,效率高且不占内存 ...

  8. [转] [Elasticsearch] 数据建模 - 处理关联关系(1)

    [Elasticsearch] 数据建模 - 处理关联关系(1) 标签: 建模elasticsearch搜索搜索引擎 2015-08-16 23:55 6958人阅读 评论(0) 收藏 举报 分类: ...

  9. Python学习笔记第二十三周(Flask架构)

    目录: 一.变量引用 内容: 备注:PyCharm小技巧,comm+alt+l  自动修改格式,comm+alt+return  向上添加新行 一.变量引用 1.url生成 from flask im ...

  10. 【c++基础】从json文件提取数据

    前言 标注数据导出文件是json格式的,也就是python的dict格式,需要读取标注结果,可以使用c++或者python,本文使用c++实现的. JsonCpp简介 JsonCpp是一种轻量级的数据 ...