What is the <iosfwd> header?
Asked Answered
A

3

102

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

Any example?

Arran answered 29/11, 2010 at 3:47 Comment(0)
E
101

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;
}
Exothermic answered 29/11, 2010 at 3:54 Comment(5)
Can you explain in detail how it forwards references?Arran
@wp2: It just declares the types without defining them. Why not have a peek inside it yourself? It's quite small.Exothermic
What's the benifit to #include <iostream> in foo.cc instead of doing it directly in foo.h?Arran
@wp2: Yes, but only in the .cpp file that needs the declaration, rather than in every single .cpp file that uses foo.h.Exothermic
Does iosfwd has anything but beyond namespace std { class istream; cass ostream; } ?Kiddush
F
55

As @Marcelo Cantos mentioned, it's so you can include the declaration of iostream classes and functions without including the full definitions. In C and C++, a declaration is a statement that says "here is the name of something (a function/class/etc.), but I'm not going to tell you anything more about it besides its name". For a function, that means the name of the function, but not the body containing the function's code. For a class, that means the name of the class but not any of the class's member variables or methods.

Conversely, a definition is the full definition: the function body, the class members, etc.

Oftentimes you only need the declaration of something to use—in the case of a function, you don't need to know what the function's body looks like in order to call it (except in the case of templated or inlined functions). Likewise, with a class, you don't need to know what members the class has if all you're doing is passing around pointers or references to instances of that class. But as soon as you need to access a member variable or call a class method, then you do need the full definition.

By only including the declarations instead of the definitions, the total amount of code that the compiler has to process is much, much less, and hence compilation will proceed much more quickly.

To give you an idea of how much code is being processed, here's how much code is contained in my local implementation:

# The following commands create a source file that includes a single header
# file (on stdout), preprocess it with g++ -E, and then count how many lines
# are in the resulting preprocessed output
$ echo '#include <iosfwd>' | g++ -E -xc++ - | wc
    2598    6534   57875
$ echo '#include <iostream>' | g++ -E -xc++ - | wc
   25631   59613  631998

A file that includes <iosfwd>, the compiler has to process 2598 lines of code from various header files, whereas a file that includes <iostream> has to process a whopping 25631 lines of code. That's before compiling the actual code you care about in your source file!

Freitag answered 29/11, 2010 at 4:12 Comment(1)
how following command works, $ echo '#include <iosfwd>' | g++ -E -xc++ - | wc I tried to run following command but it show some error $ echo '#include <QtGlobal>' | g++ -E -xc++ - | wc Where I was wrong ?Morin
G
26

Basically when you use <iosfwd> is because you want to eliminate compile-time Dependencies.

You use <iosfwd> instead of the traditional stream headers ( <iostream> and friends ) so that you can avoid including the definition of the whole streaming stuff. With <iosfwd> you are only making a forward declaration of all the streaming stuff.

I found this link particulary useful: http://www.gotw.ca/gotw/007.htm

Gabbi answered 17/7, 2013 at 8:33 Comment(3)
In which way is this more insightful than the two already existing and over 2 years old answers.Keramic
I was under the impression that this is more of a build optimization: it takes a lot less time to compile <iosfwd> than <iostream>. Or is that application the main thrust of what you were saying?Baccalaureate
@ChristianRau, To help you & googlers out with the answer: for one (among apparently a at least a dozen others) I really did appreciate the brevity of this one. I didn't need any of the fluff of the others, just the magic word "forward declaration", which, as you probably missed, appeared nowhere else on this page (except for the question tag that was added after this answer).Teriann

© 2022 - 2024 — McMap. All rights reserved.