#include <string>?????

Can someone exlpain this to me.....
I can compile and run my program using string variables I declared
WITHOUT including the header file <string>.
Why didn't my compiler throw an error?????
which headers do u use?
it return error if <string> is not included, it means, it is included in another header file which you inclulded;)
Last edited on
The header file I have included is <iostream>.....
Could it be that <string> is included in it??????
....This is only my FIRST year in C++, But I do have a firm understanding
of Java though.....!!!!
<string> is included in <iostream>, that is why your compiler does not throw any error;)
I thought that std::string was available at any time, and that <string> contained some extra, not-always-useful string functions. I could be wrong.
No, not really. The string library is in fact included if you include the iostream library. But the iostream does not include the string library(not directly). Or in other words, the preprocessor statement "#include <string>" is not defined inside iostream but in it's inherited member, ios_base.

However, ios_base does not also directly include the string class. The preprocessor definitions of ios_base are:
1
2
3
4
/*ios_base.h*/
#include <bits/atomicity.h>
#include <bits/localefwd.h>
#include <bits/locale_classes.h> 


The header file locale_classes.h is the one that defines the string class.
1
2
3
4
5
6
/*locale_classes.h*/
#include <bits/localefwd.h>
#include <cstring>		// For strcmp.
#include <string>
#include <bits/atomicity.h>
#include <bits/gthr.h> 


So saying that the string library is included in iostream is partly correct. I would rather say that the string library is inherited by iostream from ios_base.

@Ganon11:
If you do not include the library(directly or indirectly) and declare a string class, it will throw an error.
@vince:
I think this seems to be implementation dependant. If the standard doesn't explicitly say anything about whether the <string> header is included in another header, you must include it when you use the string class. Otherwise your code isn't valid C++. Although it might still work, of course.
As far as the object-oriented paradigm of C++ is concerned, even if some header libraries are just implicitly included, it is still a valid C++ code because of inheritance.

I tested this myself passing most of the warning and error flags in gcc-g++ with ansi standard and it never gave any errors nor warning about using the string class using iostream without defining #include <string> in the preprocessor directives. So it is still valid C++ standard code.

But I always say and recommend to explicitly include all the library that you are using, regardless if it is a valid C++ standard procedure or not.
Ok, let me rephrase: Instead of questioning whether it's "valid C++", I question whether it "follows the specification of the standard library", and whether it's "portable C++".

The include files are inserted in the code by the preprocessor, even before the compiler sees it. There's no way the compiler can warn you about a missing include directive, even if you enable all warnings and standard compliance.

The only thing you have asserted is that somwhere in the GNU implementation of the library, the string header is included. You can't guarantee that the code will compile on MSVC++, or some future "C++ Compiler 20xx" etc..
But there's no missing library. That is the reason why I can assure that it will work on any compiler unless someone changed the iostream implementation.

Ok I guess it's my mistake that I skipped a lot of connections regarding inheritance. I studied the ios inheritance and here are the actual includes that the preprocessor checks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*locale_classes.h*/
#include <string> //If this is removed, then there's the issue regarding future compilers.

/*ios_base.h*/
#include <bits/locale_classes.h>

/*ios*/
#include <bits/ios_base.h>

/*ostream*/
#include <ios>

/*istream*/
#include <ostream>

/*iostream*/
#include <ostream>
#include <istream> 


It's all removing redundant includes. So, I'm sure you know this but, it's like saying I have all my includes here:
1
2
3
4
5
6
7
8
9
10
11
/*spec.h*/
#ifndef _SPEC_H
#define _SPEC_H
#include <iostream>
#include <vector>
#include <stack>
#include <list>
#include <cstring>
#include <string>
#include <cstdlib>
#endif 


Then I can include all those directives in my main program by simply including that header.
1
2
/*main.cpp*/
#include "spec.h" 
There is no single implementation of the iostream library. The GNU implementation is just one. It follows the specification of the C++ standard. There's also other platforms that have iostream and other header files that are written completely independant from the GNU files. The only thing that is more or less guaranteed to be the same is the specification they follow.

For instance: There may not even be a directory called "bits" when using MSVC++.
As far as I know, for iostream to follow C++ standards, it must inherit/include ios_base.h, then ios_base.h must include implementations inside locale_classes. I can not assure though that string is required to be included in locale_classes cause I never read it before (probably it is mentioned in the latest draft).

I guess even if the "bits" directory is not present in MSVC++, Microsoft must somehow still implement what locale_classes.h define to follow the standards. I do not know this myself cause I never used managed C++, I only used VC++ for compiling win32 api's written in C(and not C++).
All the files you are referring to are part of the GNU project.

If you have any reference to a C++ standard document mentioning anything about "locale_classes", please point me to it.

BTW: A header file that includes another header file has nothing to do with "inheritance".
BTW: With "MSVC++", I mean Microsoft Visual C++, not "Managed C++" which is something completely different that I have never tried.
I guess you misinterpret what I meant by inherited/included. I only mentioned that iostream inherits from ios_base, which is true. So to inherit from ios_base, iostream needs to include ios_base, hence I mentioned inherit/include. Sorry for the confusion, it's because many of the references found over the web refers ios_base as inherited by iostream.

If you look at www.open-std.org and look at the C++ standard go to Input/Output Library then look at ios_base some of its functions returns a locale which is (in GNU) implemented in locale_classes. Hence I mentioned, "ios_base.h must include implementations inside locale_classes", not ios_base must include a specific locale_classes.
This is what you wrote:
>> I would rather say that the string library is inherited by iostream from ios_base.

A header file is not a "library". A library may contain one or more functions and classes, and may be described in one or more header files.

If you inherit from a class defined in a header file that happens to include another header file, it's not refered to as "inheriting a library".

The C++ standard describes a header file called <locale>. It's not the same file as <locale_classes.h> found on your system. It is not required to include any other header files described in the standard. In fact, no other header file described in the standard is required to include <string>.

However, the issue that started this discussion was that you assumed that the contents of the header files you have on your system are the same on all other platforms. Different platforms doesn't only have different complilers, but they also have very different versions of all the header files in the standard library. You can not assume anything about other platforms by looking at what they contain on your system.
I wrote:
>> In fact, no other header file described in the standard is required to include <string>.

Correection: <sstream> is required to include <string>
I understand, thanks.

Forgive some of the misinformation as I have only learned programming on my own. I am not a programming student so my knowledge is not very vast. From what I learned, if a library is included in the base class's definition, if the derived class publicly inherits the base class, the derived class also includes the library. I might be wrong though as I have only learned this through web and some programmers but not from an actual student nor teacher.

locale_classes of GNU defines the class locale and the class locale of GNU follows C++ standard hence I said, whatever is inside the locale_classes.h (class locale) must be implemented. I don't think I said the actual file must be implemented. Again, forgive the misinterpretation.

Thanks for this interesting discussion.
Topic archived. No new replies allowed.