Using namespace std

Pages: 12
We all know how to use it, but how does one stop using it?

1
2
3
4
5
6
7
  using namespace std;
  
  int main(){
    cout << "blah";
    //stop using namespace std; however that works
    std::cout << "hi"; // I am required to use std:: again.
  }
Last edited on
you put std on everything.
std::cout

or a better way, IMHO, is to make yourself a 'header' file that has your favorites like cin/cout and such with some usings
using std::cout
using std::cin
... //probably about 50 or so of these depending on how you feel about it ... you need the containers, I/O, and most used algorithms, and whatever. I would just grow it as needed.

Last edited on
Aw 😔. I was hoping there was some sort of keyword out there.
If I were to make an empty namespace and then whenever I want to stop I just say using namespace empty_namespace; would it stop with using namespace std?

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

using namespace std;

vector <int> a(12);

namespace empty_namespace {

}; 

using namespace empty_namespace;

class vector{
  // code that’s not for a resizable array
}
Nope. You can "use" more than one namespace at a time.

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

namespace A {
    void func1() { std::cout << "func1\n"; }
}

namespace B {
    void func2() { std::cout << "func2\n"; }
}

int main() {

    using namespace A;
    using namespace B;
    using namespace std;
    
    cout << "hello\n";
    func1();
    func2();
}

Last edited on
it does not work like that.

cout is IN the namespace std:: --- you can't change this (easily, and bad idea to try).
if you do not have a using statement and you do not say std::cout, it will fail to compile: error, cout undefined blah blah.

You must either use std namespace, use std::cout, or prefix it with std::cout each time.

your empty namespace has no effect on this: cout is still not visible until you qualify it or expose it.

it looks like you don't really understand what all this means. really simply, and in short...
namespaces add a bogus layer around code to protect against name problems. Huge projects may have a dozen void print() functions but if they are all in their own namespace, it works. If they are all in the global (no name) namespace, it fails to compile: void print redefined. You can't have 2 of them. STD is a namespace where all the built in stuff lives. It has grown so big, though, that people accidentally reuse things they did not know or forgot about. If you use namespace std (effectively promoting it to the global namespace) you get a collision and problems. If you do not do this, they are kept distinct and safe. But you are unlikely to accidentally redefine the most used stuff like vector, cout, etc. So promoting selected entities to the global namespace TO ME is a fine compromise between catching a STD in your code and pulling in the whole thing to the global namespace.

Does this make sense?
I see a lot of coders here put std:: on everything. I just flat out refuse to do this. Its visual clutter and extra typing and generally horrible IMHO. But you can do what you want, my opinion is just that :)

if you get into a big project, you can un-use something at the end of a file as well, if you feel paranoid.
Last edited on
Ok. So. Everything made sense up until you said I could "un - use" something at the end of a file. Do you mean just because it’s a file so it automatically un - uses stuff or is there an actual way to do this I’m very confused
I personally don't know of any "unuse" feature, though I might have missed one.

Instead, what you have is a potential scope of the using declaration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using namespace n1
{
 using namespace std;

 void foo()
   {
     cout << "test"; // works
   }
}// closes namespace n1

void foo2()
{
 cout << "test"; // fails, must use std::cout
}


This would show a complaint in foo2, because the "using namespace std" is limited to the scope of "namespace n1".

If the "using namespace std" were above the namespace declaration for "n1", then foo2 would not give an error.

You may want to refer to namespace documentation with particular attention to the newer C++17 exclusive features, as they go further than this.
Last edited on
Could I do the same thing with a function or something?
Yes (for a function, I don't know what "or something" might imply)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

namespace m
{
 int run();
}// close of namespace m

int main()
{
 return m::run();
}

void f1();

namespace m
{
 using namespace std;
 
 int run()
 {
   cout << "Test";

   f1();

   return 0;
 }

}// close of namespace m

void f1()
{
 //using namespace m; // ****** cout is unknown unless this is uncommented

 cout << "test2";
}


The "using namespace m" can appear inside a function, so if the line in f1 is un-commented, the code compiles.

Further, the "using" declaration is limited to the scope of the function


@dutch 's example showed that, too

Last edited on
Yeah your right I have no idea why I said or something lol, thanks guys!
I had to look it up again (shows how much I use it, haha)
if you put the using statement in a block it ends, supposedly.
eg
{
using std::cout
cout << "words"; //ok
}

cout << "moar words"; //no longer ok.

you can mess with the idea to see if it still works. I dunno if it was language intended or a compiler side effect or not.



Do you mean I could literally just stick some brackets in? Or does it have to be in a function or for loop or if statement or whatever makes a scope (that always kills me I’m to basic for that 😫)

so like just brackets is exactly in my mind:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>

template <class T>
class vector{
  int a;
  char b; // I don’t know it’s a new class same name you get it.
};

int main(){
  {
    using namespace std;
    vector<char> a; // uses the resizable array vector class
  }
    vector<int> b; //uses the useless char and int vector class because I’m not 
                          //Using namespace std anymore. 
}


Or the regular just scope like said before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector>

std::string func(){ // have to put the std:: because I haven’t said using std
  using namespace std;
  string result; // don’t have to put the std:: because I said using std;
  result = "blah";
  return result;
}

int main(){
  std::string z = func(); // have to put std:: because the using statement is in
                         // a different scope.
}


Sry about always prematurely marking the thing as answered I just am not good at that.
Last edited on
Your first code shouldn't compile...
13:5: error: reference to 'vector' is ambiguous
Last edited on
Crap. Right...

Oops.


Ok then I guess nvm! 😜
C++'s name lookup rules mean that using unqualified names (e.g. not typing std:: everywhere) can result in a program that silently calls the wrong functions. That is the primary reason why teaching using namespace to beginners is completely wrongheaded. But that's just my strong opinion.
Last edited on
main may not like having extra {} around it.

regardless, play with it. I did not go this route; again, I pulled in the common ones into a header file that is a micro subset of standard. So you have 3 easy solutions to avoid pulling the whole thing in up front ... scoped usings, a small set of global usings, or std:: in front of everything. Given how things are going, c++20 maybe should see if this can be improved.

1
2
3
4
5
6
7
    vector<double>foo;  //illegal  

	int main()
	{
		using std::vector; 
		vector<int> test; //fine
	}
Last edited on
Hopefully maybe.. if I put using namespace std in a class does that class automatically become a namespace itself?
If I put using namespace std in a class does that class automatically become a namespace itself?


No:
class foo { namespace bar { } }; // error
Last edited on
@highwayman & @mbozzi,

It seems @highwayman is asking about this sort of thing:

1
2
3
4
5
6
class foo
{
 using namespace std;

 void f1() { cout << "does this work?"; } 
};


Yet, no, it isn't permitted. The using declaration works inside functions, but not within classes.

Nothing "automatically" causes a class to become a namespace, per se. The name of the class becomes an enclosing name, but not a namespace, it is a "class space" (I'm fairly sure that's not really a phrase in use, and I'll deny using it in the future :) ).

However, the namespace enclosing foo can declare using:

1
2
3
4
5
6
7
8
9
namespace m
{
 using namespace std;

 class foo
  {
    void f1() { cout << "Does this work"; } // should work using std, and is local to the enclosing namespace m
  };
};
Hm. That’s too bad. When you say class space, does that mean I could have a class acting as both a namespace and make objects of it? What would be the essential difference between a "class space" and a namespace?
Pages: 12