Using Namespace

I was reading about Namespaces and the general impression I'm getting from what I've read online is doing Using namespace std is a big no no.

Should I start writing programs without that included?
Should I start writing programs without that included?

Yes.

While prefixing everything with std:: may seem clumsy at first, it will increase the clarity of code after you get used to it, because you will have a hint if something is from the C++ standard library or not.

Here's the rationale as to why using namespace std; is bad style:

http://www.parashift.com/c++-faq/using-namespace-std.html
There was an earlier discussion of this topic here: http://www.cplusplus.com/forum/general/72248/

And if you search around, you would find a few more.
Is there a reference or guide somewhere that I can look at to know when I need to use std::

I'd like to avoid trial and error learning if I could.


For example, something like this gives me a compile error and I'm not sure why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// bad_alloc standard exception
#include <iostream>
#include <exception>

int main () {
  try
  {
     int* myarray= new int[1000];
  }
  catch (exception& e)
  {
    std::cout << "Standard exception: " << e.what() << "\n";
  }
  return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// bad_alloc standard exception
#include <iostream>
#include <exception>

int main () {
  try
  {
     int* myarray= new int[1000];
  }
  // catch (exception& e)
  catch( const std::exception& e )
  {
    std::cout << "Standard exception: " << e.what() << "\n";
  }
  // return 0;
}


Tip: as far as possible, catch exceptions by reference to const

In general, you won't go wrong if you always use std:: to qualify every name from the standard library.

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

int main ()
{
    std::string str ;

    // this is fine; ::std::getline is found via koenig look up (ADL)
    // and there is no candidate ::getline()
    getline( std::cin, str ) ;

    // this too is obviously fine; koenig lookup is not required
    // it would be fine even if there was a viable ::getline()
    std::getline( std::cin, str ) ;
}
Thanks for the help. I think I found a way to know what needs to have the std:: also.

When you mentioned the standard library I googled that and found a list of what is considered the standard library on wikipedia. :)

http://en.wikipedia.org/wiki/C++_Standard_Library
I think I found a way to know what needs to have the std:: also.

Simple rule: anything that is from the C++ standard library, and isn't a macro*, should be prefixed with std::.

* Macros live outside namespaces.

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

namespace Test
{
#define K_MACRO 100
    const int k_variable = 500;
}

int main()
{
    std::cout << K_MACRO << '\n';
    // std::cout << k_variable << '\n'; // compile error
    std::cout << Test::k_variable << std::endl;
}


In essence, everything from the C++ standard library is in the std namespace.
Resist the temptation to abuse the cases where not writing std:: also works.
Topic archived. No new replies allowed.