using namespace std;

This is a discussion on using namespace std; within the C++ Programming forums, part of the General Programming Boards category; hey guys, can someone explain the difference of declaring namespace right below the library Code: #include <iostream> using namespace std; ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    using namespace std;

    hey guys, can someone explain the difference of declaring namespace right below the library
    Code:
    #include <iostream>
    using namespace std;
    and using the std:: in the code it self?
    Code:
    std::cout << "Hello, World!" << std::endl;
    is this compiler specific, and is there a standard? I have two books, "Absolute C++" by Savitch whitch teaches the decleration of 'using namespace std;' and "Accelerated C++" by Koenig and Moo whitch starts of with std::

    thankx,

    axon

    ----edit----
    I use VC++ 6.0, and both ways work, but the authors of accelerated c++ say that things such as
    Code:
    using std::vector
    vector<double>::size_type n = 0;
    does not work, and displays and error message
    Last edited by axon; 03-05-2003 at 04:53 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    there really is no difference to std:: and using namespace std;

    however, a difference does lie in which fxns can be used. when you said using namespace std; you can just write cout, cin, endl, etc. as you normally would. std:: means i am going to use this fxn from the std namespace. the using namespace std; line is like saying, give me access to use all the fxns in the std namespace, but I'll only use these fxns. it is programmer dependent. it is compiler independent.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >there really is no difference to std:: and using namespace std;
    On the contrary, using namespace std pollutes the global namespace by opening everything in std even if you do not use it while the std:: prefix keeps things nice and tidy, you only get what you ask for. If you don't want to type the prefix in all of the time but don't like pollution (I don't blame you), you can use this variant:
    Code:
    #include <iostream>
    
    using std::cout; // I'm going to use cout
    using std::endl; // and endl, but nothing else from std
    
    int main()
    {
      cout<<"Test"<<endl;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Looks like you have some crapy books. What you've already deferred is correct however - that is, you can use the scope resolution operator "::" on namespaces or just declare that you are "using" a particular namespace. As for standard C++, the VC++ 6.0 compiler isn't the most conforming (not by a long shot), but it has namespacing down (almost, but that's another topic).
    The reason namespaces were added to the standard was to avoid name collisions....so you can use both:
    Code:
    codeplugs_crazy_vector::vector
    and
    Code:
    std::vector
    gg

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    thanks for the replies, but is there an "industry standard"?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    Originally posted by Codeplug
    Looks like you have some crapy books.
    from my understanding these two books are probably the best for beginners as well as advanced programmers...even some highly regarded members of this board recommended them.

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    ah, yes Prelude, i meant to delete that line after i posted the stuff below it. Thanks for the info.

    but is there an "industry standard"?
    not as far as i know.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Exactly what did they say "does not work" and what is the error message?

    using std::vector

    should make vector available anyplace after the directive appears without having to prefix it with "std::" (within that scope).

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Yes, "crappy books" was too harsh. All books are bound to have errors in them (no pun intended, or was it?).

    gg

  10. #10
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    Originally posted by R.Stiltskin
    Exactly what did they say "does not work" and what is the error message?
    here is the direct error:

    Code:
    vector<double, class std::allocator<double> > : is not a class or namespace name
    and here is their solution around it:
    Code:
    using std::vector; 
    
    #ifdef _MSC_VER 
    std::vector<double>::size_type n = 0; 
    #else 
    vector<double>::size_type n = 0; 
    #endif
    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    It looks like that "workaround" is specifically for Microsoft compilers.

    The explicit scope resolution std::vector... is only being used if _MSC_VER has been defined, i.e. a Microsoft header file is being included.

    When a non-Microsoft compiler is used, that code compiles as just vector<double>::size_type n = 0;
    which is supposed to work as long as using std::vector precedes it in the current scope.

    I guess the answer to your original question is, yes there is a standard (standard C++) but apparently Microsoft doesn't adhere to it.

    Anyway, none of that refers to the directive
    using namespace std;
    which as far as I have seen, works fine with all C++ compilers, even Microsoft's.

    But it would be interesting to hear what the experienced programmers have to say about this.

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, 11:30 AM
  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, 01:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21