Skip to content

Forward Declarations #89

Open
Open

Description

@jbandela

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.

Activity

  1. sbowman-mitre commented on Oct 5, 2018

    @sbowman-mitre

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions