std::cout or using namespace std or using std::cout

This is a discussion on std::cout or using namespace std or using std::cout within the C++ Programming forums, part of the General Programming Boards category; What do you guys think is a better method when using the standard name space, std::cout or using namespace std ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    21

    std::cout or using namespace std or using std::cout

    What do you guys think is a better method when using the standard name space,

    std::cout or using namespace std or using std::cout

    I'm refering to ease of use and standards compilent.

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well, using the std::cout statment is a bit overkill but it does prevent a minute possibility of future name clashing with some header file not even made yet.

    Using the using std::cout method invokes exactly what you need however if you are extensively using the the std namespace can become burdensome.

    Using the using namespace std, invokes the entire namespace into global scope and thus may cause name clashing in some applications you may wind up implementing.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    i usually use

    using namespace std;

  4. #4
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    In my experience, each method represents your level of paranoia. For example, if I'm writing a library for others to use then I'll make sure to use the prefix method (ie. std::cout) because I have no idea how the library will be used in combination with other libraries.

    If I feel that it's possible my lack of knowledge about a library could cause name conflicts, I'll use the declaration method (ie. using std::cout). In that situation I'm comfortable that the name I'm releasing won't cause conflicts, but other names that I may not know about could.

    If I have full control over the application and I know the libraries well enough to be confident in avoiding conflicts, I'll use the directive method (ie. using namespace std). This is especially true for small applications and test programs that I would post to forums like this one.

    Until projects get bigger, namespaces are pretty much just a nuisance because their benefit doesn't shine through until you have a large project with multiple libraries from multiple sources and a team of programmers working together on it.
    Kampai!

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I prefer the 'std::cout << whatever' method, although I use the containers much more frequently than the output/input streams ('list' and 'map' are much more likely to clash than 'cout' or 'cin'). Anyway, my advice: Whichever method you choose, try to avoid 'using namespace std;' in header files. Why you'd want to do this I leave as an excerise for the interested reader.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I've never found the need to make my own namespace, so I prefer to just specify the std namespace at the top. However I can understand where everybody else is coming from if they make their own containers.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm refering to ease of use and standards compilent.
    Based on ease of use, I think

    using namespace std;

    wins. As for standards compliant, I don't know enough about the standards, but I think either way is standards compliant-->if you are using a standards compliant compiler and it doesn't give you an error, then it's standards compliant.

    However, to make an informed choice as to which method to use, I think you have to understand why namespaces are used in the first place. It's to prevent what's called 'name collisions", which is two variables having the same name. For instance, if you were working on a huge program with 1,000 other programmers, what would happen if several of you happened to use the same variable name? When you put all your separate sections of the program together, and tried to compile it, the compiler would give you a bunch of errors. On the other hand, if every programmer were assigned their own unique namespace, which is just an additional name tacked on to each variable, then there wouldn't be any duplicate variable names. Two programmers could both have a variable named Today, but when it is also qualified by their unique namespace name, there is no duplicate variable name:

    SallyC::Today
    MarkS::Today

    When, you use the statement:

    using namespace std;

    you are essentially erasing std:: from the names of all the variables in the standard library. Are you going to accidentally use the same variable name as a variable in the standard library file you are including?

    I've always used:

    using namespace std;

    without problems.
    Last edited by 7stud; 01-31-2005 at 12:00 AM.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Another option would be to something along the following:

    Code:
    void foo()
    {
      using namespace std;
      cout<<"Blarg"<<endl;
    }
    Or even better (IMO)
    Code:
    void foo (std::ostream& out = cout)
    {
      using namespace std;
      out<<"Blarg"<<endl;
    }
    Both allow you to use the ease of not having to define a namespace and it keeps it local to the function so that it won't interfer with any other function.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I agree with Thantos. That's my preferred way, too.

    But it should be:
    std :: ostream &out = std::cout
    Last edited by CornedBee; 01-31-2005 at 02:21 AM.
    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
    Occasionally correct Mr.OC's Avatar
    Join Date
    Oct 2004
    Posts
    6
    For me, it really depends on how lazy I'm feeling at the time. Normally I'll use the std:: prefix. Of course, the 'using namespace std' statement kind of defeats the purpose of even having namespaces, just as having public member data in a class defeats encapsulation. It all depends on the person, though.

    Using the std:: prefix follows the standards a bit more, I'd imagine. That's just my two cents.

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    We've had this debate before, with the conclusion that it doesn't really matter. The compiler will catch all errors except for a special case with "using namespace std" where undefined behaviour is invoked if you forget to include a file under certain conditions.
    "using namespace std" is perfectly legitimate (especially for the example programs often posted here).

    But you must remember to never use any using-declaration within globals scope of header files, because that will affect every file that includes the header.

    Quote Originally Posted by Mr.OC
    Using the std:: prefix follows the standards a bit more, I'd imagine.
    There are no degrees of following the standard. Either you do, or you don't. Every variant does.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    But it should be:
    std :: ostream &out = std::cout
    Opps, yeah typo

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I believe the topic has been quite beaten to death in this old old old thread (Found it by searching 'using namespace std' combined with username 'jlou'):
    including namespaces
    If I recall correctly, the general end consensus was that it's safest to use std:: in front of everything rather than using using - and that's what I usually do (if I need to shorten a name like std::vector<std::vector<std::map<std::string, std::list<int> > > >, i'll typedef it as 2DSToLMapVect or something depending on what its use is).

    From a purely practical standpoint though, I think that Sake makes a very good point about project-omniscience in choosing what to do.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Hunter2
    If I recall correctly, the general end consensus was that it's safest to use std:: in front of everything rather than using using.
    Actually, only using namespace std was shown to have possible undefined behaviour under certain circumstances (the programmer forgets to include a header combined with a std-named function in the global namespace).

    Nevertheless, using namespace std is perfectly legitimate. Prefixing with std:: every time obfuscates the code too much IMO.

    Generally, it's in the end all a matter of style, as long as you don't pollute the global namespace when writing headers.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using namespace std;
    By Wraithan in forum C++ Programming
    Replies: 11
    Last Post: 07-08-2006, 11:40 AM
  2. using namespace std;
    By spank in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 05:28 AM
  3. namespace std
    By blankstare77 in forum C++ Programming
    Replies: 27
    Last Post: 07-24-2005, 12:33 PM
  4. Namespace std
    By Nubi in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2003, 08:30 PM
  5. using namespace std;
    By axon in forum C++ Programming
    Replies: 10
    Last Post: 03-05-2003, 11:41 PM

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