cpp-best-practices / cpp-best-practices/cppbestpractices

Forward Declarations

未关闭
#89 1 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
没有语言数据
星标
8.8k
派生
902
PR 合并指标
30 天内没有已合并 PR

描述

Regarding "Forward Declare When Possible", I think you should (almost) never forward declare for the following reasons:

1. It makes backward compatible refactorings more complicated and prone to breakage. This is the reason that the the C++ Standard Library and abseil forbid forward declarations. In C++ you can often refactor a function into a lambda or function object or into a template and keep backward compatibility with uses of the function. However, the signature will have changed.

2. It is dangerous. Forward declarations are an easy way to get hard to diagnose errors like ODR violations. In addition, if something changed in a header you were depending on, forward declarations postpone the error to link-time or to compile time.

3. It is a short term optimization. Hopefully modules in C++ will come soon and make this better. But even without modules, such techniques as pre-compiled headers are safer and work in more scenarios.

Based on these, I think better advice would be to avoid forward declarations.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。