Re: what does using namespace std mean?

Discussion in 'C++' started by Sean Fraley, Aug 21, 2003.

  1. Sean Fraley

    Sean Fraley Guest

    soni29 wrote:

    > hi,
    > i did some c++ back in college, currently work mainly with java, and
    > i'm trying to refresh myself in c++, after starting a tutorial online
    > i noticed that the author used the line using namespace std. what is
    > that? i've never seen that in college or in my c++ text. i see the
    > site has the #include<iostream.h> to bring in cin and cout but not
    > sure why the std line is there. the code the site had is:
    > #include <iostream.h>
    > #include <string>
    > using namespace std;
    > int main() {
    > string name;
    > cout << "What is your name?";
    > cin >> name;
    > cout << "Welcome, " << name << ".";
    > return 0;
    > }
    > Thank you.

    When C++ was standardized by ANSI/ISO, some new features were introduced,
    namespaces being one of them. They are basically a way to prevent
    conflicts between items from different libraries having the same name. For
    a detailed explanation, look up a couple of web sites.

    With the introduction of namespaces into the standard, some changes were
    also made to the preferred methods of including headers from the standard
    library. The main issues are as follows:

    1) When including items from the standard library, don't use the ".h" file
    suffix anymore. Just use the name of the file without the suffix.

    example: #include <iostream.h>
    #include <iostream>

    2) When including file that were part of the Standard C Library, put a 'c'
    at the beginning of the name and drop the ".h" suffix.

    example: #include <stdlib.h>
    #include <cstdlib>

    3) The old method of including standard library headers is still perfectly
    functional, since the current standard has the old ones around for backwards
    compatibility. If the standard changes, and allows for the old ones to be
    removed, then code that uses the old method will fail to compile on a
    standard compliant compiler.

    4) This only applies to standard library files. Includes from your own
    project, and those from other libraries still use the old method.

    As for the "using namespace std" line:

    All of the contents of the standard library are part of the namespace "std".
    To use the components of the library, you need to tell the compiler so.
    That is what the "using" keyword is for. "using namespace std" is telling
    the compiler that you are using everything in the std namespace. Keep in
    mind that if you use the new method of including standard library files,
    compiling will fail if you don't do this. There is a bit more to the use
    of the "using" keyword than this, and can be found in other sources.
    Sean Fraley, Aug 21, 2003
    1. Advertisements

  2. Sean Fraley wrote:

    > 3) The old method of including standard library headers is still perfectly
    > functional, since the current standard has the old ones around for backwards
    > compatibility.

    This is incorrect. <iostream.h>, for example is *not* part of the C++
    Standard. The only headers that are part of the standard and end with .h
    are those that were inherited from C, and they are deprecated in favor
    of the headers that add a 'c' on the front and drop the '.h'.

    Sean, why did you set the "followup-to" field to some random thing? That
    makes replying kind of difficult.

    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 21, 2003
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter van Merkerk

    Re: what does using namespace std mean?

    Peter van Merkerk, Aug 21, 2003, in forum: C++
    Aug 22, 2003
  2. Dave O'Hearn
    Agent Mulder
    Aug 23, 2003
  3. Petter Reinholdtsen
    Nov 29, 2004
  4. Replies:
    Jacek Dziedzic
    Oct 3, 2006
  5. Jeffrey Walton
    Mathias Gaunard
    Nov 26, 2006

Share This Page