using namespace std

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Halcyon

    #1

    using namespace std

    so i've been "using namespace std" happily in all my source files at
    the global scope, and i then go to to use cout, vector, string etc
    without having to use std:: everytime. However today i was informed
    that i should eschew "using namespace std" and use "using std::string",
    "std::vecto r" et al.

    I don't quite get what the potential drawbacks of "using namespace std"
    are...
    i perused a few books, but most authors *do* use "using namespace std",
    and the person who asked me to make the change wouldn't explain.

    I was under the impression that the using keyword just lets the
    compiler know where to look...
    i mean, would it make a difference in code size or anything?

    cheers,
    G

  • Alan Johnson

    #2
    Re: using namespace std


    Halcyon wrote:
    so i've been "using namespace std" happily in all my source files at
    the global scope, and i then go to to use cout, vector, string etc
    without having to use std:: everytime. However today i was informed
    that i should eschew "using namespace std" and use "using std::string",
    "std::vecto r" et al.
    >
    I don't quite get what the potential drawbacks of "using namespace std"
    are...
    i perused a few books, but most authors *do* use "using namespace std",
    and the person who asked me to make the change wouldn't explain.
    >
    I was under the impression that the using keyword just lets the
    compiler know where to look...
    i mean, would it make a difference in code size or anything?
    >
    cheers,
    G
    Please see section 27.5 of this group's FAQ:



    --
    Alan Johnson

    Comment

    • Gary Wessle

      #3
      Re: using namespace std

      "Halcyon" <gaurang.sardes ai@gmail.comwri tes:
      so i've been "using namespace std" happily in all my source files at
      the global scope, and i then go to to use cout, vector, string etc
      without having to use std:: everytime. However today i was informed
      that i should eschew "using namespace std" and use "using std::string",
      "std::vecto r" et al.
      >
      I don't quite get what the potential drawbacks of "using namespace std"
      are...
      i perused a few books, but most authors *do* use "using namespace std",
      and the person who asked me to make the change wouldn't explain.
      >
      I was under the impression that the using keyword just lets the
      compiler know where to look...
      i mean, would it make a difference in code size or anything?
      >
      cheers,
      G
      take a look at vol.1 pages
      91
      414
      757
      and others from the inedx, it is a e-book for free, I bought the hard
      copy and it is very good.

      Comment

      • Halcyon

        #4
        Re: using namespace std


        Alan Johnson wrote:
        Thanks for that. I missed it.
        --
        Alan Johnson

        Comment

        • Mark P

          #5
          Re: using namespace std

          Halcyon wrote:
          so i've been "using namespace std" happily in all my source files at
          the global scope, and i then go to to use cout, vector, string etc
          without having to use std:: everytime. However today i was informed
          that i should eschew "using namespace std" and use "using std::string",
          "std::vecto r" et al.
          >
          I don't quite get what the potential drawbacks of "using namespace std"
          are...
          i perused a few books, but most authors *do* use "using namespace std",
          and the person who asked me to make the change wouldn't explain.
          >
          I was under the impression that the using keyword just lets the
          compiler know where to look...
          i mean, would it make a difference in code size or anything?
          >
          cheers,
          G
          >
          First of all, I'd be leery of anyone who tells you never to do something
          and can't explain why. Second, "using namespace std" is OK provided the
          scope of such a directive is limited. The general rule of thumb that
          follows from this is that it's OK in a .cpp file but should not be used
          in a .h file.

          What's the difference you ask? A .h file may be #included all over the
          place and anyone who #includes your .h file would, perhaps unwittingly,
          also include the using directive.

          And what's so bad about that, you ask? Well you may be content to have
          cout mean std::cout and vector mean std::vector, but others may not feel
          the same way. If someone else has a piece of code where they've defined
          their own vector class-- perhaps a linear algebra package-- and they
          include your header file (including using namespace std; and #include
          <vector>) the compiler will complain about an ambiguous name.

          Comment

          • Halcyon

            #6
            Re: using namespace std


            Mark P wrote:
            >
            First of all, I'd be leery of anyone who tells you never to do something
            and can't explain why.
            >Second, "using namespace std" is OK provided the
            scope of such a directive is limited. The general rule of thumb that
            follows from this is that it's OK in a .cpp file but should not be used
            in a .h file.
            right, that's what i gleaned. But considering that fact that i wrote
            the .cpp file, and i know what i'm doing, it should definitely be safe
            in my .cpp file.

            however, on reading the various arguements, i think i'm going to play
            it safe and stick to using std::cout/std::string/std::vector.
            >
            What's the difference you ask? A .h file may be #included all over the
            place and anyone who #includes your .h file would, perhaps unwittingly,
            also include the using directive.
            >
            And what's so bad about that, you ask? Well you may be content to have
            cout mean std::cout and vector mean std::vector, but others may not feel
            the same way. If someone else has a piece of code where they've defined
            their own vector class-- perhaps a linear algebra package-- and they
            include your header file (including using namespace std; and #include
            <vector>) the compiler will complain about an ambiguous name.
            thanks for the lucid explanation.

            cheers,
            G

            Comment

            • Daniel T.

              #7
              Re: using namespace std

              In article <1155081284.781 653.201180@75g2 000cwc.googlegr oups.com>,
              "Halcyon" <gaurang.sardes ai@gmail.comwro te:
              so i've been "using namespace std" happily in all my source files at
              the global scope, and i then go to to use cout, vector, string etc
              without having to use std:: everytime. However today i was informed
              that i should eschew "using namespace std" and use "using std::string",
              "std::vecto r" et al.
              >
              I don't quite get what the potential drawbacks of "using namespace std"
              are...
              i perused a few books, but most authors *do* use "using namespace std",
              and the person who asked me to make the change wouldn't explain.
              >
              I was under the impression that the using keyword just lets the
              compiler know where to look...
              i mean, would it make a difference in code size or anything?
              The book "C++ Coding Standards" by Sutter and Alexandrescu specifically
              OK what you are doing.

              There really is no reason not to put "using namespace std" in your cpp
              file after all your includes, unless you aren't using anything from the
              standard namespace of course. You will notice that the reason everyone
              (who says not to do it) says not to do it, is that your code may not
              compile. If it does compile, what's the problem? If it doesn't, because
              of some ambiguity, just qualify the ambiguous names and it will.

              Comment

              Working...