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

Forward Declarations

Open
#89 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
8.8k
Forks
902
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the documentation section titled "Forward Declare When Possible" and read its surrounding guidance and any linked references. Update the recommendation to reflect the issue's concerns about forward declarations, then confirm the section is internally consistent and clearly states the preferred practice.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.