We all know how to use it, but how does one stop using it?
1 2 3 4 5 6 7
usingnamespace std;
int main(){
cout << "blah";
//stop using namespace std; however that works
std::cout << "hi"; // I am required to use std:: again.
}
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.
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 usingnamespace 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>
usingnamespace std;
vector <int> a(12);
namespace empty_namespace {
};
usingnamespace empty_namespace;
class vector{
// code that’s not for a resizable array
}
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.
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
#include <iostream>
namespace m
{
int run();
}// close of namespace m
int main()
{
return m::run();
}
void f1();
namespace m
{
usingnamespace 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
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(){
{
usingnamespace 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
usingnamespace 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.
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.
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
}
It seems @highwayman is asking about this sort of thing:
1 2 3 4 5 6
class foo
{
usingnamespace 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
{
usingnamespace 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?