Sinisterly
Something I realized about the standard namespace - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C)
+--- Thread: Something I realized about the standard namespace (/Thread-Something-I-realized-about-the-standard-namespace)



Something I realized about the standard namespace - Confidential - 06-15-2018

Using the standard namespace can actually be bad practice because it can be referred to as "global namespace pollution". This means that you may run into problems whenever you have multiple namespaces that contain an identical function name with signature, so it would be kind of obscure for the compiler to be able to choose which one it would call, but this can be bypassed by designating the namespace with your function call, like this:

Code:
std::cout

Or, if you don't want to type 'std::', then you can just do something like this:

Code:
#include <iostream> using std::cout; using std::cin; using std::endl; int main() { cout << "Hello, world!"; return 0; }

Some of you may already knew this and these "problems" are pretty much an uncommon thing to come across in the first place, but I just thought I would share it with you guys.


RE: Something I realized about the standard namespace - Vultra - 06-15-2018

I haven't used std yet as a learner but, seems interesting to read thou Smile


RE: Something I realized about the standard namespace - phyrrus9 - 06-15-2018

This problem is solved entirely if you treat it like it were C, and follow these rules:

1. only include files you absolutely need
2. fully qualify everything

With C++, rule #1 gets gummed up, because C++ programmers being retarded tend to put everything into one header file. The concept of namespaces in the first place was to combat this problem.