Take the 2-minute tour ×

The header <iostream> has a companion <iosfwd> which is sufficient if I only need pointers or references to streams. The latter is a common scenario when overloading operator<<.

Is there such a header for the STL containers? I want to define a couple of functions that take references to STL containers, and I do not want to include <vector>, <list>, <set> et. al. in their entirety just so I can have references to those types. Are there standard, boost or other solutions to this problem?

share|improve this question
The "work-around' for this is to use precompiled headers to include expensive headers like Boost and other template libs. Visual C++ has these, not sure about other compilers. If you do this, be conservative about also including work-in-progress headers, or you will find you have to a lot of frustrating full rebuilds. –  Steve Townsend Nov 17 '10 at 19:03
@up - That's right; also does have precompiled headers. But I'm not an user of those and actually I'm not sure about the compile-time performance yield of those as far as templates are concerned - from what I heard, the contents of a PCH with templates is not much different from the source code itself. :) Which sounds somewhat logical. –  Kos Nov 17 '10 at 19:17
@Kos - it's not intended that the contents be different, it's to reduce overall build time by not having to read and preprocess the full header files in every compilation unit that needs them. –  Steve Townsend Nov 17 '10 at 19:48
I thought about it, and decided that I should think of STL containers as basic types to the C++ system, and just as python includes [] in every file, I can afford doing that as well. The minuscule gain at compilation time just doesn't worth it. –  Elazar Leibovich Apr 5 '11 at 8:26

4 Answers 4

up vote 1 down vote accepted

No, unfortunately not. The standard only introduced <iosfwd> for backward compatibility to support code that was written for the old, non-templated IO streams library.

share|improve this answer

Is there such a header for the STL containers?

No, there isn't.

Are there standard, boost or other solutions to this problem?

Not that I know of.

You will have to include those headers.

share|improve this answer

Actually, why wouldn't it be possible to have a template class forward declaration?

If you desperately need a workaround, you could be able to go with something like this:

namespace std {

    template<typename T>
    class allocator;

    template<typename T, typename Alloc>
    class vector;

Add an include guard, save as "stdfwd.h" or something and here you go.

Then something like this works without including <vector>:

void print(std::vector<int, std::allocator<int> >& v);

Worked for me with GCC 4.5. I have no idea at the moment how to make a transparent workaround for the problem that you cannot have a default argument (also template argument) in declaration and in definition at the same time, which .. But it might be possible - maybe anyone could take over from here? :) I wouldn't use it in production code I think, but looks like an interesting excercise.

share|improve this answer
Unfortunately, it is illegal to add stuff to the std namespace. –  FredOverflow Nov 17 '10 at 19:55
Nose demons will come and arrest you –  John Dibling Nov 17 '10 at 20:18
That's not the only problem. Implementations are free to add template parameters to standard classes if they're optional. So your code is just not standard-complying. –  wilhelmtell Nov 17 '10 at 20:24
@FredOverflow not always. You can specialize templates from std::, although I suppose it's usually not a good idea. I've seen it done for the copy-and-swap idiom for example. –  wilhelmtell Nov 17 '10 at 20:27
"The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited." [C++11] –  boycy Sep 26 '13 at 15:24

With iosfwd it's possible to do because there are only two types the templates have to be instantiated for: char and wchar_t. So what you get in the end is forward declarations of instantiated concrete type. STL containers, on the other hand, are suppose to work with any type, so there is nothing explicit in STL headers.

share|improve this answer
<iosfwd> forward-declares the templates, not the instantiations. Forward-declaration of a template instantiation doesn't make sense as it isn't something you can declare in the first place; only new names can be 'declared'. Forward-declaration of the standard containers is indeed technically possible, it's just not required by the standard and not allowed in user code. For example, assuming that the vector template is exactly as specified, then the following would be a forward-declaration to it: namespace std { template <class,class> class vector; } –  boycy Sep 26 '13 at 15:31

Your Answer


