Results 1 to 8 of 8

Thread: Why 'std::' vs. 'using namespace std'?

  1. #1
    Join Date
    Sep 2006
    Location
    BC, Canada
    Beans
    347
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Why 'std::' vs. 'using namespace std'?

    Another WHY question. I've read conflicting reports but it is generally agreed that...

    Code:
    using namespace std;
    ...is bad practice and you should use the std:: prefix whenever invoking a std namespace function like cout or the string class. Example:

    Code:
    std::string str = "Some string.";
    
    std::cout << str << std::endl;
    My question is, why? What are the advantages of the 'std::' prefix or the pitfalls of 'using namespace std'?

  2. #2
    Join Date
    Oct 2007
    Beans
    1

    Re: Why 'std::' vs. 'using namespace std'?

    I think it's more a matter of personal preference rather than a rule or good/bad practice. Typing std:: all the time gets pretty boring though. Wouldn't you rather type "using namespace std" once rather than sprinking std:: everywhere? That's what I like to do anyway.


    SM.

  3. #3
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: Why 'std::' vs. 'using namespace std'?

    The C++ FAQ lite covers this topic.

    Additionnaly, prefixing with std:: allows one to distinguish at a glance what parts of the program use the standard library, and what parts use "custom" code.
    Not even tinfoil can save us now...

  4. #4
    Join Date
    Jun 2006
    Location
    St. Louis, MO
    Beans
    60

    Re: Why 'std::' vs. 'using namespace std'?

    There are many different styles here that are all valid, but there is one RULE:

    Do NOT put "using namespace xxxx" in a header file. PERIOD.

    I personally like to always be explicit and put std::string instead of string, but it is perfectly fine to have "using namespace std;" in an implementation file. While I don't do it myself, it is not bad style at all in my opinion.

    The reason you shouldn't ever put using namespace xxx in a header file is that it makes a conflict later much more likely. If you use a namespace and then another header file after that uses a namespace with some of the same names and references one of those functions then the code might not compile, but only when including both headers.... which is obviously a really unfortunate bug. This is why you don't use a namespace in a header. Implementation is different though because you don't include implementation files (.cpp, etc...)

    I'm sure there are varying opinions, but these guidelines are certainly not wrong.

  5. #5
    Join Date
    Nov 2006
    Location
    Israel
    Beans
    765
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Why 'std::' vs. 'using namespace std'?

    The idea is to not overrun your global function namespace with functions. If you don't care, you can use "using namespace std;". That is really all there is to it as far as I know.
    Intel E6300 / MSI P4M890M / 2GB DDR2 677 / 80GB + 1TB SATA2 / GeForce 6200TC / DL DVD+-RW / Dell 24" U2410

  6. #6
    Join Date
    Dec 2006
    Location
    Australia
    Beans
    1,097
    Distro
    Xubuntu 15.10 Wily Werewolf

    Re: Why 'std::' vs. 'using namespace std'?

    You can also use
    Code:
    using namespace_name::member;
    to specify which member of that namespace to use.

    Example:
    Code:
    using std::cout;
    using std::cin;
    using std::endl;
    Will allow you to use cout, cin, and endl, without specifying std:: in front of every usage.

  7. #7
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,284
    Distro
    Kubuntu

    Re: Why 'std::' vs. 'using namespace std'?

    Since this post is on the topic of namespaces, can someone refresh my memory on how to forward-declare an std:: object (within a header file) in lieu of including the header file that defines it?

    I want to believe it's something like this, but I'm not sure:

    Code:
    #ifndef FOO_H
    #define FOO_H
    namespace std
    {
      class string;
    };
    
    ...
    #endif
    Last edited by dwhitney67; October 29th, 2007 at 03:00 AM.

  8. #8
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Why 'std::' vs. 'using namespace std'?

    You can't. The problem is they are not classes, but typedefs of templates. http://www.gotw.ca/gotw/034.htm

    The STL writers do provide
    <iosfwd>, but that is only for iostream. No others.
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •