Boost logo

Boost :

From: Vesa Karvonen (vesa.karvonen_at_[hidden])
Date: 2001-07-20 04:51:56


From: "John Max Skaller" <skaller_at_[hidden]>
> Vesa Karvonen wrote:
>
> > The item 1.7. of the C++ Coding Guidelines mentions using forward
declarations
> > for avoiding compile-time dependencies. I strongly agree with using
forward
> > declarations for the purpose of avoiding compile-time dependencies. I
would
> > like to see the practice of having a forward declaration header per
library as
> > a requirement or at least a strong recommendation in Boost.
>
> Please report on the applicability of the technique to
> _templated_ libraries, esp. considering the current
> state of compiler template technology.

For traditional class template libraries, it works very well. The standard
library has <iosfwd>, which has proven very useful. Some, but not all, Boost
libraries have a forward declaration header.

I'm interested to hear if a library author has had problems with forward
declaration headers. Also, I would be interested to know if the use of forward
declaration headers has caused significant amounts of bugs for library
clients. Use of forward declarations can cause headaches with compilers that
do not warn about deleting pointers to incomplete types (see
http://www.boost.org/libs/utility/utility.htm#checked_delete).

The main reasons for explicitly requiring that the library (and not the 3rd
party clients) must implement the forward declaration header is that it is
difficult to create external forward declarations of templates and because
creating such external forward declarations makes the client code more
difficult to maintain in respect to the library. For example, creating forward
declarations of standard library components is not portable (see 17.4.3.1).

For template metaprogramming libraries, the forward declaration header acts as
a metafunction prototype header and is especially useful as formal
documentation.

For generative/PLA class template libraries, forward declarations work at
least internally. In particular, you can typically include just the ICCL
(Implementation Components ...) forward declarations. However the benefit from
forward declarations of generative/PLA class template libraries can be
difficult to achieve. I think that this area probably needs to be examined
more closely as there might be specific ways to construct generators so that
minimal amount of DSL class and metacode definitions is required. However, if
there is considerable benefit to be had from forward declarations of
generative/PLA class template libraries, then it should be preferable to have
the forward declarations as a part of the library.

For broken compilers, forward declarations can cause some problems, but in
most cases there are workarounds. In such cases, it is preferable to have the
workarounds implemented just once rather than per library client. At least on
MSVC 6.0, it is sometimes necessary to duplicate template default parameters
in both declarations and definitions.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk