Thread: using namespace std;

  1. #1
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

    using namespace std;

    Code:
    // This is the Hello World program (HelloWorld.cpp)
    // Note: You should always start with a description of your program here.
    // Written by: Colin Goble
    // Note: Please put your name here or both names if you are working in a pair.
    // Date: 19 June 2005
    // Note: Put the current date here
    // Sources: None
    // Note: Always cite all sources you may have used, such as other students you
    // may have had help from, web sites you may have referenced, and so forth.
    // If none, write None.
    
    // The next few lines are standard boilerplate you will need...
    // ...in front of virtually every C++ program you will write this term.
    
    #include <iostream>                 //Required if your program does any I/O
    
    using namespace std;                //Required for ANSI C++ 1998 standard.
    
    int main ()
    {
        char reply;
        cout << "Hello World" << endl;
        
        // This section stops the program 'flashing' off the screen.
        cout << "Press q (or any other key) followed by 'Enter' to quit: ";
        cin >> reply;
    	return 0;
    }
    I have never heard of using namespace std; being required for any standards... Anyone else?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I think it means that this:
    Code:
    cout << "Hello World" << endl;
    requires that you use the std namespace. As opposed to pre-C++98 stuff in which cout was not in the std namespace.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes. The comment is wrong.

    With the new (c++ 98) headers, as Dave explained, the std namespace was introduced to the standard library. Three techniques are used to reach these objects.

    (in order of preference)
    1. Explicit access (for example std::cout << "hello world" << std::endl;)
    2. Using declarations (for example using std::cout; )
    3. Using directives (for example using namespace std; )

    Of the three techniques, it is in fact the third that is the least recommended and may eventually lead to name clashes. Whereas the first technique is the fail safe one and most recommended. As such, that comment seems to endorse the use of the worst of the three techniques.

    What the comment is trying to tell you is that you do need to access iostream objects (and other objects from other headers) through the std namespace in order to comply with the C++ standards. You do this by defining your headers in the new style (between < and >, and without the h extension).
    Last edited by Mario F.; 06-26-2006 at 12:35 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Oh I know what using namespace std; does, just never heard of it being a requirement...

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well, it is a requirement... for compliance with the standards
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    The namespace is required, but that statement
    Code:
    using namespace std;
    is not required.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Location
    Hungary
    Posts
    4
    As far as I know, using namespace std is required if you want to use the C++ functions of the <iostream> library (like cin, cout, cerr). If you just use C functions like printf() or scanf(), you don't have to type it in.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Please read what has been said before. Not. It's not required.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Location
    Hungary
    Posts
    4
    Sorry!
    I just wanted to help.

  10. #10
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    well, if u didnt use the statement/command: using namespace std; u would have to put it at the start of ur statements.
    example:
    Code:
    cout << (would become:)
    std: :cout<<
    if i have posted anything wrong, let me no
    Last edited by beene; 07-08-2006 at 11:31 AM.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Beene, please don't answer an old thread with something that was not asked. But worst, with an answer that is wrong.

    std: : cout<<
    is malformed.

    the scope operator has no space between each colon.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    oh, sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why std:: and not using namespace std?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2009, 03:10 PM
  2. std:: or namespace std
    By C-+ in forum C++ Programming
    Replies: 16
    Last Post: 12-04-2007, 12:30 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. need ; before using namespace std
    By wwfarch in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2004, 10:42 PM
  5. Site tutorials
    By Aalmaron in forum C++ Programming
    Replies: 20
    Last Post: 01-06-2004, 02:38 PM