Quote:
Originally Posted by Nylex
(Post 2947563)
It compiles and runs fine without "#include <string>" (on my machine with gcc 4.1.2, anyway).
|
First and foremost, even if it does, it’s wrong. There is nothing special about strings that allows their use without their definition, and their is nothing special about namespace std (in fact, namespace std does not exist without the standard library headers). See §17.4.2.1.
Second, are you sure? Whose gcc do you have? For me, it throws an error with (and most of these are near-vanilla) g++-3.4.6, g++-4.1.2, and g++-4.2.2.
Also, there is a separate issue (probably) not present in the original post. When you #include certain standard headers, they end up including others. This behavior is expressly permitted by the standard (§17.4.4.1). The only problem is it is not specified what headers include other headers except that a header must include another header if it contains a needed definition. So free rein is given to implementors on what headers can include what other headers. For example, with most g++ implementations, if you #include <iostream>, you have implicitly #included <string> as well. Except for the unlikely event that your <vector> header #includes <string>, I don’t see how your implementation successfully compiles the original program. This gets to limit the portability of compiling a program, since some implementors chose differing headers to be included by other headers. As such, the next version of g++ (4.3 branch) will strive to eliminate all unnecessary headers inclusion of other headers. If that happens, a program which successfully compiles with g++ will be guaranteed (at least in that respect) to compile on any other conforming compiler.
|