Jump to content

Recommended Posts

Do you recommend doing:

#include <iostream>
using namespace std;

int main(void) {
   cout << "Test Text";
}

or

#include <iostream>

int main(void) {
  std::cout << "Test Text";
}

 

Throughout a variety of reading, I have been leaning towards std::cout instead of doing use namespace.  What is your personal preference?

 

What are the pros/cons of each?

I know you can setup your own namespaces:

#include <iostream>
using namespace std;

namespace mystuff{
int value = 5;
}

int main()
{
      cout << mystuff::value; //outputs 5
      return 0;
}

As of yet however, I have not found a really need to do this.

 

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c - This link has also gotten me doing some additional thinking. If it is bad practice to use "using namespace"

then "why" is it a bad practice to begin with. All in all since I have read it's bad practice I have been consistently using the std::cout way of doing things, but I have always wondered as to "why" it is considered more proper.

Link to comment
https://forums.phpfreaks.com/topic/233276-c-std-or-using-namespace/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.