------- Comment #1 from fang at csl dot cornell dot edu  2007-04-03 19:45 
-------
Funny you mention this, I've done exactly this in my own utility headers:
create forward-declaration-only headers of STL containers and algorithms.  

"STL/allocator_fwd.h":
namespace std {
template <class T>
class allocator;
}

"STL/list_fwd.h"
#include "STL/allocator_fwd.h"
namespace std {
template <class T, class A = allocator<T> >
class list;
}

etc...

I've even made typedef templates in some cases:
namespace std {
template <class T>
struct default_vector {
typedef vector<T, allocator<T> > type;
}
}

Another possible advantage of doing this would be to facilitate separate
template compilation of STL containers and their methods *without* relying on
the "extern template" extension.  (Has worked for me.)  I've never sought to
push this idea, as it might create a minor headache for co-developers who might
suddenly find that including a header that only uses a forward-declared
template class suddenly needs to include the full-definition header on the side
to be able to use it.  e.g.

#include <STL/list_fwd.h>

template <class T>
void
some_func(const std::list<T>& l) {
...
}

Uses of the header that don't instantiate some_func will compile fine, whereas
instantiations will require the rest of <list>.  I don't necessarily find this
a bad idea, but it can be a source of problems.  

FWIW, I use autoconf to detect and conditionally include headers that we're not
supposed to include directly, such as <bits/stringfwd.h>.  

#if HAVE_BITS_STRINGFWD_H
#include <bits/stringfwd.h>
#endif

Compilation speedups have been noticeable.  


-- 

fang at csl dot cornell dot edu changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fang at csl dot cornell dot
                   |                            |edu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31464

Reply via email to