What is the <iosfwd> header?

wp2 picture wp2 · Nov 29, 2010 · Viewed 18k times · Source

What's the <iosfwd> header used for? Why is it necessary?

Any example?

Answer

Marcelo Cantos picture Marcelo Cantos · Nov 29, 2010

It's so you can declare, in your own headers, methods that rely on the declarations of iostream types without having to #include the iostream headers themselves, which are large, complex and slow to compile against.

Here's a simple example:

// foo.h
#include <iosfwd>

void sucker(std::iostream& is);

 

// foo.cc
#include <iostream>

void sucker(std::iostream& is) {
    is >> somevar;
}