0

Since we are including the iostream header, what was the need to define cin, cout, endl, etc. inside the std namespace?

What added advantage did this cause?

Example: In C, we use to include stdio header file and used to call printf() and scanf() directly.

(I am aware namespaces is a C++ feature, the example is just to point at my confusion to understand its usage).

Edit : What would happen if cin, cout, etc. weren't a part of any namespace and we could just use them directly by just importing the header file. What problems would that cause?

Sumaiya A
  • 137
  • 1
  • 14
  • These are standards commands, why shouldn't they be in the standard (std) namespace? – BNilsou Sep 15 '20 at 12:06
  • Separation of concern perhaps? Separation of symbols? Minimizing name-collisions? – Some programmer dude Sep 15 '20 at 12:06
  • 1
    namespaces are to organize names. You can have `std::foo` and `my_lib::foo` and the names don't clash. Actually I have no clue how C deals with that – 463035818_is_not_an_ai Sep 15 '20 at 12:09
  • 1
    @idclev463035818 it doesn't. Much pain may ensue. – Quentin Sep 15 '20 at 12:09
  • @idclev463035818 C would have """solved""" the problem with endless names: `std_foo` and `my_lib_foo` ^^ This is why C libraries often prepend every functions names by the library name. – Fareanor Sep 15 '20 at 12:17
  • Are you actually asking: "What is the use of namespaces? Why do they exist?" – Jabberwocky Sep 15 '20 at 12:19
  • 2
    Fun fact, C++ didn't have namespaces from the onset (though they were in the first published standard). There are legacy C++ codebases in the wild today that bear the mark of using the C way to avoid name collisions. It's not pretty. – StoryTeller - Unslander Monica Sep 15 '20 at 12:23
  • @Jabberwocky I do understand what is the use of namespaces. Like in some comments mentioned above, I understand there maybe more than one definition of foo() and c++ allows us to use namespaces to keep them in different namespace scopes. I am specifically asking why was cin, cout, etc placed inside a namespace? why separate scope in this case? – Sumaiya A Sep 15 '20 at 12:29
  • @SumaiyaA - What about stuff like `vector`, `list` or `stack`? Do you think it's a good idea for the standard library to hog those in the global namespace? And if it's gonna partition those, why leave other stuff out? – StoryTeller - Unslander Monica Sep 15 '20 at 12:31
  • once you have namespaces you want to use them. `cin` and `cout` are not an expception, why would they be? – 463035818_is_not_an_ai Sep 15 '20 at 12:35
  • @StoryTeller-UnslanderMonica Let me reframe my question to point out my confusion. What would happen if cin, cout, etc. weren't a part of any namespace and we could just use them directly by just importing the header file. What problems would that cause? – Sumaiya A Sep 15 '20 at 12:42
  • The problem of having a standard library that is inconsistent in its organization. – StoryTeller - Unslander Monica Sep 15 '20 at 12:49
  • @StoryTeller-UnslanderMonica So std was used to group standard functions under one scope for better organization? Is my understanding correct? – Sumaiya A Sep 15 '20 at 12:54

1 Answers1

4

It stops pollution of the global namespace. This is a headache in C and a lot of potential pitfalls have grown up: e.g. everything beginning with str is reserved in C. Furthermore it allows the C++ standard to define behaviour around std:: - for example, you are not allowed to add things to std with a few exceptions.

By and large then the global namespace is at the mercy of the programmer.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483