Since the namespace std already has the c++ libraries that contain the function definitions(if i am right), then why do we include header files on top of it??. Since namespace std includes the c++ standard libraries, I don't see a reason to include the declarations of it separately.
When you do #include <iostream>
it causes a set of classes and other things to be included in your source file. For iostream, and most of the standard library headers, they place these things in a namespace named std
.
So the code for #include <iostream>
looks something like this:
namespace std {
class cin { ... };
class cout { ... };
class cerr { ... };
class clog { ... };
...
}
So at this point, you could write a program that looks like:
#include <iostream>
int main() {
std::cout << "hello\n";
return 0;
}
Now, some people feel that std::cout
is too verbose. So they do:
#include <iostream>
using namespace std;
int main() {
cout << "hello\n";
return 0;
}
Personally, I'd recommend against this, and if you really feel that std::cout
is too verbose, then I'd suggest that you use a smaller using
statement.
#include <iostream>
using std::cout;
int main() {
cout << "hello\n";
return 0;
}
If you're wondering why I would recommend against using namespace std
, then I would forward you to the following two other posts on stackoverflow:
The compiler itself does not have the definitions of the things that are in any namespace (whether it is std
or some other namespace). That is the role of source files and header files.
What using namespace std;
tells the compiler is that "If you can't find some name in the current namespace, go look in the std
namespace as well".
What #include <iostream>
tells the compiler is that you want the contents of the header called iostream
to be included in your sources. This will provide the compiler with code to do cin
, cout
and a lot of other related functionality. The content of this file is declared like namespace std { ... all the stuff goes here ... }
.
The usage of namespace allows someone else, working in namespace math;
to not have to worry about "Hmm, what do I do now, I need a counter for number of entrances, let's call it cin
- but hang on, is that ever used anywhere?".
This may not be the greatest of examples, but in large projects, it gets increasingly hard to keep track of things and what names they have. And C++ is a language intended for large projects of millions of lines of code - and now it starts getting hard to remember if you've used a particular name or not. Namespaces make sure that you don't have to worry about it outside of a particular namespace.
(Oh, and in my code, I tend to not use using namespace std;
, but write std::cout << "Hello, World!" << std::endl;
- this helps to make it clear that the cout
I'm using here is the std
one, and not something else. This is particularly useful when you have several namespaces with similar things, like in my own compiler, where I have my compiler with it's functionality, the std
namespace providing some things, and llvm
compiler things - if I were to stick using namespace llvm;
at the beginning of the code, it would be very hard to track whether Type* p = ...
; is from LLVM or some part of my own code.)
...why do we include header files on top of it???
Yes, there is some big confusion here.
Namespaces are a method to categorize or group together, symbols such as function names.
Namespaces are design to prevent name conflicts between different software components, such as libraries.
Functions that are a part of the standard language, are grouped under the namespace std
.
The C++ language provides statements to reduce the amount of typing when using namespaces. One of these is the using
statement.
When you write a program, the compiler is not required to automatically include all the symbol definitions, such a function declarations. You need to tell it which functions you plan on using.
For example, I could write a program without using the sort
or advance
functions from the algorithm
group. Therefore I would not include the header file algorithm
.
The C++ language is designed to be "use what you need", in other words, we can create small programs by only including the functions we need.
By the way, there are many other platforms out there than the one you are using.
Some platforms need to fit in small memory areas and may not have a keyboard or display (such as embedded controllers).
So remember, C++ was defined to support platforms from the small and constrained to the large and virtually unconstrained system.
Thus the requirement to "include only what you need" theme.
In summary, since the C++ languages doesn't automatically, magically, provide the definitions of the entire library, including the template library, you need to tell the compiler which groups of functions you want to use. This allows for quicker compilations since only the required header files are specified.
Note : Some shops and library supplies like to use the Monolith include system. This means that they have one include file that includes their entire library, whether you use one function or many. The windows.h
is a classic example. One detriment is that when one header file is changed, everything needs to be rebuilt. With separated include files, only the components that include the changed header file need to be rebuilt.
Use of preprocessor directive #include is as old as c++ itself. And its not going away any sooner.In C++ namespace Doesn't import anything into your program, it just defines the scope of your particular header file's function.So, both are required. Click here to understand why use namespace.
Your question is: namespace std
has all the definitions of functions/classes of iostream
library. So simply using using namespace std
is enough to call or use cout
or all other functionalities of iostream
library. Why do we have to use line #include <iostream>
? It seems redundant.
Roughly we can think that iostream
library has two files: header and implementation/source file. These two files have a namespace called std
. The header file only contains the declarations or forward declarations of classes or functions or variables that iostream
library is going to use and these declarations or forward declarations are under the std
namespace. The implementation file contains the actual implementations of the classes or functions or variables and these implementations are under the std
namespace; this file is also called the source file.
So if you only use using namespace std
without #include <iostream>
in your main.cpp
file compiler will search std
namespace in your main.cpp
file, but it is not here. You will get compiler error.
Now if you include this #include <iostream>
line in your main.cpp
, preprocessor will go iostream
's header file and copy the std
namespace along with its code into our main.cpp
. And linker will link the precompiled(as iostream
is a SLT so it comes with compiler with prebuilt) source/implementation file of iostream
to your main.cpp
. Now to use functions or variables of iostream
that sit under std
namespace in main.cpp
we have to use scope resolution operator(::) to tell the compiler that cout
, cin
and other functionalities of iostream
sit at std
namespace. Thus we simply write like this: std::cin
, and std::cout
.
Now typing std::
seems redundant to some people so they tell the compiler by using this using namespace std
"Hey compiler, if you don't find any variables/functions/classes in global/current namespace go look in the std
namespace." Though this is not the best practice but that is another topic to discuss. Therefore your assumption that std
namespace contains all the definitions of all SLT's functions/classes/variables of C++ is not correct in most cases, but std
namespace only contains the declaration from STL is the correct assumption.
Here is a dummy implementation how iostream
libray is added to our code files: iostream.h
:
// This is header file that only contains the
// functions declarations.
namespace std_dum
{
int add(int x, int y);
int mult(int x, int y);
}
iostream_dum.cpp
:
// This is source file of iostream_dum.h header
// which contains the implementation of functions.
#include "iostream_dum.h"
namespace std_dum
{
int add(int x, int y)
{
return x + y;
}
int mult(int x, int y)
{
return x * y;
}
}
main.cpp
:
#include <iostream>
#include "iostream_dum.h"
int main()
{
std::cout << std_dum::add(100, 200) << '\n';
std::cout << std_dum::mult(100, 200) << '\n';
return 0;
}
To see what happens to our main.cpp
file after preprocessing run this command: g++ -E main.cpp iostream_dum.cpp
.
Here is roughly our main.cpp
looks like:
namespace std_dum
{
int add(int x, int y);
int mult(int x, int y);
}
int main()
{
std::cout << std_dum::add(100, 200) << '\n';
std::cout << std_dum::mult(100, 200) << '\n';
return 0;
}
namespace std_dum
{
int add(int x, int y)
{
return x + y;
}
int mult(int x, int y)
{
return x * y;
}
}
For the sake of clarity, I discarded all the codes that the preprocessor copied from #include <iostream>
.
Now it should be pretty clear.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.