Why not using namespace std ?

Hello, everyone! These days i was told that is not good to use using namespace std and that i should put std:: in front of every std function( like cout, cin, etc). I am asking you, real programmers why should i do this.. if you can explain in large amounts please!
Thanks in advance!

It is a bad idea due to name conflicts.
closed account (z05DSL3A)
The using directive, ie: using namespace std;, does not add a name to the current scope, it only makes the names accesable from it. This means that:
» If a name is declared within a local scope it hides the name from the namespace;
» a name in a namespace hides the same name from an enclosing scope;
» a compilation error occurs if the same name is made visible from multiple namespaces or a name is made visible that hides a name in the global name space.

using Declarations, ie:
1
2
3
using std::cout;
using std::cin;
using std::endl

add names to the scope in which they are declared. The effect of this is:
» a compilation error occurs if the same name is declared elsewhere in the same scope;
» if the same name is declared in an enclosing scope, the the name in the namespace hides it.
Last edited on
so, from what i understand , this would not be a problem for my begginer/intermediate programs but it will become when i will start real programming. Am i right?
It's good practice to not make bad habits now when you know you'll stop later.
Indeed! Thank you for the explanations and the advices. Good day!
closed account (z05DSL3A)
I have seen people fall foul in a 'small' project...the important thing is to know what happens so you recognise when you have a problem due to it.
> this would not be a problem for my begginer/intermediate programs
> but it will become when i will start real programming. Am i right?

Right.

A good programmer is, to a large extent, a programmer who has developed good programming habits; one who cares about programming hygiene.

The problem with inculcating early bad habits is that, in practice, it is exceedingly difficult to get rid of them later. Far easier to start doing things right from the very outset.
This should be self-explanatory:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

namespace foo
{
    int my_variable = 5;
}

namespace bar
{
    int my_variable = 10;
}

int main()
{
    int my_variable = 15;

    std::cout << foo::my_variable << std::endl; // 5
    std::cout << bar::my_variable << std::endl; // 10
    std::cout << my_variable;                   // 15
    
    return 0;
}

For a more concrete example, suppose you write a program that computes the sum of all the integers from 1 to 100:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>

using namespace std;

int count = 0;

int main()
{
    for (int i = 1; i <= 100; i++)
    {
        count += i;
    }

    cout << count; // 5050
}

No problem, right? That is, until you introduce the following line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<algorithm> // !!!

using namespace std;

int count = 0; // Name Conflict

int main()
{
    for (int i = 1; i <= 100; i++)
    {
        count += i;
    }

    cout << count;
}

This is because there already exists an identifier std::count in algorithm.
Thank you again actualy i understood a lot now from these codes. I'll try creating myself some good habbits in programming basing on what i learn new.
closed account (S6k9GNh0)
In some cases, name collisions with namespaces is a good thing... especially if its meant to be interchangeable.
> In some cases, name collisions with namespaces is a good thing...
> especially if its meant to be interchangeable.

Experience had shown that name collisions were rarely, if ever, good.
Which was the raison d'être for introducing inline namespaces in C++11.
Topic archived. No new replies allowed.