10-08-2009, 08:56 AM
(10-08-2009, 08:21 AM)MrD. Wrote: [ -> ]Wow, why quote all of that?
Lol this kid, does it matter? Not too much at all now does it, just wanted to have a post? Ahh..
(10-08-2009, 08:21 AM)MrD. Wrote: [ -> ]Wow, why quote all of that?
(10-08-2009, 08:56 AM)Guerriero420 Wrote: [ -> ]Lol this kid, does it matter?"Kid"... I'm 21, hardly a kid.
(10-06-2009, 03:29 PM)MrD. Wrote: [ -> ]Actually, that's wrong. Using a "using namespace" IS namespace pollution. This isn't really an issue in a cpp file since it will only be polluted local to that file (unless you are using a unity build); however using it inside a header file is much worse since not only does all the contents of the std library then pollute your header file at a global scope, it also pollutes any files that include that header file. I never use "using namespace" inside a header file, even for my own namespaces (I do use it for my own namespaces inside a cpp file though).
Essentially namespace pollution gets it's name from the fact that it pollutes the "global scope" with other functions, classes, structs, enums, typedefs, etc, that were not originally there. This can lead to easily avoidable namespace conflicts.
For (a pointless) example, say you had your own class called "string" inside your own namespace called "mine", you also include the std:tring header file. Now this works all well and good, when you need to use your string you specify "mine:tring", and when you want to use the STL string, you use "std:tring". You promote your own namespace to the global scope since you know what's in it and everything is hunky dory.
Now, you add a "using namespace std;" to your file. What happens is that everything in the "std" namespace is promoted to global scope so that you can access it without adding the "std" part. You remove the "std::" qualifier and re-compile your code and see the following error.
Quote:error C2872: 'string' : ambiguous symbol could be 'mine:tring' or 'std:tring'
Why has this happened? Simple, the compiler now has two classes called "string" in the global scope and it doesn't know which one it should use. It requires you to solve this by explicitly scoping your usage of "string" with the correct namespace.
Won't compileCode:#include <iostream>
#include <string>
//! My namespace
namespace mine
{
//! My string class
class string {};
}
using namespace std;
using namespace mine;
int main()
{
string stdString;
mine::string myString;
return 0;
}
Will compileCode:#include <iostream>
#include <string>
//! My namespace
namespace mine
{
//! My string class
class string {};
}
using namespace std;
using namespace mine;
int main()
{
std::string stdString;
mine::string myString;
return 0;
}
I am not saying that using "using namespace" is a bad thing in all cases, it isn't and in most cases it saves a lot of unnecessary typing. I am however saying that you should only really use it for things you know the contents of. I doubt you know the name of everything that exists in the STL, and while the chance of getting a namespace conflict with it is minimal, I still wouldn't recommend it.
Ultimately it boils down to choice, namespaces were added to C++ to avoid conflicts between library code, and your code. Collapsing the namespaces of libraries is for the most part stupid, but it is your choice. It is because it is your choice that I never use a "using namespace" in my header files, because doing so would cause anyone using that header file to have their choice of whether or not they want to pollute their global namespace with my objects taken away from them, and that is not something I want to force on them.
tl;dr "using namespace" is bad in header files as it causes namespace pollution which propagates into other files.
Aside from that, the rest of the lesson is fine. Good job