Jump to content

using namespace std;

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
When C++ was being developed, before C++ was standardized, there were no namespaces. There was a lot of pre-standard C++ that looked like this:
int main()
{
   string line;
   cout << "enter a line of text: ";
   getline(cin, line);
   cout << "text entered: \"" << line << "\"\n";
}
Library identifiers (type names, function names, object names), such as string, cout, getline in the above example, are reserved identifiers. That means you, as a programmer, are not allowed to use these names -- or at least that you are not guaranteed to be able to use them without bugs and/or build errors arising from choosing to use a reserved identifier in the wrong context.

C and C++ have defined many reserved identifiers, and there are special rules about which ones you might be able to use and under what circumstances. The list of "forbidden" names is rather large.

When C++ was standardized, however, namespaces were added. This means, among other things, that all of these "forbidden" names can be tucked away into a namespace, which then means that these names are once again available to the programmer for use. Standard C++ places its identifiers in a namespace called std.

To use an identifier from the standard namespace, you can use the scope resolution operator :: to indicate that the programmer means the name defined in a particular namespace.

So the earlier example, with the addition of the necessary headers this time, would then look like this:
#include <iostream>
#include <string>

int main()
{
   [COLOR="red"]std::[/COLOR]string line;
   [COLOR="red"]std::[/COLOR]cout << "enter a line of text: ";
   [COLOR="red"]std::[/COLOR]getline([COLOR="red"]std::[/COLOR]cin, line);
   [COLOR="red"]std::[/COLOR]cout << "text entered: \"" << line << "\"\n";
}

/* my output
enter a line of text: hello world
text entered: "hello world"
*/
The std:: "warts" may be seen as tedious or cluttered for some programmers. Fortunately, there are alternatives. One way would be to use the using directive to specify just the names you are using from a particular namespace. For example:
#include <iostream>
[COLOR="red"]using std::cout;
using std::cin;
using std::getline;[/COLOR]
#include <string>
[COLOR="red"]using std::string;[/COLOR]

int main()
{
   string line;
   cout << "enter a line of text: ";
   getline(cin, line);
   cout << "text entered: \"" << line << "\"\n";
}

/* my output
enter a line of text: hello world
text entered: "hello world"
*/
In the above example, I have chosen to group together the using directives under the header in which the name is found. This is not a necessity, I just do it to help me remember where things come from or as an explanation of why I have #included a particular header.

Alternatively, a using directive can bring in all identifiers in a namespace.
#include <iostream>
#include <string>
[COLOR="Red"]using namespace std;[/COLOR]

int main()
{
   string line;
   cout << "enter a line of text: ";
   getline(cin, line);
   cout << "text entered: \"" << line << "\"\n";
}

/* my output
enter a line of text: hello world
text entered: "hello world"
*/
Beginners are often shown this method because of its simplicity. But in entirely defeats the purpose of a namespace in trying to avoid bugs and build errors (or, more specifically, naming collisions). I remember chasing one down that was related to having a variable named "count". If you want to avoid such potential problems, avoid using namespace std;.

So using namespace std; may be a quick and dirty workaround for "toy" code, but you really don't want to use it for any "real" code. You especially want to avoid using an entire namespace in a header file.

Edited by dcs, 15 February 2010 - 03:59 PM.


#2
genux

genux

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
Nice tut :)

int coffeePerDay = 10; // need to cut down!!!

Codingfriends

#3
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
I always wondered what that does >.> I don't usually code in C++ unless someone needs help.
Thanks alot !

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Very well written :thumbup:
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
very useful :)