Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learn C++ (The Correct Way) - Lesson 1
#6
(10-04-2009, 06:05 PM)Code King Wrote:
Code:
using namespace std;

This line of code tells the compiler that our application will be using the standard library. Using this line of code allows us to simply write cout instead of std::cout, most people do not use this line of code which is a bad habit as it namespace pollution. Make sure you end the line of code with a semicolon (;) as it separates out your statements.

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:Confusedtring header file. Now this works all well and good, when you need to use your string you specify "mine:Confusedtring", and when you want to use the STL string, you use "std:Confusedtring". 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:Confusedtring' or 'std:Confusedtring'

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.

Code:
#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;
}
Won't compile

Code:
#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;
}
Will compile

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 Yeye
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply


Messages In This Thread
RE: Learn C++ (The Correct Way) - Lesson 1 - by MrD. - 10-06-2009, 03:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Learn C++ (The Correct Way) - Lesson 2 Code King 12 3,355 04-07-2012, 06:05 PM
Last Post: AceInfinity
  At What Age Did You Learn C++ Win 66 18,683 12-11-2011, 02:52 PM
Last Post: Neel

Forum Jump:


Users browsing this thread: 1 Guest(s)