using namespace std; or using std::whatever

Discussion in 'C++' started by patrick@kirks.net, Oct 2, 2006.

  1. Guest

    Why do some code listings for learning C++ have the entire namespace
    std being used while others just specify the parts they want to use?
    , Oct 2, 2006
    #1
    1. Advertising

  2. Howard Guest

    <> wrote in message
    news:...
    > Why do some code listings for learning C++ have the entire namespace
    > std being used while others just specify the parts they want to use?
    >


    You'd really have to ask the person who wrote the code, if you want to know
    why they wrote it that way. Personally, I never write "using namespace
    std;", because it's overkill. Why do I need the netoire std namespace?
    Often, I just need string, cin, cout, and endl. I sometimes write "using
    std::cout;" or similar using statements. Other times I just specify the
    std:: with whatever I'm using from it. It all depends on how readable it
    makes the code, and how much it's really saving me to use a using statement
    at all.

    -Howard
    Howard, Oct 2, 2006
    #2
    1. Advertising

  3. mlimber Guest

    wrote:
    > Why do some code listings for learning C++ have the entire namespace
    > std being used while others just specify the parts they want to use?


    It's a matter of preference in most cases. The rule from _C++ Coding
    Standards_, item 59 is (italics in original): "You can and should use
    namespace using declarations and directives liberally /in your
    implementation files after #include directives/ and feel good about it.
    Despite repeated assertions to the contrary, namespace using
    declarations and directives are not evil and they do not defeat the
    purposes of namespaces. Rather, they are what make namespaces usable."

    Cheers! --M
    mlimber, Oct 2, 2006
    #3
  4. Howard Guest

    "Howard" <> wrote in message
    news:6TcUg.173946$...
    >
    > <> wrote in message
    > news:...
    >> Why do some code listings for learning C++ have the entire namespace
    >> std being used while others just specify the parts they want to use?
    >>

    >
    > You'd really have to ask the person who wrote the code, if you want to
    > know why they wrote it that way. Personally, I never write "using
    > namespace std;", because it's overkill. Why do I need the netoire std
    > namespace?


    "netoire"??? Where the $#%#% did THAT come from? Dyslexic fingers, I
    guess. (Sounds French somehow, don't it?) I meant "entire", as in "Why do
    I need the ENTIRE std namespace?"

    :-}

    (By the way, I DO use entire namespaces sometimes, when the namespace isn't
    as huge as std - especially if it's my own namespace.)

    -Howard
    Howard, Oct 2, 2006
    #4
  5. Guest

    mlimber wrote:
    >
    > It's a matter of preference in most cases. The rule from _C++ Coding
    > Standards_, item 59 is (italics in original): "You can and should use
    > namespace using declarations and directives liberally /in your
    > implementation files after #include directives/ and feel good about it.
    > Despite repeated assertions to the contrary, namespace using
    > declarations and directives are not evil and they do not defeat the
    > purposes of namespaces. Rather, they are what make namespaces usable."
    >
    > Cheers! --M


    Interesting.

    So I use using namespace std; I lose nothing compared to the chap who
    careful typed using std::string; using std::cin, and so on? There is
    no security or performance price to pay?
    , Oct 2, 2006
    #5
  6. mlimber Guest

    wrote:
    > mlimber wrote:
    > >
    > > It's a matter of preference in most cases. The rule from _C++ Coding
    > > Standards_, item 59 is (italics in original): "You can and should use
    > > namespace using declarations and directives liberally /in your
    > > implementation files after #include directives/ and feel good about it.
    > > Despite repeated assertions to the contrary, namespace using
    > > declarations and directives are not evil and they do not defeat the
    > > purposes of namespaces. Rather, they are what make namespaces usable."
    > >
    > > Cheers! --M

    >
    > Interesting.
    >
    > So I use using namespace std; I lose nothing compared to the chap who
    > careful typed using std::string; using std::cin, and so on? There is
    > no security or performance price to pay?


    Well, using namespace xxx will pull in *all* the symbols from that
    namespace, which could lead to name collisions, but this is generally
    evident at compile-time. For an example, see this post:

    http://groups.google.com/group/comp.lang.c /msg/3544f38ad8a18a20

    Cheers! --M
    mlimber, Oct 2, 2006
    #6
  7. Renato Golin Guest

    mlimber wrote:
    >> So I use using namespace std; I lose nothing compared to the chap who
    >> careful typed using std::string; using std::cin, and so on? There is
    >> no security or performance price to pay?

    >
    > Well, using namespace xxx will pull in *all* the symbols from that
    > namespace, which could lead to name collisions, but this is generally
    > evident at compile-time. For an example, see this post:


    I generally use "using namespace std" myself partly because I prefer
    using cout instead of std::cout and partly because I'm used to.

    I never had collision problems with my own classes as I'm a bit careful
    when choosing my class names and I think that's the point.

    If you get collision between std and other namespaces and you do
    have/want to "using std" and "using other" you should be specific only
    on that case.

    --renato

    --
    Reclaim your digital rights, eliminate DRM, learn more at
    http://www.defectivebydesign.org/what_is_drm
    Renato Golin, Oct 3, 2006
    #7
  8. wrote:
    > mlimber wrote:
    >
    >>It's a matter of preference in most cases. The rule from _C++ Coding
    >>Standards_, item 59 is (italics in original): "You can and should use
    >>namespace using declarations and directives liberally /in your
    >>implementation files after #include directives/ and feel good about it.
    >>Despite repeated assertions to the contrary, namespace using
    >>declarations and directives are not evil and they do not defeat the
    >>purposes of namespaces. Rather, they are what make namespaces usable."
    >>
    >>Cheers! --M

    >
    >
    > Interesting.
    >
    > So I use using namespace std; I lose nothing compared to the chap who
    > careful typed using std::string; using std::cin, and so on? There is
    > no security or performance price to pay?


    You pay the price if you are "using namespace" within a
    _header_ file (bad practice). This way you force everyone
    who #includes your header file to pull in all the namespace
    symbols. Therefore, as long as header files are concerned
    you should take the effort to type in "std::string" etc
    and not be "using namespace";
    As far as .cpp files are concerned, the first line I type
    after the #includes is usually "using namespace std;"

    HTH,
    - J.
    Jacek Dziedzic, Oct 3, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zeljko

    Template ? Or whatever...

    Zeljko, Nov 21, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    299
    Saravana [MVP]
    Nov 22, 2003
  2. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,344
    Howard
    Nov 29, 2004
  3. bb
    Replies:
    1
    Views:
    294
    Alf P. Steinbach
    Sep 7, 2007
  4. RichardOnRails
    Replies:
    4
    Views:
    350
    RichardOnRails
    May 28, 2011
  5. Shriramana Sharma
    Replies:
    2
    Views:
    144
    Shriramana Sharma
    Jun 27, 2013
Loading...

Share This Page