Before knowing that the declaration of namespace can be split, I had the question that why I can use some entity defined inside the namespace std but not the others unless I explicitly #include the related header file.
Now I have an idea about how the namespace std is defined. I post it just to confirm, or if wrong, look for a correct explanation.
1 2 3 4 5 6
// header file iostream
// can be used by #include <iostream>
namespace std
{
// objects such as cout, cin are defined
}
1 2 3 4 5 6 7 8 9 10 11
// header file string
// can be used by #include <string>
namespace std
{
class string
{
// string class definition
}
// other functions related
}
So, in the main program, #include <iostream> will define part of the std namespace, but not including the class string. Although class string is indeed defined inside the same namespace, it is in the separate header file <string>. So I have to #include <string> to use std::string.
Am I correct? Do you have something supplement to this?
One more question, is the C library inherited by C++ in the std namespace?
> So, in the main program, #include <iostream> will define part of the std namespace
Yes.
> but not including the class string.
It may or may not indirectly include <string>. One standard header is allowed to include other standard headers.
> So I have to #include <string> to use std::string
Yes. To use the full functionality of std::string in a portable program, we have to include <string>
> is the C library inherited by C++ in the std namespace?
Yes. For instance, a superset of the functionality provided by the C89 header <math.h> is provided by the standard library header <cmath> http://en.cppreference.com/w/cpp/header/cmath
I have tried the <cmath> header.
If <cmath> is defined in the std namespace, why is the scope operator not necessary to call cos function? (both std::cos(0) and cos(0) compile)
> If <cmath> is defined in the std namespace,
> why the scope operator is not necessary to call cos function?
For compatibility with C, the C ++ standard library provides the C headers <math.h> etc.
An implementation is allowed to place the names in the C library in the global namespace first and then inject them into namespace std via using declarations. It is also allowed to declare/define these names within namespace std and then inject them into the global namespace scope.
To write portable C++ code, #include <cmath> and use std::cos()
To use the C library in compatibility mode, #include <math.h> and use ::cos()
> And, if <string> is indirectly included,
> will we have the full functionality without explicitly #include <string>?
if <string> is indirectly included, will we have the full functionality without explicitly #include <string>?
There's a common catch for the beginners: a library header may be partially indirectly included by another library header. #include <iostream> always includes the part of <string> where the class std::string is defined, but in most implementations it does not include the parts of <string> where std::getline is defined or where string I/O and comparison operators are defined. So even though you would be able to write std::string s = "abc"; if you included <iostream> without including <string>, you wouldn't be able to write std::cout << s; or if(s1 == s2). Of course none of that matters if you always include what you use.
So is it correct to say the following?
<headerC> is the header file users normally #include for full functionality. But the actual definition is in <headerA> which is not known by users.