#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Europe
    Posts
    51
    Rep Power
    11

    How do I explicitly not use namespace std?


    Hi,

    In some places of my code I need to use std, in other places I want to to use it. For example, I need to use the following, which won't work if I use std (I am under MS visual C++):

    ifstream datastream("dich178.txt", ios::in);
    for(int i=0; i<OBS; i++){
    for(int j=0; j<VARS+1+AUXVARS; j++){
    datastream>>data[i][j];
    }
    cout<<data[i][VARS+1]<<" ";
    };

    If I drop "use namespace std;", it works, but then in other places I need to use std stuff like list<int>, etc. I could use std-specific data input streams, but it's painfully slow on my computer comparing to non-std input.

    If I want to use std, I can explicitly write std::cout<<"blah", but is there any name of "default" namespace, so I can write like 'something::cout<<"blah"', or 'using namespace not-std;'. What is the name of that "default" namespace? Any advise please? Thanks in advance,

    --Raokramer
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    1. Don't put this statement:
    using namespace std;
    in your code files.

    2. Don't include old C style header files like this:
    #include <iostream.h>

    instead include the new C++ style header files like this:
    #include <iostream>

    Why?? Because including iostream.h is equivalent to writing the following two statements:
    #include <iostream>
    using namespace std;

    which is not what you want. The reason for the above is because old C++ standards didn't support namespaces. When they introduced namespaces into C++, the new include files dropped the .h extension, but they kept the old .h files for compatibility reasons and made them work as described above. Similarly, use C++ equivalents for all the C headers (cstdio instead of stdio.h, cstdlib instead of stdlib.h etc.)

    If you do the above, then the namespace std will not be implied by default, unless you put your own using namespace std; statement in the code.

    Hope this helps :)
    Last edited by Scorpions4ever; September 5th, 2003 at 09:42 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Europe
    Posts
    51
    Rep Power
    11
    Yes, that helped! Thanks a lot :)
  6. #4
  7. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by Scorpions4ever
    Why?? Because including iostream.h is equivalent to writing the following two statements:
    #include <iostream>
    using namespace std;
    [/B]
    It's not really equivalent, in the sense that if you use <iostream.h> you're not necessarily using the standard library at all. It's quite possible that an implementation will maintain two separate headers and that the .h version will contain extensions that are not available in the standard library, or be incompatible with it in some way or other, as the .h version is retained by many implementations for backward compatibility. Of course, some implementations have dropped <iostream.h> already.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802

    Re: How do I explicitly not use namespace std?


    Originally posted by raokramer
    ...I need to use the following, which won't work if I use std...
    A better question would be why does it not work? Anyhow to answer your question directly...

    The statement

    using namespace std ;

    moves the symbols in the std namespace to the global namepase.

    You can explicitly access a symbol in the global namespace by prefixing the symbol with :: (scope resolution operator), with no left operand, so

    ::cout

    may not be the same as

    std::cout

    Clifford.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Europe
    Posts
    51
    Rep Power
    11
    Thanks BigBadBob and Clifford,

    Now I am not using "using namespace std;", but instead I am prefixing with "std::" anything that belongs to it.
    BigBadBob's argument regarding libraries was a little over my head though. I don't yet really understand how libraries work or what exactly global namespace is, which fortunately doesn't keep me from using them :)
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Originally posted by BigBadBob
    It's not really equivalent, in the sense that if you use <iostream.h> you're not necessarily using the standard library at all. It's quite possible that an implementation will maintain two separate headers and that the .h version will contain extensions that are not available in the standard library, or be incompatible with it in some way or other, as the .h version is retained by many implementations for backward compatibility. Of course, some implementations have dropped <iostream.h> already.
    That's why I said "equivalent" instead of "exact". Maybe a better way would have been to toss the word "almost" in front. BTW, just out of curiosity, I was wondering if you know of any compilers that dropped the .h files already.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally posted by raokramer
    ...I don't yet really understand how libraries work or what exactly global namespace is...
    The global namespace is what you previously referred to as the default namespace. It is where everything that is not in an explicit namespace belongs.

    A library is simply a collection of pre-compiled object code collected into a single file. The linker can extract these object code modules and link them to your code.

    Clifford
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Europe
    Posts
    51
    Rep Power
    11
    Thanks,

    I actually didn't know the libraries were pre-compiled. May be this will help me in the future to write more efficient code for my specific tasks. --Raokramer

IMN logo majestic logo threadwatch logo seochat tools logo