I spend some time examining boost::
libraries architecture and was interested with the following fact:
In some parts of the libraries a yyy_fwd.hpp
idea is used pretty common (see boost/detail
or boost/flyweight
for examples).
These files obviously contain only forward declarations of some template-based classes and as far as I understand, could benefit in terms of compilation time.
Could someone point out in what cases do they help and should I use the same idea when designing my own templates?
Thank you.
Classes. In some object-oriented languages like C++ and Objective-C, it is sometimes necessary to forward-declare classes. This is done in situations when it is necessary to know that the name of the class is a type, but where it is unnecessary to know the structure.
Generally you would include forward declarations in a header file and then include that header file in the same way that iostream is included.
A forward declaration tells the compiler about the existence of an entity before actually defining the entity. Forward declarations can also be used with other entity in C++, such as functions, variables and user-defined types.
To write a forward declaration for a function, we use a function declaration statement (also called a function prototype). The function declaration consists of the function header (the function's return type, name, and parameter types), terminated with a semicolon. The function body is not included in the declaration.
Forward declarations are needed to reduce compile-time dependencies. For instance, when implementing Pimpl idiom.
One more case is that, for instance, boost::pool
* depends on windows.h
on Windows platform. When creating my interface I don't want to force the users of my class to include the system headers by using my interface.
*Ok, that's a bad example, because boost/poolfwd.hpp
still includes windows.h
, but I hope they'll fix this issue. And I hope you get the idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With