Thread: std namespace

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    29

    std namespace

    Quick Questions:

    When and why should you use the īstdī namespace ?
    What is the difference in writing std::cout and cout ?
    Is the std namespace only used with the STL ?

    Thanks,

    bigSteve
    bigSteve

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >When and why should you use the īstdī namespace ?
    When: When you need to use a name in the standard library. Otherwise you would have trouble since they are all nested inside the std namespace.

    Why: Because the C++ standard says so, and forces you to if you want to use the standard library.

    >What is the difference in writing std::cout and cout ?
    std::cout is guaranteed to work. cout may or may not work depending on whether or not there is a using directive or declaration visible. One of the two following:
    Code:
    using namespace std;
    using std::cout;
    >Is the std namespace only used with the STL ?
    No, the std namespace is used with the whole of the standard library.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    Hmmm.....I thought the std namespace was already defined in the header file.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Prelude
    Why: Because the C++ standard says so, and forces you to if you want to use the standard library.
    I've been wondering why they decided to go this route? Why make the standard library harder/confusing to use? Was it to alleviate problems if you wish to define your own components with the same name as a replacement?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I've been wondering why they decided to go this route?
    The C++ standard library has a lot of names and most programmers aren't aware of them all. This creates a big risk for collisions. That is the primary reason why everything is in the std namespace. The committee debated whether or not to use one big namespace or several, but ended up deciding that splitting up the library into several namespaces wouldn't be an advantage over a single std namespace mainly because it would require the programmer to remember where everything came from, and it would be very chaotic. And how would you structure a namespace hierarchy without changing the structure of the library? The way I see it, there is no reasonable solution to that problem, so a single std namespace was used.
    My best code is written with the delete key.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    So as I surmised it's to allow "collision" names or to easily rewrite a module/method and not have to rename it.

    Thanks for the explanation. Makes sense, at least to a degree.


    One thing came to mind while reading your explanation:
    splitting up the library into several namespaces wouldn't be an advantage over a single std namespace mainly because it would require the programmer to remember where everything came from
    Like stdio.h, stdlib.h, string.h, as nauseum
    Last edited by WaltP; 01-19-2004 at 09:50 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    317
    Or to put it another way, namespaces are just another way to define scope. It really is as simple as that.

    As for my comment above, in the header file for iostream (the one provided with MS software) they place the std namespace into scope with the using namespace std; declaration. Thus you do not need to make this statement again in your code.

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by Traveller
    As for my comment above, in the header file for iostream (the one provided with MS software) they place the std namespace into scope with the using namespace std; declaration. Thus you do not need to make this statement again in your code.
    Sometimes compilers put that into the old headers (e.g. <iostream.h>) along with an include of the new header to make it appear as though they are backwards compatible with the non-standard libraries. However, it is probably a better idea to use the standard headers (e.g. <iostream>) and not count on the using declaration being there because a) it was bad form (or maybe non-standard) to put it into the new header if an implementation actually did that; and b) you can't count on that happening with any other library implementation.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by Prelude
    std::cout is guaranteed to work. cout may or may not work depending on whether or not there is a using directive or declaration visible. One of the two following:
    Code:
    using namespace std;
    using std::cout;
    And even then, of course, they're not guaranteed to 'work' because of the possibility of name clashes. Only a namespace prefix gives you any kind of guarantee.

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