Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2001-07-20 05:50:03


> 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).

Being ignorant of what the standard says here, and a big fan of forward
declaration, a while ago I actually tried to create forward declarations for
parts of the standard library. As you say, it is not portable. std::string, for
example, turns out to have forward declarations in some standard libraries, but
not in others. Of course, these kind of issues can be easily solved by
configuration.

The big problem I had was the handling of default template parameters.
Basically, I could not specify these again, so users of the forward declaration
library were forced to spell out all the template parameters. Consider the
following example:

//map_fwd.hpp
namespace std {

  template <class Key,
            class T,
            class Compare,
            class Alloc>
  class multimap;
}

//some client .hpp file
#include "map_fwd.hpp"

class map_tester {
public:
  map_tester();
private:
  //map pointer can use a forward declaration
  std::multimap<int,int, std::less<int>, std::allocator<int> >* mmapPtr_;
};

This works, but it is pretty ugly....

Jeff


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