There is always one global namespace, you can't get rid of it, and as every other namespace is nest somewhere within it, you can't really avoid it.
The real question is what do you put in it. I don't think that it's a bad idea for beginners to put their identifiers directly in the global namespace. In fact, if you are writing the main function, then I see no reason not to use all of the global namespace as you see fit. Only once you are writing a library for others to use does it become imperative that you minimize your impact on the global namespace, usually by injecting a very small number (one?) of namespaces into it.
Whether using namespace std;
is a good idea or not is a separate, but related subject.
I personally believe that beginners should never do using namespace std;
. By definition, they are the sort of users who will not know every identifier that the std
namespace might contain and error messages partially caused by name clashes can be very confusing.
For example, with the right (or wrong!) includes the following 'beginner style' code produces an error about count
being ambiguous. An expert would have no problem fixing the code. A beginner might be stumped.
using namespace std;
int count = 3;
int main()
{
cout << count << endl;
return 0;
}
Also, using using namespace std;
requires you to introduce the word namespace and either the concept of namespaces, or fudge the directive as 'required magic' or some such. Without it, you can just say the name of the standard string type provided by C++ in the <string>
header file is std::string
. The namespace concept itself only has to be understood when placing things into separate namespaces or injecting things from one namespace into another. These are both much more advanced topics that can be save until later.