Offirmo Offirmo - 2 months ago 14
C++ Question

What are the dangers of forward declarations?

I just had an interview. I was asked what is a "forward declaration". I was then asked if they were dangers associated with forward declarations.

I could not answer to the second question. A search on the net didn't show up any interesting result.

So, do someone know any dangers associated with the use of forward declarations ?


Forward declaration is the symptom of C++ missing modules (going to be fixed in C++17?) and using headers inclusion, if C++ had modules there were no need at all for forward declarations.

A forward declaration is not less than a "contract", by using it you actually promise that you will provide the implementation of something (after in the same source file, or by linking a binary later).

The cons of that is that you actually have to follow your contract, not much very an issue because if you don't follow your contract the compiler will somehow complain early, but in some languages code just get executed without the need to "promise its own existence" (speaking of dynamically typed languages)