7 Replies - 1012 Views - Last Post: 09 August 2011 - 09:55 AM Rate Topic: -----

#1 Goast Remusov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-April 11

Practical differences between "using namespace std" or "st

Posted 08 August 2011 - 01:36 PM

I haven't been studying C++ very long, but I've been using a variety of resources and noticed that there is some variation among programmers that favor "using namespace std;" or "std::" when having to access that namespace in their examples. Is there any practical difference between the two methods?

To me, it would seem more efficient to just put "using namespace std;" at the top of the code, especially in long programs that call on it constantly.

Is This A Good Question/Topic? 0
  • +

Replies To: Practical differences between "using namespace std" or "st

#2 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: Practical differences between "using namespace std" or "st

Posted 08 August 2011 - 01:46 PM

Many programmers believe that
using namespace std;
is bad practise because it often defeats the purpose of namespaces. Let me illustrate.

Imagine that you have a namespace called foo and within that namespace you declare a function called bar() that calculates some value, but with some kind of special property that is not within std::bar.

Namespaces would be useful here because you would access the normal bar by using:
std::bar(x)

and you would access your own bar() using
foo::bar(x)


Which is one of the cool thing about namespaces.
But when doing
using namespace std;
you tell the compiler to use the std namespace for every function you call if it's present in the namespace, which kinda defeats the purpose.

I personally prefer writing "std::" in front of every call from the std namespace just to be consistent, even though I might only use the std namespace in my application. That way I'm sure that people know which function I am talking about when somemone other than me is reviewing my code.

This post has been edited by MathiasVP: 08 August 2011 - 01:58 PM

Was This Post Helpful? 4
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: Practical differences between "using namespace std" or "st

Posted 08 August 2011 - 04:14 PM

When using namespace std; and you have an namespace conflict sometimes the error messages are very misleading. Even without you defining your own functions there can be conflicts between standard functions in the global namespace and standard functions in the std::namespace. For example there is a function named tolower() in both the std::namespace and global namespace. See this topic.

So for small programs it is possibly acceptable to use using namespace std;, but I would recommend using the scope resolution operator ( :: ) for most programs. Also don't forget that you can use using std::cin; to just import cin into the global namespace.


Jim
Was This Post Helpful? 1
  • +
  • -

#4 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 232
  • Joined: 07-July 10

Re: Practical differences between "using namespace std" or "st

Posted 08 August 2011 - 07:34 PM

You can also use namespace aliases like so:
namespace dts = std;
dts::cout << "asdf";

This post has been edited by Coding in the name of: 08 August 2011 - 07:34 PM

Was This Post Helpful? 1
  • +
  • -

#5 ryan20fun  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 109
  • Joined: 20-December 10

Re: Practical differences between "using namespace std" or "st

Posted 09 August 2011 - 12:58 AM

i use the "using namespace std;" in my source files, not in the headers to avoid the problems mentioned.
just easier to write string then std::string in the source file.

my 2 cents :)
Was This Post Helpful? 0
  • +
  • -

#6 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Practical differences between "using namespace std" or "st

Posted 09 August 2011 - 02:59 AM

Just so you know in a code like this . . .
int main()
{
    using namespace std;
    cout<<"Hello, World!"<<endl;
    return 0;
}
void foo()
{
    cout<<"Goodbye, World!"<<endl;
}

The cout<<"Goodbye, World!"<<endl; will not compile because they live in the std namespace so having using namespace std in one function will not interfere with another function. I know this is not the answer to your question but just in case you didn't know about this. :)

So in a long function which only uses namespace std, i will write using namespace std at the beginning :)

This post has been edited by hulla: 09 August 2011 - 03:00 AM

Was This Post Helpful? 0
  • +
  • -

#7 Goast Remusov  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-April 11

Re: Practical differences between "using namespace std" or "st

Posted 09 August 2011 - 09:47 AM

Thanks for your input everyone. I have a better understanding of the issue now.

I found Mathias' answer very useful given the examples that he provided because I had gotten into the habit of just including std:: before the rest of my code. Also, the topic that Jim linked me to will probably save me some frustration in the future. Everyone else gave me some useful tips to, so again, thanks everyone!
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Practical differences between "using namespace std" or "st

Posted 09 August 2011 - 09:55 AM

Nobody has mentioned optimization issues. I recall that by doing using namespace std; includes everything defined in that namespace, whereas std:: only includes the functionality when that scope is resolved. I'm well aware now that optimizing compilers will do this for you anyway, but it is something to keep in mind if you disable optimization.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1