Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. 'Should I use <code>using namespace std;</code> in my code?'

'Should I use <code>using namespace std;</code> in my code?'

Scheduled Pinned Locked Moved C / C++ / MFC
15 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stefan_Lang
    wrote on last edited by
    #1

    I've put the topic in quotes, because I'm not asking for me. Instead I am referring to the various occasions I've seen where people advise inexperienced programmers to add

    using namespace std;

    to their program. Personally I feel that this is not generally a good advice, for all of the reasons pointed out at http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5[^]. But maybe the people offering this advice do have their reasons and take a different view? I'd like to know. What is your stance? Why do you think it is good to add a

    using namespace

    statement, and when? Or do you agree with me and generally advise against it? If so, can you offer any reasons not already offered at the site I linked above? P.S.: After reading the responses I've found there are indeed (IMHO) few strong arguments in favor of using using namespace std;. The two main arguments were: 1. Typing effort 2. Lack of readability, most notably (but not only) with the use of formatting in stream classes Personally I don't think the typing effort should be considered a valid reason, unless there are absolutely no drawbacks to consider. Apart from that, both cases can be countered by various methods: 1. Use intelligent editors to minimize the typing effort 2. Use typedef to simplify complex (or just hard to type) expressions 3. Use a using declaration (e. g. using std::cout;) rather than a using directive In those rare cases where, locally, symbols from namespace std are being heavily used (e. g. an output function using lots of formatting), adding a using directive locally (i. e. within the scope of the function only) could be a reasonable compromise. Thanks to Stephen Hewitt for pointing that out.

    L N C S 4 Replies Last reply
    0
    • S Stefan_Lang

      I've put the topic in quotes, because I'm not asking for me. Instead I am referring to the various occasions I've seen where people advise inexperienced programmers to add

      using namespace std;

      to their program. Personally I feel that this is not generally a good advice, for all of the reasons pointed out at http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5[^]. But maybe the people offering this advice do have their reasons and take a different view? I'd like to know. What is your stance? Why do you think it is good to add a

      using namespace

      statement, and when? Or do you agree with me and generally advise against it? If so, can you offer any reasons not already offered at the site I linked above? P.S.: After reading the responses I've found there are indeed (IMHO) few strong arguments in favor of using using namespace std;. The two main arguments were: 1. Typing effort 2. Lack of readability, most notably (but not only) with the use of formatting in stream classes Personally I don't think the typing effort should be considered a valid reason, unless there are absolutely no drawbacks to consider. Apart from that, both cases can be countered by various methods: 1. Use intelligent editors to minimize the typing effort 2. Use typedef to simplify complex (or just hard to type) expressions 3. Use a using declaration (e. g. using std::cout;) rather than a using directive In those rare cases where, locally, symbols from namespace std are being heavily used (e. g. an output function using lots of formatting), adding a using directive locally (i. e. within the scope of the function only) could be a reasonable compromise. Thanks to Stephen Hewitt for pointing that out.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Well, if you have lots of code using funcs only form one namespace in a file, then you can reduce the amount of typing you have to do by stating 'using namespace' at the top of the file. If you are not, and you are mixing lots of code from different namespaces, then you shouldnt, because you might have a clash somewhere, and either compiler errors or runtime errors.

      ============================== Nothing to say.

      S 1 Reply Last reply
      0
      • S Stefan_Lang

        I've put the topic in quotes, because I'm not asking for me. Instead I am referring to the various occasions I've seen where people advise inexperienced programmers to add

        using namespace std;

        to their program. Personally I feel that this is not generally a good advice, for all of the reasons pointed out at http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5[^]. But maybe the people offering this advice do have their reasons and take a different view? I'd like to know. What is your stance? Why do you think it is good to add a

        using namespace

        statement, and when? Or do you agree with me and generally advise against it? If so, can you offer any reasons not already offered at the site I linked above? P.S.: After reading the responses I've found there are indeed (IMHO) few strong arguments in favor of using using namespace std;. The two main arguments were: 1. Typing effort 2. Lack of readability, most notably (but not only) with the use of formatting in stream classes Personally I don't think the typing effort should be considered a valid reason, unless there are absolutely no drawbacks to consider. Apart from that, both cases can be countered by various methods: 1. Use intelligent editors to minimize the typing effort 2. Use typedef to simplify complex (or just hard to type) expressions 3. Use a using declaration (e. g. using std::cout;) rather than a using directive In those rare cases where, locally, symbols from namespace std are being heavily used (e. g. an output function using lots of formatting), adding a using directive locally (i. e. within the scope of the function only) could be a reasonable compromise. Thanks to Stephen Hewitt for pointing that out.

        N Offline
        N Offline
        Niklas L
        wrote on last edited by
        #3

        I personally almost never use it, simply because it clutters the global namespace. The worst abuse is to put it in a header file, and thus, polluting the global namespace in all files including that header. I imagine though it can be useful in some situations, like when dealing with streams and manipulators. The code tends to get quite verbose if it's fully qualified. A local using namespace could be acceptable in my oppinion. Good topic by the way.

        S 1 Reply Last reply
        0
        • N Niklas L

          I personally almost never use it, simply because it clutters the global namespace. The worst abuse is to put it in a header file, and thus, polluting the global namespace in all files including that header. I imagine though it can be useful in some situations, like when dealing with streams and manipulators. The code tends to get quite verbose if it's fully qualified. A local using namespace could be acceptable in my oppinion. Good topic by the way.

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          Niklas Lindquist wrote:

          when dealing with streams and manipulators

          Yes, I do find myself writing std::cout and std::endl quite a lot, but so far I've resisted the urge to add a using statement. However, you could insert a using declaration instead of a using directive for the most commonly used symbols, as pointed out at C++ FAQ.

          Niklas Lindquist wrote:

          The code tends to get quite verbose

          Are you implying this is a bad thing? I actually find that helpful. Besides, the extra typing is not actually an issue, thanks to intelligent editor tools.

          N C 2 Replies Last reply
          0
          • S Stefan_Lang

            Niklas Lindquist wrote:

            when dealing with streams and manipulators

            Yes, I do find myself writing std::cout and std::endl quite a lot, but so far I've resisted the urge to add a using statement. However, you could insert a using declaration instead of a using directive for the most commonly used symbols, as pointed out at C++ FAQ.

            Niklas Lindquist wrote:

            The code tends to get quite verbose

            Are you implying this is a bad thing? I actually find that helpful. Besides, the extra typing is not actually an issue, thanks to intelligent editor tools.

            N Offline
            N Offline
            Niklas L
            wrote on last edited by
            #5

            Stefan_Lang wrote:

            The code tends to get quite verbose

            Are you implying this is a bad thing?

            In most cases, no. In some cases, yes. It does affect readability if it's too frequent. However, I have not experienced this in any other situation than when working with std streams.

            A 1 Reply Last reply
            0
            • S Stefan_Lang

              I've put the topic in quotes, because I'm not asking for me. Instead I am referring to the various occasions I've seen where people advise inexperienced programmers to add

              using namespace std;

              to their program. Personally I feel that this is not generally a good advice, for all of the reasons pointed out at http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5[^]. But maybe the people offering this advice do have their reasons and take a different view? I'd like to know. What is your stance? Why do you think it is good to add a

              using namespace

              statement, and when? Or do you agree with me and generally advise against it? If so, can you offer any reasons not already offered at the site I linked above? P.S.: After reading the responses I've found there are indeed (IMHO) few strong arguments in favor of using using namespace std;. The two main arguments were: 1. Typing effort 2. Lack of readability, most notably (but not only) with the use of formatting in stream classes Personally I don't think the typing effort should be considered a valid reason, unless there are absolutely no drawbacks to consider. Apart from that, both cases can be countered by various methods: 1. Use intelligent editors to minimize the typing effort 2. Use typedef to simplify complex (or just hard to type) expressions 3. Use a using declaration (e. g. using std::cout;) rather than a using directive In those rare cases where, locally, symbols from namespace std are being heavily used (e. g. an output function using lots of formatting), adding a using directive locally (i. e. within the scope of the function only) could be a reasonable compromise. Thanks to Stephen Hewitt for pointing that out.

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              i'll use it in a .cpp unless i'm feeling particularly masochistic that day and want to type "std::" a hundred times.

              image processing toolkits | batch image processing

              1 Reply Last reply
              0
              • N Niklas L

                Stefan_Lang wrote:

                The code tends to get quite verbose

                Are you implying this is a bad thing?

                In most cases, no. In some cases, yes. It does affect readability if it's too frequent. However, I have not experienced this in any other situation than when working with std streams.

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #7

                I agree... it can affect readability...

                1 Reply Last reply
                0
                • S Stefan_Lang

                  I've put the topic in quotes, because I'm not asking for me. Instead I am referring to the various occasions I've seen where people advise inexperienced programmers to add

                  using namespace std;

                  to their program. Personally I feel that this is not generally a good advice, for all of the reasons pointed out at http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5[^]. But maybe the people offering this advice do have their reasons and take a different view? I'd like to know. What is your stance? Why do you think it is good to add a

                  using namespace

                  statement, and when? Or do you agree with me and generally advise against it? If so, can you offer any reasons not already offered at the site I linked above? P.S.: After reading the responses I've found there are indeed (IMHO) few strong arguments in favor of using using namespace std;. The two main arguments were: 1. Typing effort 2. Lack of readability, most notably (but not only) with the use of formatting in stream classes Personally I don't think the typing effort should be considered a valid reason, unless there are absolutely no drawbacks to consider. Apart from that, both cases can be countered by various methods: 1. Use intelligent editors to minimize the typing effort 2. Use typedef to simplify complex (or just hard to type) expressions 3. Use a using declaration (e. g. using std::cout;) rather than a using directive In those rare cases where, locally, symbols from namespace std are being heavily used (e. g. an output function using lots of formatting), adding a using directive locally (i. e. within the scope of the function only) could be a reasonable compromise. Thanks to Stephen Hewitt for pointing that out.

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  It's worth noting that it can be used in any scope, not just globally. For example, you could use it in a function like this:

                  void DoStuff()
                  {
                  using namespace std;
                   
                  cout << "Yeah!" << endl;
                  }

                  You can also selectively "lift" something into a namespace with a using declaration:

                  void DoStufff()
                  {
                  using std::cout;
                  using std::endl;
                   
                  cout << "Yeah!" << endl;
                  }

                  Steve

                  S 1 Reply Last reply
                  0
                  • S Stefan_Lang

                    Niklas Lindquist wrote:

                    when dealing with streams and manipulators

                    Yes, I do find myself writing std::cout and std::endl quite a lot, but so far I've resisted the urge to add a using statement. However, you could insert a using declaration instead of a using directive for the most commonly used symbols, as pointed out at C++ FAQ.

                    Niklas Lindquist wrote:

                    The code tends to get quite verbose

                    Are you implying this is a bad thing? I actually find that helpful. Besides, the extra typing is not actually an issue, thanks to intelligent editor tools.

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    Stefan_Lang wrote:

                    Are you implying this is a bad thing?

                    I think it clutters the code. Anyone puzzled by (for instance) STL error messages could share my opinion.

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    S 1 Reply Last reply
                    0
                    • L Lost User

                      Well, if you have lots of code using funcs only form one namespace in a file, then you can reduce the amount of typing you have to do by stating 'using namespace' at the top of the file. If you are not, and you are mixing lots of code from different namespaces, then you shouldnt, because you might have a clash somewhere, and either compiler errors or runtime errors.

                      ============================== Nothing to say.

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #10

                      I don't consider the typing to be a problem - good editors will reduce the extra typing to a minimum, and even in Notepad there's nothing preventing you from copy-pasting the extra std:: repeatedly. I do it all the time. (the copy-pasting, not using Notepad ;) ) P.S.: You could also use typedefs to improve readability and reduce typing effort. This can be especially helpful for containers and iterators.

                      L 1 Reply Last reply
                      0
                      • C CPallini

                        Stefan_Lang wrote:

                        Are you implying this is a bad thing?

                        I think it clutters the code. Anyone puzzled by (for instance) STL error messages could share my opinion.

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        S Offline
                        S Offline
                        Stefan_Lang
                        wrote on last edited by
                        #11

                        Ummm, I've never checked, but do error messages look different depending on whether or not you have a using directive? :confused:

                        C 1 Reply Last reply
                        0
                        • S Stephen Hewitt

                          It's worth noting that it can be used in any scope, not just globally. For example, you could use it in a function like this:

                          void DoStuff()
                          {
                          using namespace std;
                           
                          cout << "Yeah!" << endl;
                          }

                          You can also selectively "lift" something into a namespace with a using declaration:

                          void DoStufff()
                          {
                          using std::cout;
                          using std::endl;
                           
                          cout << "Yeah!" << endl;
                          }

                          Steve

                          S Offline
                          S Offline
                          Stefan_Lang
                          wrote on last edited by
                          #12

                          Hey, thanks, I actually never thought of that! Yes, I suppose if you have some dedicated IO functions it would be quite reasonable to add a using directive in there. Good idea! :)

                          1 Reply Last reply
                          0
                          • S Stefan_Lang

                            Ummm, I've never checked, but do error messages look different depending on whether or not you have a using directive? :confused:

                            C Offline
                            C Offline
                            CPallini
                            wrote on last edited by
                            #13

                            There's no relation, of course. My point of view is: using std:: everywhere simply makes code look more cluttered and 'cluttered-looking' code is bad (as you may see in the 'really messy' STL error messages).

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            S 1 Reply Last reply
                            0
                            • C CPallini

                              There's no relation, of course. My point of view is: using std:: everywhere simply makes code look more cluttered and 'cluttered-looking' code is bad (as you may see in the 'really messy' STL error messages).

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              S Offline
                              S Offline
                              Stefan_Lang
                              wrote on last edited by
                              #14

                              I've always thought that it's mostly the stuff that isn't also visible in the code, that makes compiler errors so hard to read, whereas the namespace symbols help separate the mangled symbols into managable bits that can be easier to understand. Personally, when I look at unfamiliar code and try to locate a problem, seeing std:: or the like helps me exclude parts that I know I don't need to bother about. Similarly, when I try to understand what a particular piece of code does, I often start with the bits that call out 'STL' to me, because that is what I have the least trouble to grasp. In short, to me the namespace qualifiers within the code are more helpful than obstructive. Of course, it might depend on the code you're looking at. If you're faced with types like

                              std::list<std::pair<MyClassA, MyClassB>, MyAllocator<std::pair<MyClassA, MyClassB> >::const_iterator

                              then you're in need of a typedef or two ;)

                              1 Reply Last reply
                              0
                              • S Stefan_Lang

                                I don't consider the typing to be a problem - good editors will reduce the extra typing to a minimum, and even in Notepad there's nothing preventing you from copy-pasting the extra std:: repeatedly. I do it all the time. (the copy-pasting, not using Notepad ;) ) P.S.: You could also use typedefs to improve readability and reduce typing effort. This can be especially helpful for containers and iterators.

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #15

                                Stefan_Lang wrote:

                                I don't consider the typing to be a problem

                                So dont use 'using' then. :)

                                ============================== Nothing to say.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups