Which way is better ?

This is a discussion on Which way is better ? within the C++ Programming forums, part of the General Programming Boards category; I was just wondering is Code: using namespace std; A good idea or just program without it?...

  1. #1
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Question Which way is better ?

    I was just wondering is
    Code:
    using namespace std;
    A good idea or just program without it?
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's a question for debate, really. There is no right or wrong answer, but rather it is a "it depends".

    My personal view is that it is fine to use "using namespace std" at the beginning of a C file. Or "using namespace X" for that matter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    if you know there aren't and won't be any name conflicts, go for it. if you aren't sure, don't take the risk. simple.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    My opinion is to use it if you have to, otherwise save yourself the keystrokes.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It is generally bad form to put the std using directive in header files or above other header #includes. So except for simple programs I'd stay away from doing that.

    As for using it in source files, that is up to you. I prefer not to do so but there are valid arguments on both sides.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Daved View Post
    It is generally bad form to put the std using directive in header files or above other header #includes. So except for simple programs I'd stay away from doing that.
    Indeed & completely agreed - using directives (std or otherwise) should never[1] be found in header-files, because it "messes" with what happens in code that includes that header file beyond what you can expect.

    [1] I would consider exceptions to this that you may for example implement some sort of replacement functions for some standard functions, and thus do something like:
    Code:
    // mydebug.h
    
    using MyDebug::somefunc;
    ...
    So if you include "mydebug.h" you get some debugging version of the standard function, otherwise you get the normal version. But that is a very special case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    I would encourage you to use 'using' directives.
    I would even encourage you to use 'using namespace' directives.
    I'll even tell you that you will eventually have to use a 'using' directive.

    The problems come from putting such a directive in a header or at the top of source files from global scope. It is evil. If you have ambiguities, collisions, you must fully qualify the name. Any 'using' directive can cause these ambiguities. If you must fully qualify the name there is no point to namespaces. If it comes to that, you may as well just be using the C style 'long_name_prefix'.

    I've always used the 'local' or '<anonymous>::local' "trick" to get just what I need to implement the source for a particular translation unit.

    [Edit]
    using directives (std or otherwise) should never[1] be found in header-files
    This is silly. Or maybe you just don't mean what you said. If the directive is enclosed in some private scope it shouldn't cause any problems.

    If you are dealing with templates and you need a function symbol to resolve to non-standard overloads you have no choice.
    [/Edit]

    Soma

    Code:
    namespace
    {
       namespace local
       {
          using foo1::bar1;
          using foo1::bar2;
          using foo2::bar3;
          using foo2::bar4;
          using namespace foox::barx;
       }
    }
    
    fully::qualified::type some::other::name()
    {
       using namespace local;
       // ...
       bar1 b1;
       bar2 b2;
       bar3 b3;
       bar4 b4;
       // ...
       barx1 bx1;
       // ...
    }
    Last edited by phantomotap; 02-09-2009 at 02:36 PM.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by phantomotap View Post
    If you are dealing with templates and you need a function symbol to resolve to non-standard overloads you have no choice.
    Would you care to elaborate on that? I've never seen any situation where you couldn't just fully qualify the name to get it working.

    I'm pro-using directives too, but putting them before #include directives or in the global space of a header file is evil.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Would you care to elaborate on that? I've never seen any situation where you couldn't just fully qualify the name to get it working.
    Code:
    namespace foo {
      class MyComplexObject { ... };
      inline void swap(MyComplexObject &l, MyComplexObject &r) { /* fast swap */ }
    }
    
    namespace bar {
      template <typename T>
      void MyTContainer<T>::swap_with_back(T& t) {
        std::swap(t, this->back()); // Oops, doesn't use the fast swap for MyComplexObject.
      }
    
      template <typename T>
      void MyTContainer<T>::swap_with_back(T& t) {
        using std::swap;
        swap(t, this->back()); // Ah, ADL finds and uses the fast swap.
      }
    }
    Of course, that doesn't justify putting them in the global namespace, but phantomatap didn't say that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,171
    I personally prefer to only "using" the things I need.

    Code:
    using std::cin;
    using std::cout;
    using std::vector;
    ... etc
    Less pollution, and no possibility of someone introducing something into the std namespace in a later standard and break my code.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by phantomotap
    I would encourage you to use 'using' directives.
    I would even encourage you to use 'using namespace' directives.
    You can just call the former using declarations and the latter using directives.

    My preferred example of when a using declaration would be appropriate in a restricted scope in a header file would be to bring in names from a base class hidden by overloads in a derived class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by phantomotap View Post
    I would encourage you to use 'using' directives.
    I would even encourage you to use 'using namespace' directives.
    I'll even tell you that you will eventually have to use a 'using' directive.

    The problems come from putting such a directive in a header or at the top of source files from global scope. It is evil. If you have ambiguities, collisions, you must fully qualify the name. Any 'using' directive can cause these ambiguities. If you must fully qualify the name there is no point to namespaces. If it comes to that, you may as well just be using the C style 'long_name_prefix'.
    I am typically against using directives, since I like to use them as categories. If I put functions inside a namespace String, I won't have to prefix those functions "Str" or such.
    So I have this:
    Code:
    namespace String
    {
        std::string Cut(std::string&, std::string&);
    }
    And then do...
    Code:
    using namespace Strings;
    Cut(...);
    ...instead of...
    Code:
    String::Cut(...);
    Which is more readable?
    It can get worse with other functions such as if you have functions named "Load" or similar.
    But using isn't evil. Oh no. There is a time where it can be applied that gives more readability. I'm just saying one should be careful in doing so, and that fully expressing them isn't so bad as it sounds, and we're not back in the C era.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The thing with using namespaces as categories is that it wouldn't really avoid conflicts, would it? If your library puts math-related functions into a namespace called Math and my library does the same since that's the most logical thing to do in order to categorize things, there would still be naming conflicts?

    On the other hand (top-level) namespaces like std or boost don't categorize things and just try to be library-specific...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    But you can do both.
    Code:
    namespace MyLibrary { namespace Math { ... } }
    So, if you wanted, you could do using namespace MyLibrary, but not namespace Math.
    This is typically what I do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, yes.

    But since I don't really care that it is YourLibrary that I'm using (because the only math library I am using is yours, whereas I might care more that it is the math module of YourLibrary) why shouldn't I do this in source files?

    Code:
    namespace my_library
    {
    namespace math
    {
        void sqrt() {}
    }
    }
    
    using namespace my_library;
    
    int main()
    {
        math::sqrt();
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21