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 make namespace std global?

Should I make namespace std global?

Scheduled Pinned Locked Moved C / C++ / MFC
c++wpfwcfoopquestion
9 Posts 5 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.
  • L Offline
    L Offline
    Link2600
    wrote on last edited by
    #1

    Should I do this: #include <iostream> using namespace std; or should I do this: #include <iostream> using std::cout; using std::cin; using std::endl; . . . I find that making the namespace std global in implementation file easier to work with, but some people suggest avoid doing that. Should I? Do you make namespace std global? Thanks, Alex ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".

    C R L 3 Replies Last reply
    0
    • L Link2600

      Should I do this: #include <iostream> using namespace std; or should I do this: #include <iostream> using std::cout; using std::cin; using std::endl; . . . I find that making the namespace std global in implementation file easier to work with, but some people suggest avoid doing that. Should I? Do you make namespace std global? Thanks, Alex ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      The second option is neater, and better style. In the real world, it's doubtful that you'll notice the difference. I always do the second, to be a nerd. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

      1 Reply Last reply
      0
      • L Link2600

        Should I do this: #include <iostream> using namespace std; or should I do this: #include <iostream> using std::cout; using std::cin; using std::endl; . . . I find that making the namespace std global in implementation file easier to work with, but some people suggest avoid doing that. Should I? Do you make namespace std global? Thanks, Alex ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        I rarely use either. The whole point of namespaces is to be able to separate groups of things to avoid name clashes. The using directive basically gets rid of the namespaces and puts everything in the global namespace, making the use of the namespace pointless, especially the first form. If I do use the using directive, I use the second form. It's a lot cleaner and helps to prevent clutter of the global namespace.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        1 Reply Last reply
        0
        • L Link2600

          Should I do this: #include <iostream> using namespace std; or should I do this: #include <iostream> using std::cout; using std::cin; using std::endl; . . . I find that making the namespace std global in implementation file easier to work with, but some people suggest avoid doing that. Should I? Do you make namespace std global? Thanks, Alex ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".

          L Offline
          L Offline
          Link2600
          wrote on last edited by
          #4

          Okay, so if I use using namespace std;, does that mean the program will compile everything named under namespace std? Many people recommend not to make the namespace std global, but some say it doesn't matter. But I usually do the second way, typing explicitly using::string, using::cout......until I read the newly released book from Andrei Alexandrescu (highly repected person in the field), the book named C++ coding standard. (ISBN: 0-321-11358-6) Here is a quote from the book: But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widge) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way. After reading this, I was thinking, am I doing the right way? So I thought I would ask people here how they are doing it. // Implementation file #include <iostream> using namespace std; int main() { // code return 0; } or should I do this: // Implemenation file #include <iostream> using::cout; using::cin; using::endl; int main() { //code return 0; } ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".

          R 1 Reply Last reply
          0
          • L Link2600

            Okay, so if I use using namespace std;, does that mean the program will compile everything named under namespace std? Many people recommend not to make the namespace std global, but some say it doesn't matter. But I usually do the second way, typing explicitly using::string, using::cout......until I read the newly released book from Andrei Alexandrescu (highly repected person in the field), the book named C++ coding standard. (ISBN: 0-321-11358-6) Here is a quote from the book: But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widge) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way. After reading this, I was thinking, am I doing the right way? So I thought I would ask people here how they are doing it. // Implementation file #include <iostream> using namespace std; int main() { // code return 0; } or should I do this: // Implemenation file #include <iostream> using::cout; using::cin; using::endl; int main() { //code return 0; } ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".

            R Offline
            R Offline
            Ryan Binns
            wrote on last edited by
            #5

            Alex Ngai wrote: But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widge) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way. Which is exactly why I rarely use them either. Promoting any namespace member to the global namespace (which is what the using directive actually does) has the rather dangerous ability to shadow a member already in the global namespace with the same name, much like an overloaded function, but a lot more subtle. These errors can be very difficult to debug.

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            L 1 Reply Last reply
            0
            • R Ryan Binns

              Alex Ngai wrote: But here's the common trap: Many people think that using declarations issued at namespace level (for example, using N::Widge) are safe. They are not. They are at least as dangerous, and in a subtler and more insidious way. Which is exactly why I rarely use them either. Promoting any namespace member to the global namespace (which is what the using directive actually does) has the rather dangerous ability to shadow a member already in the global namespace with the same name, much like an overloaded function, but a lot more subtle. These errors can be very difficult to debug.

              Ryan

              "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

              L Offline
              L Offline
              Link2600
              wrote on last edited by
              #6

              Thanks for your reply. But many feature in stardard C++ are in namespace std. Even the simple input & output, string are in namespace std. So, should I stick with using::string style? Is it better or it doesn't matter, like what the author said? ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".

              M 1 Reply Last reply
              0
              • L Link2600

                Thanks for your reply. But many feature in stardard C++ are in namespace std. Even the simple input & output, string are in namespace std. So, should I stick with using::string style? Is it better or it doesn't matter, like what the author said? ----------------------------- C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".

                M Offline
                M Offline
                Mike Beckerleg
                wrote on last edited by
                #7

                My preference is to use individual using std::string; type statements instead of bringing the whole std namespace in with using std; But of course there is no reason why you have to use using ...; at all. You can just use the full names at the appropriate points. eg std::cout << "This is a test" << std::endl; Instead of using std::cout; using std::endl; cout << "This is a test" << endl; Mike

                A 1 Reply Last reply
                0
                • M Mike Beckerleg

                  My preference is to use individual using std::string; type statements instead of bringing the whole std namespace in with using std; But of course there is no reason why you have to use using ...; at all. You can just use the full names at the appropriate points. eg std::cout << "This is a test" << std::endl; Instead of using std::cout; using std::endl; cout << "This is a test" << endl; Mike

                  A Offline
                  A Offline
                  Antony M Kancidrowski
                  wrote on last edited by
                  #8

                  Mike Beckerleg wrote: You can just use the full names at the appropriate points. Indeed, I prefer to use the explicit full names. It is then obvious which function you intend to call. Ant. I'm hard, yet soft.
                  I'm coloured, yet clear.
                  I'm fruity and sweet.
                  I'm jelly, what am I? Muse on it further, I shall return!
                  - David Walliams (Little Britain)

                  R 1 Reply Last reply
                  0
                  • A Antony M Kancidrowski

                    Mike Beckerleg wrote: You can just use the full names at the appropriate points. Indeed, I prefer to use the explicit full names. It is then obvious which function you intend to call. Ant. I'm hard, yet soft.
                    I'm coloured, yet clear.
                    I'm fruity and sweet.
                    I'm jelly, what am I? Muse on it further, I shall return!
                    - David Walliams (Little Britain)

                    R Offline
                    R Offline
                    Ryan Binns
                    wrote on last edited by
                    #9

                    Antony M Kancidrowski wrote: Indeed, I prefer to use the explicit full names. It is then obvious which function you intend to call. Exactly :)

                    Ryan

                    "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                    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