Site Tools


wiki:hpp-iosfwd

Table of Contents

<iosfwd>

<iosfwd> provides forward declarations of stream classes without including the full implementation. Use it in header files when you want to accept a stream parameter or return type without pulling in the heavy iostream headers.

It's a small optimization to reduce compile times.

Example

This example declares a function that takes an ostream parameter using only forward declarations, avoiding the overhead of including iostream.

// compile: g++ -std=c++11 -o iosfwdexample iosfwdexample.cpp
// run: ./iosfwdexample
// description: forward declaration of ostream in a header
 
#include <iosfwd>
#include <iostream>
 
void print_message(std::ostream& out);
 
void print_message(std::ostream& out) {
    out << "hello from iosfwd\n";
}
 
int main() {
    print_message(std::cout);
    return 0;
}
wiki/hpp-iosfwd.md · Last modified: by 127.0.0.1