views:

271

answers:

4

Iostream, when all of the files it includes, the files that those include, and so on and so forth, adds up to about 3000 lines.

Consider the hello world program, which needs no more functionality than to print something to the screen:

#include <iostream> //+3000 lines right there.
int main()
{
    std::cout << "Hello, World!";
    return 0;
}

this should be a very simple piece of code, but iostream adds 3000+ lines to a marginal piece of code. So, are these 3000+ lines of code really needed to simply display a single line to the screen, and if not, do they create a less efficient program than if I simply copied the relevant lines into the code?

+1  A: 

It makes the compile slow (but that can be mitigated with precompiled headers etc), however any decent linker should remove the stuff that's not needed.

Preet Sangha
A: 

The compiler removed everything that is not needed during the link step. Be confident on the compiler there is no need for a manual cleaning! Regarding performance if you have many big headers you use in many cpp files, consider using precompiled headers that'll boost up compile time performance a lot (the included headers are pre-parsed and re-used).

jdehaan
A: 

in C++, functions which are not called in your code don't get compiled.

Andrew Keith
Including <iostream> adds the header to "your code", i.e. the compiler has to parse through all the templates, whether they are used or not.
DevSolar
Yeah, but they're non-instantiated templates, which means there's no code generated etc.
MSalters
This is blatantly false. Maybe you mean "template functions"?
Tom
+3  A: 

If you worry about the size of <iostream> when all you want is print a line of text, try <cstdio> and std::puts().

(Seriously, why do people use printf() or cout when the much simpler and quicker puts() fits the bill perfectly? It even appends an appropriate line feed automatically...)

In a serious application, the size and compile time of <iostream> won't be significant. (Plus, as others already noted, the linker won't link in what isn't used.)

Edit: I just realized I didn't really answer the question. No, not all the 3000 lines are really required to print the line of code, but you'll find it next to impossible to find "the few lines" required to produce your line of output, as I/O library source tends to be heavily interdependent. And aside from increasing compile times a bit, they don't harm - your code does not get any less efficient as the "fluff" is dropped at linker stage.

DevSolar
iostreams is indeed rather heavy-weight for "hello world". However, it does add something called type safety which is valuable in other, less trivial contexts (try using <cstdio> in templated contexts!)
UncleBens
Agreed. But the OP explicitly referred to the trivial example, and using printf() / cout for one-line text-only output is a pet peeve of mine. ;-)
DevSolar