Why Use Namespace? Exploring std, iostream & cout

  • Thread starter JonnyG
  • Start date
In summary: When people started using STL more and more, it made sense to put all of those related functions under a single namespace. It also made it easier to remember which function you were looking for.
  • #1
JonnyG
233
30
Let's take the cout function for example. That's included in the iostream library (is library the correct word here?) which is why I type #include <iostream> at the beginning of my program. Now when I want to use the cout function, I either have to type std::cout or I can just type cout but have to write using namespace std or using std::cout before my main() function.

I was told that the reason why we have namespaces is because what if we use another library that also has a cout function for example? The compiler won't know which cout function to use. But why do we associate cout with std? Isn't cout a function in the iostream library? So wouldn't it make more sense to type iostream.cout or iostream::cout when we want to use the cout function that specifically belongs to the iostream library?

What is the purpose of the namespace in this sense and what is the relationship between the namespace std and the library iostream?
 
Technology news on Phys.org
  • #2
The std namespace is reserved for important io methods called often in C++ programs. However to be fair, you have to specify if you’re using it to eliminate the std prefix.

if you recall in C programs, there is the stdio.h header file that is needed to get the various print functions.

edit: fixed studio.h to stdio.h
 
Last edited:
  • #3
jedishrfu said:
if you recall in C programs, there is the studio.h header file
Minor point, but it's stdio.h, not studio.h.
JonnyG said:
Let's take the cout function for example.
cout is not a function -- it's an output stream, similar to cin, which is an input stream. The actual function that is typically used with cout is <<, or more precisely operator<<(), the overloaded << operator.
JonnyG said:
What is the purpose of the namespace in this sense and what is the relationship between the namespace std and the library iostream?
Dividing function names and such into namespaces helps to prevent name collision. For example, you could have your own variable named cout, and as long as you didn't include iostream.h (and didn't include the line using namespace std;) your use of cout would not conflict with how the cout stream is normally used.
I don't recommend you do this, though, since it would be much more difficult to have your program do output to the monitor.

The idea of namespaces is a little like how local variables (variables defined inside functions) work. Two functions funcA() and funcB() could have a variable named temp. The two temp variables are completely separate -- setting one of them to some value inside funcA() would have no effect on the variable of the same name in the other function. The concept of namespaces takes this idea a step farther.
 
  • Like
Likes phinds
  • #4
Yes you’re right, the spellcheck changed the spelling to studio as I posted it and I didn’t notice.
 
  • #5
JonnyG said:
What is the purpose of the namespace in this sense and what is the relationship between the namespace std and the library iostream?
Using namespace is just a convenient shortcut in the syntax. Using it is never necessary. In fact, it is often discouraged and sometimes disallowed by programming standards.
 
  • #6
I think the question is less "why use namespaces?" and more "why is all of STL under a single namespace, std?"

That's probably more history than computer science. One factor is that people who adopted STL usually adopted all of it. Another is that it is probably easier to have developed STL under a single namespace. A third is that the advantages to splitting into a dozen smaller namespaces are small, and the cost of remembering what namespace gives you what function is usually larger than the benefit. But there's no technical reason why they couldn't have done it differently.
 
Last edited:
  • Like
Likes Timo
  • #7
I read all of the answers but I still do not understand. I think I may have done a poor job of explaining my confusion. Let me try again:

When we write #include <iostream> we are importing the iostream library. This is necessary for us to use cout and cin. If there were another cout for example, then we could uniquely identify the cout from iostream by writing iostream.cout (I know this is not proper syntax, I am just using it as a demonstration). Why is std used to refer to the cout that belongs to iostream? Wouldn't iostream be enough to uniquely identify which cout we are talking about?

Adding to my confusion, if we want to use vectors then we write #include <vector>, but then we again have to somehow use std to reference vector when declaring a vector. Why wouldn't the vector class be enough to uniquely identify the vector we are talking about?
 
  • #8
JonnyG said:
Why is std used to refer to the cout that belongs to iostream?

Because "std" refers to the Standard Template Library (or STL) which was originally an add-on. That add-on included cout, vector and a lot of other things.
 
  • Like
Likes JonnyG
  • #9
Vanadium 50 said:
Because "std" refers to the Standard Template Library (or STL) which was originally an add-on. That add-on included cout, vector and a lot of other things.

Thank you. Your comment about it being more history than computer science makes more sense now.
 
  • #10
You might include many libraries with the same named functions, each with a different use. You might use "use namespace" to make one of them the default, but that might not be reasonable unless the one you choose is the one you want the vast majority of the time. Otherwise, it is best to just indicate clearly, which function you want each time by prefixing it with the correct library name.
 
  • #11
jedishrfu said:
Yes you’re right, the spellcheck changed the spelling
Spellcheckers seem to me to more the problem than the solution.
 
  • Like
Likes jedishrfu
  • #12
JonnyG said:
When we write #include <iostream> we are importing the iostream library.
More precisely, we are telling the compiler to (in effect) "paste" the text of a particular "include file" into your program code at the location of the #include directive. It contains the definitions of types such as istream, ostream, fstream; the declaration of specific objects such as cout (which is of type ostream) and cin (which is of type istream); etc.

Without the namespace mechanism, if you were to #include another header file that also defines a type named istream and an object named cout, I don't think there would be any way for the compiler to distinguish between them. It would give you error messages complaining about the duplicate names. You would get the same or a similar error message if you were to declare the same variable name twice within one of your own functions.

This is why the namespace mechanism exists. In general, you have to specify the namespace of all names that you use, that are defined at the global level (outside any function), either explicitly (e.g. by writing std::cout whenever you use cout), or implicitly (e.g. by writing using namespace std or using std::cout at the beginning of your program).

When you define a class, variable, or function at the global level, it is by default in the "global namespace" which has no name. I found this example while reading up about namespaces:

C++:
#include <iostream>

int a = 100;

main () {
    int a = 200;

    std::cout << "local a is: " << a << std::endl;
    std::cout << "global a is: " << ::a << std::endl;

    return 0;
}
Here, in order to use the global a inside the function, you have to refer to it as ::a because otherwise it's "hidden" by the existence of the local a. If there's no local a, then you can refer to the global a as simply a.

(I hope I got these details right. I had to spend some time reading up about namespaces because my memory was rusty and probably incomplete.)
 
Last edited:

1. What is a namespace?

A namespace is a declarative region in which identifiers (such as variables, functions, and classes) can be declared. It helps to organize code and prevent naming conflicts.

2. Why should I use namespaces?

Using namespaces can prevent naming conflicts and make code more organized and readable. It also allows for easier reusability of code.

3. What is the "std" namespace in C++?

The "std" namespace is the standard namespace in C++. It contains declarations for the standard library, including commonly used functions, data types, and objects such as cout and cin.

4. What is the "iostream" header file?

The "iostream" header file is a C++ standard library header file that contains declarations for input/output streams, including the commonly used streams cin and cout.

5. How do I use "cout" to print output in C++?

The "cout" object is a part of the "std" namespace and is used for standard output in C++. To use it, first include the "iostream" header file, then use the syntax "std::cout << [data to be printed];" to print the desired output.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
4
Views
781
  • Programming and Computer Science
Replies
3
Views
726
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
2
Views
872
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top