Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward Declarations #89

Open
jbandela opened this issue Oct 4, 2018 · 1 comment
Open

Forward Declarations #89

jbandela opened this issue Oct 4, 2018 · 1 comment

Comments

@jbandela
Copy link

jbandela commented Oct 4, 2018

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.

@sbowman-mitre
Copy link

I think these are great points. I'm not as learned as some of you, but I subjectively respond negatively to forward declarations in code. I feels like dirty code that is masking circular references or something. Perhaps a little more substantially, the other c++ guide I like is Google's publication and they also suggest that forward declarations are to be avoided.

https://google.github.io/styleguide/cppguide.html#Forward_Declarations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants