2

I included the line using namespace std;on top of my code. Now consider the declaration int a,b,c;.Is above code an equivalent to int std::a,std::b,std::c;? If so, consider the following example:

If I declared(defined) a namespace Hi after the line using namespace std;. Is Hi a part of std i.e .., am I supposed to use Hi as std::Hi?

I'm a beginner .

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
Nitesh_Adapa
  • 133
  • 7
  • No. You only use the stuff from the namespace. The `std` namespace contains stuff like `cout` and `string` and a *LOT* of other stuff. – ICanKindOfCode Mar 21 '18 at 16:41
  • 3
    Both are no. And you need to read books instead of asking too basic questions. – llllllllll Mar 21 '18 at 16:41
  • 1
    No, with the 'using namespace' statement you enable symbols from the std namespace to be used without the namespace qualifier. – zaw Mar 21 '18 at 16:42
  • @liliscent He might just be doing it as a hobby, in which case a book isn't really a great idea. – ICanKindOfCode Mar 21 '18 at 16:42
  • 8
    @KidDoesCodingAndHasNoFriends: That is the opposite of true. – Lightness Races in Orbit Mar 21 '18 at 16:43
  • 1
    I strongly disagree, @KidDoesCodingAndHasNoFriends . Having good references saves a ton of time, even a single book. Without a good guided reference you often lack the correct terminology required to find tutorials or online reference materials via web searches and lack the frame of reference required to tell good materials from bad. Of course that requires finding a good book in the first place. [Fortunately Stack Overflow maintains a curated list of trusted reference material](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Mar 21 '18 at 16:48
  • @KidDoesCodingAndHasNoFriends I miss those times when I was a kid, had programming only as a hobby, and had enough time to read all those fancy books about C++ programming :) – Daniel Langr Mar 21 '18 at 16:52

2 Answers2

10

Is above code an equivalent to int std::a,std::b,std::c;

No. A using namespace statement means that your code can use members of that namespace without having to qualify them with the namespace's name.

For example, say you have a #include <string> statement followed by a using namespace std; statement. You can then refer to the std::string class as just string instead.

Or, say you have a #include <iostream> statement followed by a using namespace std; statement. You can then refer to the std::cin and std::cout objects as just cin and cout, respectively.

Simply using a namespace does not add anything to that namespace. It is a way of bringing content from the specified namespace into the calling namespace.

If I declared(defined) a namespace Hi after the line using namespace std;. Is Hi a part of std

No. To do that, Hi would have to be declared inside of a namespace std block, eg:

namespace std {
    namespace Hi {
        ...
    }
}

But, that is undefined behavior (except in special cases), as you are generally not allowed to add things to the std namespace.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

No, there is no need for std::Hi.The variables you declare are not in std namespace. The std generally contains common language facilities such as cout, cin, string etc.

skr
  • 914
  • 3
  • 18
  • 35