using namespace std;

I'm trying to understand how the key word Using works and I think I have a basic understanding after reading this websites tutorial here.

http://www.cplusplus.com/doc/tutorial/other_data_types/

My question is how does using namespace std; fit into what I've learned from the tutorial.

After reading the tutorial I'd think setting up the standard namespace would look something like this:

 
  using std = namespace;


or

 
  typedef namespace std;



I'm missing something here and if anybody can explain the part I'm not understanding (or a link to a manual) about why it's written using namespace std; I'd be grateful.
Scroll down to the bottom of this page:
http://www.cplusplus.com/doc/tutorial/program_structure/

You can also read about namespaces here:
http://www.cplusplus.com/doc/tutorial/namespaces/

Basically speaking, cout and cin are actually "supposed" to be std::cout and std::cin, since they are both within the std namespace.

So a simple program might look like this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
    std::cout << "Hello world!" << std::endl;
    std::cout << "Please enter a number: ";
    int number;
    std::cin >> number;
    std::cout << "You entered " << number << std::endl;
    //cout << "Hi again"; // Error: what's 'cout' ?
}

Notice that I had to put std:: in front of cout, cin, and endl, since they are all in the std namespace.
Now, let's say that I'm super lazy and I don't like having to type std:: all the time.
By putting using namespace std; in my code, it basically lets me drop the std:: prefix so I don't have to type it anymore:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello world!" << endl; // Okay: the compiler knows I mean 'std::cout' and 'std::endl'
    cout << "Please enter a number: ";
    int number;
    cin >> number;
    cout << "You entered " << number << std::endl; // But you can still put std:: if you want
}

Now, a lot of people recommend that you don't use using namespace std;.
You can read this for some reasons why:
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
I see, so if I understood what I read. You don't have to explicitly declare a type when using the key word using?

I could do something like

using C;

or

using pChar;

which would use the same mechanism as using namespace std;

but how does the type get defined later on in a program?


I have been avoiding using using namespace std; but I'd still like to understand how the key word using works which is where my questions are coming from. using namespace std; just seemed like the most ubiquitous example of using I could think of in my limited experience.
Last edited on
Err, using namespace std; just makes it so that you don't have to type std:: when accessing functions/variables/types within the std namespace. (Normally, you would have to type e.g. std::cout, but with using namespace std; you can just type cout and the compiler will know that you mean std::cout)

Namespaces aren't types.

Now, in C++11, they added a way to alias types with the using keyword:
using myIntNickname = int;
The above would be identical to
typedef int myIntNickname;

But that's a completely unrelated usage of using.

You can actually do something like
using std::cout;.
This is basically just like using namespace std;, except only for std::cout.
That is, it lets you type cout without having to prefix it with std::, but everything else in the std namespace still has to have std:: in front of it.

Your two examples, using C; and using pChar;, would not be valid C++ code.
Now, using C = char; and using pChar = char*; (for instance) would be (it defines alias of char and char*), as well as using C::foo; and using pChar::bar; (if C and pChar are namespaces and foo is a member of namespace C and bar is a member of namspace pChar. Why you would want to name your namespaces C or pChar, I have no idea).
You can also clone namespaces:
1
2
3
4
5
6
namespace standard = std;

int main()
{
    std::cout << "Hi" << standard::endl;
}
Last edited on
so basically the compiler treats the key word using differently depending on how the code is written in C++. Ie the compiler would use one set of algorithms for using namespace XYZ and a completely different and unrelated set of algorithms for using C = char;

Essentially it is an overloaded key word?

That's also kind of a nice trick to know there LB. Thanks for sharing :)
Last edited on
don't forget using-declarations. http://en.cppreference.com/w/cpp/language/using_declaration
A lot of keywords mean different things in different contexts.
Topic archived. No new replies allowed.