cpp-best-practices / cpp-best-practices/cppbestpractices
Forward Declarations
- Langage dominant
- Aucune donnée de langage
- Étoiles
- 8.8k
- Forks
- 902
- Métriques de merge des PR
- Aucune PR mergée en 30 j
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.
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Piste de recherche
Commencez par localiser la section de la documentation intitulée "Forward Declare When Possible" et lisez les indications qui l’entourent ainsi que toutes les références liées. Mettez à jour la recommandation pour refléter les préoccupations de l’issue concernant les forward declarations, puis vérifiez que la section est cohérente en elle-même et énonce clairement la pratique privilégiée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- cpp
- Domaine
- documentation
- Type d'issue
- Documentation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100