Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

using namespace std;

hello world namespace

  • Please log in to reply
No replies to this topic

#1 dcs

dcs

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 730 posts

Posted 15 February 2010 - 10:52 AM

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.

  • 0





Also tagged with one or more of these keywords: hello world, namespace

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download