Re: which headers to #include - std::ostream std::iostream std::iosfwd
On Nov 12, 11:05 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
Suppose I have a program which uses 'ostream' type as the parameter
type for a function and 'cout' object for writing into the standard
output. Following is my understanding regarding, which headers to
#include for this scenario.
Since the program uses 'cout' object, I have to #include <iostream>
header for 'cout'. In the ISO/IEC-14882:2003 document, in page 608, in
section 27.3 - Standard iostream objects, the synopsis for the header
<iostream> mentions the following line(apart from other statements):
extern ostream cout;
From the above statement, since 'cout' is mentioned for <iostream>,
#including <iostream> is sufficient for using 'cout' object in my
program. Moreover, since 'ostream' is also mentioned in the same
statement, the forward declaraion of 'ostream' type should also be
known in the <iostream> header. This means that I need NOT #include
<iosfwd>. Is this
correct ? Since the class definition of the type 'ostream' is
needed(because the function parameter type is 'ostream&'), I HAVE to
#include <ostream> header instead of relying on the particular
compiler implementation #including the <ostream> inside <iostream>.
So, for the scenario mentioned in the beginning, I need ONLY to
#include
#include <iostream>
#include <ostream>
// ...
using namespace std;
I have NOT #included <iosfwd> header.
Is the above understanding of mine is correct ?
Correct me wherever I am wrong.
Kindly explain.
Thanks
V.Subramanian
by using a dubious logic, you reached the right conclusion.
I believe you need to read more about extern storage-class specifier
and forward declarations.
quick tips:
a) extern declaration of a non-nested type requires a declaration only
(not a definition) of that type e.g.
struct A; extern A a; //ok
b) references are similar to pointers in regard to forward
declarations.
#include <iosfwd>
std::ostream& print_out( std::ostream& ) const; //ok
c) in your own headers including <iosfwd> is _usually_ enough, even
inline operators can delegate to a print( stream& ) e.g.
#include <iosfwd>
struct A {
std::ostream& print_out( std::ostream& ) const; //ok
};
std::ostream& operator <<( std::ostream& os, const A & a ) {
return a.print_out( os ); //delegate to impl
}
but anyway check this out; refer to ISO/IEC 14882:2003(E):
22.2.8/5 example includes <iostream> but uses arithmetic extractor and
inserter;
however the same standard document doesn't guarantees in any way that
<iostream> header defines any extractor/inserter operators.
there are a couple of other examples exhibiting same assumption.
according to current C++ standard one must include both <iostream> and
<ostream> for something as simple as:
std::cout << "bye" << "\n";
note: I believe this is being worked with:
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#343
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#1178
hth,
gil