I am reading from here : Can std::string be used without #include <string>?, <iostream>
is calling <string>
. But I do not see any includes of <string>
in Standard library header <iostream>
from c++ standard: https://en.cppreference.com/w/cpp/header/iostream. In <iostream>
is only included <ios>
, <streambuf>
, <istream>
and <ostream>
according to that documentation. So this code works (file foo.cpp
):
#include <iostream>
int main(){
std::cout << "enter srting\n";
std::string out; //std::string should not be included according to standard documentation
std::cin >> out;
std::cout << "test, " << out;
}
But I have to try to see dependecies generated from gcc
:
cc -H foo.cpp |& vim -
and the output is (I have regex out files with string
in it):
. /usr/include/c++/8/iostream
..... /usr/include/c++/8/bits/stringfwd.h
...... /usr/include/c++/8/string
....... /usr/include/c++/8/bits/basic_string.h
........ /usr/include/c++/8/ext/string_conversions.h
....... /usr/include/c++/8/bits/basic_string.tcc
So I can see that in the end, many "string" headers are indeed included
- (so should I trust that documentation, when the did not mentioned
<string>
header in "Includes" list of<iostream>
?).
Some of them are deeper on the #include stack
(term from gcc manual), which brings me to question,
- what calls what? And what is the "true" header, that define
std::string
of them? (is it/usr/include/c++/8/bits/basic_string.h
?...)
from this question Why does omission of "#include <string>" only sometimes cause compilation failures?, they mentioned:
Some compilers on some platforms may on some time of the month compile even though you failed to include the header
But from the upper output of the "string headers", there is multiple of them , so how's possible for a compiler to compile only sometimes
? Which of these headers are really important for successful compilation?
- How to orient in cpp headers, which are meaningful for compiler, and could be tracked their "#include stack" (i.e. other meaningful headers)?
EDIT: If it depends on my specific implementation of my stdlib++, then I want to know how can I determine from source whether that inclusion is made before I try to compile. Not by "If it compiles, then it works".