including namespaces

This is a discussion on including namespaces within the C++ Programming forums, part of the General Programming Boards category; using namespace std using std::Command std::cout << something; std::cin >> anything; Its the same but I think that the first-line ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    18

    including namespaces

    using namespace std
    using std::Command
    std::cout << something; std::cin >> anything;

    Its the same but I think that the first-line method is more handy, even if you have 2-3 commands to include.. But fairly often I see two other methods in someones code. Why so? Maybe they're differs in memory usage or whatever? Can anyone explain me?
    I know, its nonsense. But Im asking myself - 'why so?'.. So I need any answer to be quiet
    Last edited by rockdj; 07-29-2004 at 04:45 PM.

  2. #2
    nbk
    nbk is offline
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45
    It's about personal style. The third is used mainly to get into the habit(no, it's only cause it's more professional looking ) I like the first one

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    18
    I like the first one
    Yes yes, me too! :P

  4. #4
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    Professional looking? I say messy.
    *** TITANIC has quit (Excess Flood)

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    using namespace std; is kinda a newbie work around. Do a board search and you'll find a couple thousand threads on why it should be avoided.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    18
    Do a board search and you'll find a couple thousand threads
    Indeed! Search::"namespace std" found 99999999999s threads...
    Anyone who have a time please explain me why shoud I avoid to use "using namespace std"?

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    How about doing a simple search and READING the threads. Its been discussed at length and in depth many times before.

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Thantos is right, there are plenty of places that can be searched to find that answer. However, here is yet another explanation attempt:

    When you put the using directive into your code, it means that all names that are in the namespace can now be used without having to explicitly state which namespace they are from. So, if you put "using namespace std;" into your code, then anywhere inside that scope you could just type cout instead of explicitly stating that you are using the cout in the std namespace.

    That seems easier to you, why would you want to always explicitly state that you want to use the cout from namespace std, right? Well, there are all kinds of things in the std namespace, not just cout. As your code gets more complicated and you use other third party libraries, or you use classes written by other programmers or even yourself from a different project, you start to lose track of all the different names that are being used in your program.

    Eventually, it becomes more and more likely that you will be using two different things - classes, variables, enums, functions or anything else with a name - that have the same name. If you are lucky, the compiler will complain, if not, it might just choose a different one than you intended without you ever knowing it. That is the point of namespaces - to group names together that are used in the same set of code so that it can be used with other code without fear of naming conflicts. If std has a cout and your partner's class is called cout, then the compiler knows which one you are referring to if you explicitly state the namespace it is in.

    When you have very small programs, all of which are in one file and use only one or two classes and a couple header files, then chances are slim that you'll get any naming conflicts. That is why many newbies prefer the using directive - it is easier and not very dangerous for them. Also, many people used to older C++ that did not use namespaces also put in the using directive as a simple way to update their code to the new standard.

    I always explicitly type std:: (unless making modifications to someone else's code), and I would recommend you and all newbies do the same. That's just a recommendation, it's not the end of the world if you don't. It just helps you get into that habit, so in 6 months or a year when you work on a big project not only are you used to seeing std::, but you even start to like the look of it better. At the very least, if you want to use a using directive, I'd suggest putting it inside the function you want to use it in, or only add a using directive for the specific names you want, rather than bringing in the entire namespace.
    Last edited by jlou; 07-29-2004 at 06:07 PM.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    18
    Now its clear... And its not 'the same' as I think before :\ Thanks for explanation, jlou.

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    While it is better to try to avoid using namespace, it's not that bad to use it.
    This is particulary true for the std namespace, because programmers will avoid using names that conflict with the standard C++ ones.

    It's not like using void main, or anything like that.
    Quote Originally Posted by jlou
    Eventually, it becomes more and more likely that you will be using two different things - classes, variables, enums, functions or anything else with a name - that have the same name. If you are lucky, the compiler will complain, if not, it might just choose a different one than you intended without you ever knowing it.
    I cannot see that actually happening. Please give an example of an unintentional use of a symbol.

    Code:
    namespace A
    {
    int x = 1;
    int y = 1;
    }
     
    //This namespace is hidden away in some header
    namespace B
    {
    int x = 2;
    int z;
    }
     
    //Some left-over global variables
    int x = 3;
    int w = 3;
     
    using namespace A;
    using namespace B;
     
    int main()
    {
    y = 0; //Legal
    z = 0; //Legal
    w = 0; //Legal
    x = 0; //Error: ambiguous symbol
    }
    When we try to modify x we have to specify which x we want to use, like:
    Code:
    A::x = 0;
    B::x = 0;
    ::x = 0;
    What should be avoided is the using-declaration within header files and other namespaces.
    Last edited by Sang-drax; 07-30-2004 at 04:56 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by Sang-drax
    I cannot see that actually happening. Please give an example of an unintentional use of a symbol.
    I would think these problems are rare, especially if you are on your toes, however here is an example:
    Code:
    // Deep in a header in a library somewhere
    int count(int* array1, int* array2, int numElements)
    {
        int sum = 0;
        for (int i = 0; i < numElements; i++)
        {
            sum += array1[i];
            sum += array2[i];
        }
        return sum;
    }
    
    // In main.cpp
    
    // Ooops, forgot to #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int A[] = { 2, 0, 4, 6, 2, 3, 2, -7 };
        const int N = sizeof(A) / sizeof(int);
    
        cout << "Number of twos: " 
            << count(A, A + N, 2)
            << endl;
    }
    Result: undefined behavior with no compile errors. Output: 5578235 on my machine.

    In fact, in MSVC++ 6.0, even if you don't forget to include the <algorithm> header, it still uses the wrong count method and outputs the large value.

    If you had explicitly typed std::count, however, then a compile error would have made you realize that you forgot the correct header, at which point you'd fix it and then the correct function would be called.

    If you have two choices, and one has a remote chance of biting you in the butt, but is easier by a few keystrokes to implement, then I would recommend the one that takes a few more keystrokes and a little getting used to, but doesn't have the same dangers as the easier way. That's just my opinion.

  12. #12
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    I prefer using none of those methods and including <iostream.h> instead of iostream. In <iostream.h> you don't have to do anything with namespace std!
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>I prefer using none of those methods and including <iostream.h> instead of iostream. In <iostream.h> you don't have to do anything with namespace std!

    ...
    does your compiler yell at you a lot?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    <iostream.h> is no longer standard... As JaWiB is getting at, the compiler really should scream at you for using it instead of the standard one, although many compilers have that file for backward compatibility.

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    My compiler never screams at me, are u kidding me?
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including extra .cpp files visual2008
    By Cathalo in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2009, 03:29 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading a line including blanks
    By Ec4U2du in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 06:32 PM
  5. namespaces
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2002, 02:57 PM

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