-3

Admittedly I'm new to C++ and I'm working hard to learn different new stuff.

Please explain the case of using namespace std

I mean, some people use things like std::cin or std::string all the time instead of typing using namespace std and simply using cin or string from that point onward. Is there any good reason why they do such a seemingly stupid thing by milking the you know what out of std::?

3 Answers3

2

using namespace std; can be used and abused, just like most any other thing.

The one rule you should remember is

  • Never put a using namespace std; in the global namespace in a header.

That's because names like distance very easily can collide with client code.

In general, the advantages of C++ namespaces is that they allow easy disambiguation where names clash, while generally allowing you to use readable unqualified names. If one always qualifies names then the only difference from C style fixed prefixes and suffixes is the presence of :: in there, and I don't think it can be argued that having those colons in a name is any advantage. But this argument only pertains to namespaces that one defines oneself: for the std namespace it can be argued that it's imposed by the standard, so that a practice of fully qualified names means to use std:: always (and I know that this is done by a number of companies, even though I disagree strongly with these programmers' claims that the code then is more readable than without the std:: prefixes).

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • I'm a rookie. Please explain what this means: "Never put a using namespace std; in the global namespace in a header." What is global namespace? Is it the space between the preprocessor directives and the main? – The Vigilante Sting Feb 12 '15 at 05:51
  • @TheVigilanteSting check my detailed explanation , hope i helped – m0bi5 Feb 12 '15 at 05:52
  • @TheVigilanteSting Global namespace is outside a class or namespace, so if you put using napespace std in a header file, it would apply to every source file that included that header file. – Dronz Feb 12 '15 at 05:53
0

That isn't at all a stupid question.

iostream is a C++ code file (more specifically a header file, but that doesn't matter). It contains the code that defines cout, cin, endl, etc. If we want to use them, we need them to be in our code, that's why we include it. However, if we look at it's contents, we will (schematically) see something like this (This is not a serious attempt of trying to map iostream schematically, but I will refrain from making things too complicated, and for the explanation, it will do):

// iostream header file    
    namespace std
    {
       ostream cout;
       istream  cin;
       // etc etc
    };

Using the using keyword doesn't mean we add functionality, it means we say that we read things by default. If we say using namespace std; then we say: If we come across an OBJECT name that doesn't exist in our current namespace, check if there exists a namespace std in which it does exist, and use that object. Thus, it doesn't really add a function, it is the include that "loads" cout, cin, endl and all the like.

(Note that the schematic above is not what you will find when opening the iostream file, in reality it's a lot more complex and also contains a lot of definitions.)

The tutorials page gives a nice explanation http://www.cplusplus.com/doc/tutorial/namespaces/

m0bi5
  • 8,900
  • 7
  • 33
  • 44
0

Namespace is a way to structure program, imagine you have your own string class which put in namespace foo. If you don't explicitly type std:string, the compiler will get confused which string should be used.

flynn
  • 23
  • 1
  • 6