Quick question about std

This is a discussion on Quick question about std within the C++ Programming forums, part of the General Programming Boards category; In my classes in C++ so far we have been using Code: using namespace std; I see other examples of ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Post Quick question about std

    In my classes in C++ so far we have been using
    Code:
    using namespace std;
    I see other examples of code where certain keywords are prefaced with std: instead of using the above declaration. Why is this; and what is the difference between the two? Are they interchangeable?

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The
    Code:
    using namespace std;
    statement tells the compiler "if you can find a name in the namespace std, use it".

    Using
    Code:
    std::cout << x;
    doesn't let the compiler use anything else out of the "std" namespace. Simply put, it's more restrictive.

    You can also use:
    Code:
    using std::cout;
    to tell the compiler that "if you need a cout, you may use the one out of the std namespace".

    Namespaces in general are intended to avoid "name-collisions" - if you have two items (functions, classes, structs, varables or whatever it may be) with the same name, the compiler [or linker] can potentially pick up either. Namespaces allow us to separte two different "names" that are otherwise the same.

    It also can allow us to have alternative versions of something with only the namespace deciding which you get.

    In most cases, it's probably completely equal which one we choose to use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    if u dont put "using namespace std;" on top, then you will have to put "std::" in everything below, or the compiler cant recognize the cout<< and other things.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by eastmus View Post
    if u dont put "using namespace std;" on top, then you will have to put "std::" in everything below, or the compiler cant recognize the cout<< and other things.
    True - if you use anything that is part of the standard library, then you must tell the compiler that you want it to look for things in std namespace one way or another. For relatively small programs, it's usually "best" to use "using namespace std", whilst in a large program it may be better to avoid that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    There's been interesting discussions about this here before. Search for using namespace std and read through some of the longer threads to get a selection of opinions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 03:54 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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