Why you should not use using namespace std

People don't like typing std:: over and over, and they discover that using namespace std lets the compiler see any std name, even if unqualified. The fly in that ointment is that it lets the compiler see any std name, even the ones you didn't think about. In other words, it can create name conflicts and ambiguities.

For example, suppose your code is counting things and you happen to use a variable or function named count. But the std library also uses the name count (it's one of the std algorithms), which could cause ambiguities.

Look, the whole point of namespaces is to prevent namespace collisions between two independently developed piles of code. The using-directive (that's the technical name for using namespace XYZ) effectively dumps one namespace into another, which can subvert that goal. The using-directive exists for legacy C++ code and to ease the transition to namespaces, but you probably shouldn't use it on a regular basis, at least not in your new C++ code.

If you really want to avoid typing std::, then you can either use something else called a using-declaration, or get over it and just type std:: (the un-solution):

  • Use a using-declaration, which brings in specific, selected names. For example, to allow your code to use the name cout without a std:: qualifier, you could insert using std::cout into your code. This is unlikely to cause confusion or ambiguity because the names you bring in are explicit.
    #include 
    #include 
    
    void f(const std::vector& v)
    {
      using std::cout;  // ← a using-declaration that lets you use cout without qualification
    
      cout << "Values:";
      for (std::vector::const_iterator p = v.begin(); p != v.end(); ++p)
        cout << ' ' << *p;
      cout << '\n';
    }
    
  • Get over it and just type std:: (the un-solution):
    #include 
    #include 
    
    void f(const std::vector& v)
    {
      std::cout << "Values:";
      for (std::vector::const_iterator p = v.begin(); p != v.end(); ++p)
        std::cout << ' ' << *p;
      std::cout << '\n';
    }
    

I personally find it's faster to type "std::" than to decide, for each distinct std name, whether or not to include a using-declaration and if so, to find the best scope and add it there. But either way is fine. Just remember that you are part of a team, so make sure you use an approach that is consistent with the rest of your organization.

C++ has a standard library that contains common functionality you use in building your applications like containers, algorithms, etc. If names used by these were out in the open, for example, if they defined a queue class globally, you'd never be able to use the same name again without conflicts. So they created a namespace, std to contain this change.

The using namespace statement just means that in the scope it is present, make all the things under the std namespace available without having to prefix std:: before each of them.

While this practice is okay for example code, pulling in the entire std namespace into the global namespace is not good as it defeats the purpose of namespaces and can lead to name collisions. This situation is called namespace pollution.

The original article was published in the blog: https://fluentprogrammer.com/dont-use-using-namespace-std/

Never try to import the whole std namespace into your program. Namespaces are developed to avoid naming conflicts. But, when we import the whole namespace there is a possibility that this could lead to name conflicts. You can do more harm than more good.

Description

using namespace std;

Enter fullscreen mode Exit fullscreen mode

It is okay to import the whole std library in toy programs but in production-grade code, It is bad. using namespace std; makes every symbol declared in the namespace std accessible without the namespace qualifier. Now, let’s say that you upgrade to a newer version of C++ and more new std namespace symbols are injected into your program which you have no idea about. You may already have those symbols used in your program. Now the compiler will have a hard time figuring out whether the symbol declared belongs to your own implementation or from the namespace you imported without any idea. Some compilers throw errors. If you are unlucky, the compiler chose the wrong implementation and compile it which for sure leads to run time crashes.

For example, consider the below code,

#include 
#include "foo.h" \\ Contains the implementation for same_as

using namespace std; \\Bad practice
class semiregular  { }; \\Implemented in foo.h

int main() {
  semiregular  b;
  ............
  cout << "Hello, Congrats.You are on your way to becoming an expert in programming...">>
}

Enter fullscreen mode Exit fullscreen mode

The above code won’t trigger any compile-time error if compiled with C++17. It may throw an error when compiled with C++20 that the header file is not included. This is because semiregular is a feature implemented in C++20. So it’s always better to be on the safer side by not loading up your program with all the symbols a namespace offer. Instead use the:: annotation and use the full qualifier like std::cout.

#include 
#include "foo.h" \\ Contains the implementation for same_as

class semiregular  { }; \\Implemented in foo.h

int main() {
  semiregular  b;
  ............
  std::cout << "Hello, Congrats.You are on your way to becoming an expert in programming...">>
}

Enter fullscreen mode Exit fullscreen mode

The above code didn’t load the whole of std so semiregular is not considered to be part of std and it considers the class implemented by us.

Why using namespace std is not a good practice?

While this practice is okay for example code, pulling in the entire std namespace into the global namespace is not good as it defeats the purpose of namespaces and can lead to name collisions. This situation is called namespace pollution.

What happens if you dont use namespace std?

If we don't want to use this line of code, we can use the things in this namespace like this. std::cout, std::endl. If this namespace is not used, then computer finds for the cout, cin and endl etc.. Computer cannot identify those and therefore it throws errors.

Why should we avoid use of using namespace in header files?

The 'using namespace' directive pulls an entire namespace into not only your header, but also any client code that uses your header. This can provoke ambiguities in client code, or even select the wrong function during overload resolution.

Should I use using namespace in C++?

Namespace in C++ is the declarative part where the scope of identifiers like functions, the name of types, classes, variables, etc., are declared. The code generally has multiple libraries, and the namespace helps in avoiding the ambiguity that may occur when two identifiers have the same name.