Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using namespace std? [closed]

Tags:

c++

namespaces

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 .

like image 395
Nitesh_Adapa Avatar asked Jun 05 '26 21:06

Nitesh_Adapa


1 Answers

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.

like image 54
Remy Lebeau Avatar answered Jun 07 '26 12:06

Remy Lebeau