Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why should i include the header file <iostream> after using the namespace std?

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.

like image 207
Manoj Bharadwaj Avatar asked Sep 28 '14 16:09

Manoj Bharadwaj


People also ask

Why namespace is used in iostream header file?

Let’s go to the original question why namespace is used, when we have all in the iostream header file. iostream is a file that has all the things like cout, endl and etc is defined. If we need to use them we need to add that file. So basically #include <iostream> means copying and pasting the code in that file to your code.

What does it mean to include iostream in C++?

So basically #include <iostream> means copying and pasting the code in that file to your code. But if we try to use cout, endl in our code without specifying the namespace it will throw an error, because these are defined in the std namespace in the iostream.h file like following. Following is dummy of iostream.h file.

What is “std” namespace in C++?

It is known that “std” (abbreviation for the standard) is a namespace whose members are used in the program. So the members of the “std” namespace are cout, cin, endl, etc. This namespace is present in the iostream.h header file. Below is the code snippet in C++ showing content written inside iostream.h:

How many files does iostream library have?

Roughly we can think that iostream library has two files: header and implementation/source file. These two files have a namespace called std.


2 Answers

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:

  • C++ Distance Function Keeps Returning -1
  • Why is "using namespace std" considered bad practice?
like image 186
Bill Lynch Avatar answered Nov 06 '22 02:11

Bill Lynch


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.)

like image 32
Mats Petersson Avatar answered Nov 06 '22 02:11

Mats Petersson