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. The Lounge
  3. I heard this somewhere

I heard this somewhere

Scheduled Pinned Locked Moved The Lounge
c++question
19 Posts 10 Posters 1 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    (This post is being submitted under the protection afforded by Lounge posting rule #2.) I read somewhere on this site that someone had written that it's bad practice to use "using namespace std;" in your C++ code. Can anyone explain the theory behind this?

    The difficult we do right away... ...the impossible takes slightly longer.

    M Greg UtasG P L H 7 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      (This post is being submitted under the protection afforded by Lounge posting rule #2.) I read somewhere on this site that someone had written that it's bad practice to use "using namespace std;" in your C++ code. Can anyone explain the theory behind this?

      The difficult we do right away... ...the impossible takes slightly longer.

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      A rather long discussion on SO[^]

      Latest Articles:
      DivWindow: Size, drag, minimize, and maximize floating windows with layout persistence

      1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        (This post is being submitted under the protection afforded by Lounge posting rule #2.) I read somewhere on this site that someone had written that it's bad practice to use "using namespace std;" in your C++ code. Can anyone explain the theory behind this?

        The difficult we do right away... ...the impossible takes slightly longer.

        Greg UtasG Offline
        Greg UtasG Offline
        Greg Utas
        wrote on last edited by
        #3

        It's definitely frowned on in a header file, where it's best to spell out names in full. I also avoid it in a .cpp, but using declarations for frequently used items are OK in a .cpp. A using directive is for everything in a namespace (using namespace std), but a using declaration is for a specific item (using std::string or using std::ostream).

        Robust Services Core | Software Techniques for Lemmings | Articles
        The fox knows many things, but the hedgehog knows one big thing.

        <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
        <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

        1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          (This post is being submitted under the protection afforded by Lounge posting rule #2.) I read somewhere on this site that someone had written that it's bad practice to use "using namespace std;" in your C++ code. Can anyone explain the theory behind this?

          The difficult we do right away... ...the impossible takes slightly longer.

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          You're probably referring to this thread[^]. I only remember it because I made a flippant comment.

          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            (This post is being submitted under the protection afforded by Lounge posting rule #2.) I read somewhere on this site that someone had written that it's bad practice to use "using namespace std;" in your C++ code. Can anyone explain the theory behind this?

            The difficult we do right away... ...the impossible takes slightly longer.

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

            Richard Andrew x64 wrote:

            Can anyone explain the theory behind this?

            Hi, I can give you an example of why using-declarations should be avoided. Sometime ago I was on a two-man team (Hello Andy !) working on the Delivery Optimization system service[^] which would debut with Windows 10 Threshold. The original code base was developed with VS2010 and 'using namespace boost' was sprinkled throughout much of our source code. The first problem we encountered was that boost::chrono, boost::atomic, and boost:mutex conflicted with std::chrono, std::atmomic and std:mutex. There were a few other issues with std::thread but I can't remember them all. We spent several days (probably a week) refactoring our code. When C++17 came around I had a similar problem with boost::filesystem conflicting with std::filesystem in other projects. We eventually moved away from boost to the std namespace when the internal OS build system added support for C++11. Now days I just type the entire scope and avoid using-declarations.

            Richard Andrew x64R 1 Reply Last reply
            0
            • L Lost User

              Richard Andrew x64 wrote:

              Can anyone explain the theory behind this?

              Hi, I can give you an example of why using-declarations should be avoided. Sometime ago I was on a two-man team (Hello Andy !) working on the Delivery Optimization system service[^] which would debut with Windows 10 Threshold. The original code base was developed with VS2010 and 'using namespace boost' was sprinkled throughout much of our source code. The first problem we encountered was that boost::chrono, boost::atomic, and boost:mutex conflicted with std::chrono, std::atmomic and std:mutex. There were a few other issues with std::thread but I can't remember them all. We spent several days (probably a week) refactoring our code. When C++17 came around I had a similar problem with boost::filesystem conflicting with std::filesystem in other projects. We eventually moved away from boost to the std namespace when the internal OS build system added support for C++11. Now days I just type the entire scope and avoid using-declarations.

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              Very interesting. I think I haven't worked on anything in C++ that's sufficiently complex to have had such a problem. :) But I thank you for taking the time to explain it.

              The difficult we do right away... ...the impossible takes slightly longer.

              L 1 Reply Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                (This post is being submitted under the protection afforded by Lounge posting rule #2.) I read somewhere on this site that someone had written that it's bad practice to use "using namespace std;" in your C++ code. Can anyone explain the theory behind this?

                The difficult we do right away... ...the impossible takes slightly longer.

                H Offline
                H Offline
                honey the codewitch
                wrote on last edited by
                #7

                Personally, I think it's fine in your main.cpp Anywhere else, such as in a header, it clutters your global naming container. In general, don't use "using namespace" in headers at all. Some people here might suggest that using namespace std, even in main.cpp is bad form because of all the stuff it drags in. I suggest those people relax. :)

                Real programmers use butterflies

                D 1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  Very interesting. I think I haven't worked on anything in C++ that's sufficiently complex to have had such a problem. :) But I thank you for taking the time to explain it.

                  The difficult we do right away... ...the impossible takes slightly longer.

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

                  Yeah, Shortly after I responded to your post I remembered some other details. I can remember that std::mutex and boost::mutex were nearly identical so I could seamlessly switch the code below from boost to the std namespace simply by changing the using-namespace declaration. It's a bit frightening when you discover three days later that you forgot to change a mutex in a few places.

                  //using namespace boost;
                  //using namespace std;

                  mutex m;
                  m.lock();
                  //... something here
                  m.unlock();

                  Can't make that mistake if you use the entire scope:

                  std::mutex m;
                  m.lock();
                  //... something here
                  m.unlock();

                  N 1 Reply Last reply
                  0
                  • Richard Andrew x64R Richard Andrew x64

                    (This post is being submitted under the protection afforded by Lounge posting rule #2.) I read somewhere on this site that someone had written that it's bad practice to use "using namespace std;" in your C++ code. Can anyone explain the theory behind this?

                    The difficult we do right away... ...the impossible takes slightly longer.

                    S Offline
                    S Offline
                    Slacker007
                    wrote on last edited by
                    #9

                    I am not a C++ programmer. I did find this thread and topic quite interesting, though. I sometimes wish I took up C++, but perhaps its a good thing I didn't. :) I urge everyone to "actually" read the SO page that Marc Clifton linked to, including all the comments. It shows the main reasons why you don't want std using statements in the header files, which I don't see mentioned much in this thread.

                    1 Reply Last reply
                    0
                    • L Lost User

                      Yeah, Shortly after I responded to your post I remembered some other details. I can remember that std::mutex and boost::mutex were nearly identical so I could seamlessly switch the code below from boost to the std namespace simply by changing the using-namespace declaration. It's a bit frightening when you discover three days later that you forgot to change a mutex in a few places.

                      //using namespace boost;
                      //using namespace std;

                      mutex m;
                      m.lock();
                      //... something here
                      m.unlock();

                      Can't make that mistake if you use the entire scope:

                      std::mutex m;
                      m.lock();
                      //... something here
                      m.unlock();

                      N Offline
                      N Offline
                      Nelek
                      wrote on last edited by
                      #10

                      Randor wrote:

                      Can't make that mistake if you use the entire scope:

                      care to elaborate?

                      M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                      L 1 Reply Last reply
                      0
                      • N Nelek

                        Randor wrote:

                        Can't make that mistake if you use the entire scope:

                        care to elaborate?

                        M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

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

                        Nelek wrote:

                        care to elaborate?

                        We are discussing C++ namespaces[^]. C++ code containing using-declarations[^] can lead to scope resolution ambiguity. boost::mutex[^] std::mutex[^] I am sharing my opinion that using-declarations should be avoided. Best Wishes, -David Delaune

                        N 1 Reply Last reply
                        0
                        • L Lost User

                          Nelek wrote:

                          care to elaborate?

                          We are discussing C++ namespaces[^]. C++ code containing using-declarations[^] can lead to scope resolution ambiguity. boost::mutex[^] std::mutex[^] I am sharing my opinion that using-declarations should be avoided. Best Wishes, -David Delaune

                          N Offline
                          N Offline
                          Nelek
                          wrote on last edited by
                          #12

                          Yeah, I had understood so far. But I didn't get the sentence I quoted. I suppose that you meant using the "std::" or "boost::" as prefix, you have less probability of getting errors than unsing both "using", i.e. because some methods might have similar parameters but different behaviour.

                          M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                          1 Reply Last reply
                          0
                          • H honey the codewitch

                            Personally, I think it's fine in your main.cpp Anywhere else, such as in a header, it clutters your global naming container. In general, don't use "using namespace" in headers at all. Some people here might suggest that using namespace std, even in main.cpp is bad form because of all the stuff it drags in. I suggest those people relax. :)

                            Real programmers use butterflies

                            D Online
                            D Online
                            den2k88
                            wrote on last edited by
                            #13

                            honey the codewitch wrote:

                            Some people here might suggest that using namespace std, even in main.cpp is bad form because of all the stuff it drags in.

                            As if linkers are that stupid...

                            GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                            H 1 Reply Last reply
                            0
                            • D den2k88

                              honey the codewitch wrote:

                              Some people here might suggest that using namespace std, even in main.cpp is bad form because of all the stuff it drags in.

                              As if linkers are that stupid...

                              GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                              H Offline
                              H Offline
                              honey the codewitch
                              wrote on last edited by
                              #14

                              Not that. Just that people don't like intellisense coming up with a billion and a half entries every time it opens.

                              Real programmers use butterflies

                              D 1 Reply Last reply
                              0
                              • H honey the codewitch

                                Not that. Just that people don't like intellisense coming up with a billion and a half entries every time it opens.

                                Real programmers use butterflies

                                D Online
                                D Online
                                den2k88
                                wrote on last edited by
                                #15

                                I use VisualAssistX, it usually gives good entries anyway.

                                GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                H 1 Reply Last reply
                                0
                                • D den2k88

                                  I use VisualAssistX, it usually gives good entries anyway.

                                  GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                  H Offline
                                  H Offline
                                  honey the codewitch
                                  wrote on last edited by
                                  #16

                                  I just use VS Code with minimal frills when I do C++. It mostly stays out of my way and that's what I like about it. It has a few quirks, but so does everything.

                                  Real programmers use butterflies

                                  D 1 Reply Last reply
                                  0
                                  • H honey the codewitch

                                    I just use VS Code with minimal frills when I do C++. It mostly stays out of my way and that's what I like about it. It has a few quirks, but so does everything.

                                    Real programmers use butterflies

                                    D Online
                                    D Online
                                    den2k88
                                    wrote on last edited by
                                    #17

                                    I tried VSCode and while I find it all in all good I despise its side-panels management. When I can I cram everything in VS and use the external toolchain to compile. I'm using it for an embedded Linux sw, I used it for microcontrollers... :D

                                    GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                    H 1 Reply Last reply
                                    0
                                    • D den2k88

                                      I tried VSCode and while I find it all in all good I despise its side-panels management. When I can I cram everything in VS and use the external toolchain to compile. I'm using it for an embedded Linux sw, I used it for microcontrollers... :D

                                      GCS d--(d-) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++*      Weapons extension: ma- k++ F+2 X

                                      H Offline
                                      H Offline
                                      honey the codewitch
                                      wrote on last edited by
                                      #18

                                      They never bothered me that much. Once I got the hang of ctrl+click to open in the side it was all gravy for me. Mostly I like VS Code because it's got a nice mix of minimal UI wrapped around maximum functionality. I have yet to find a file type it can't open. Makes it super nice for me working on my TTF library because I can just open a test font file and look at all the glyphs in VS Code. I really hope they backport some of that functionality into Visual Studio or something but I doubt it because they are such different products.

                                      Real programmers use butterflies

                                      1 Reply Last reply
                                      0
                                      • Richard Andrew x64R Richard Andrew x64

                                        (This post is being submitted under the protection afforded by Lounge posting rule #2.) I read somewhere on this site that someone had written that it's bad practice to use "using namespace std;" in your C++ code. Can anyone explain the theory behind this?

                                        The difficult we do right away... ...the impossible takes slightly longer.

                                        K Offline
                                        K Offline
                                        KateAshman
                                        wrote on last edited by
                                        #19

                                        A.k.a. namespace pollution. If you import a large amount of classes/functions, that you do not know the name of, you end up with a higher risk of running into namespace collisions.

                                        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