including namespaces

This is a discussion on including namespaces within the C++ Programming forums, part of the General Programming Boards category; Hmm what a good idea for a project: A compiler that will audiobly berate you for errors and warnings. Something ...

  1. #16
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Hmm what a good idea for a project: A compiler that will audiobly berate you for errors and warnings. Something in a sarcastic brithish tone

  2. #17
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Heres another question on this topic, whats so bad about using "using std::cout;" instead of putting "std::" infront of each cout? Or is it no difference, or is there another big 4 paragraph story about why you shouldnt?

  3. #18
    Some Guy
    Join Date
    Jul 2004
    Posts
    32
    Good question. I'm also wondering this. I've heard this is better than the first choice (using namespace std), but that is all I know...

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    hmm... which compiler are you using, C++Child?

    The fact is that namespaces now exist, and using deprecated headers solely to avoid them is just sticking your head into the sand.
    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

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Heres another question on this topic, whats so bad about using "using std::cout;" instead of putting "std::" infront of each cout? Or is it no difference, or is there another big 4 paragraph story about why you shouldnt?
    It may not be bad, from what I see.

    For example, in a header file or the code of some library, it would make sense to use the entire identifier.
    On the other hand, within main() one might want to use the using directive and then avoid being bothered by the prefix.
    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

  6. #21
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    >>>>>>>>>>Hmm what a good idea for a project: A compiler that will audiobly berate you for errors and warnings. Something in a sarcastic brithish tone

    Hmmm, I like that idea, "You included [#include <iostream.h>]! Make sure you include [#include<iostream>] or else I'll hack your registry! *snickers*"
    >>>>>>What compiler are you using.
    Borland C/C++ Compiler 5.5 (BCC55 Command Line)
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  7. #22
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by jlou
    I would think these problems are rare, especially if you are on your toes,
    [...]
    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.
    I agree. The risk is small, but is is there.
    Code:
    using namespace std; //Should perhaps be avoided
    using std::cout; //OK to use
    The more specific syntax is OK to use.

    Some guidelines for using namespaces then:
    • Do not put using-declarations at the global scope or directly within other namespaces in header files
    • Try to avoid "using namespace XX", as there is a (very small) risk that something could go wrong within larger projects.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #23
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Using "using std::cout;" has the basically the same problems as "using namespace std;", except they are confined to cout, which of course is even more unlikely to be used as a name somewhere else. I certainly wouldn't tell anybody they are forming a bad habit if they used that form. For other namespaces besides std that are longer and more cumbersome to type (or may be nested several namespaces deep), I use that form inside functions or other small scopes, so it certainly has its place. At this point I am only arguing style, but for the std namespace, it just seems to make more sense to use "std::" everywhere.

    Ha! - all in one paragraph!
    Last edited by jlou; 08-02-2004 at 10:40 AM.

  9. #24
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Thantos
    Hmm what a good idea for a project: A compiler that will audiobly berate you for errors and warnings. Something in a sarcastic brithish tone
    Hmm... I kind of like the Fritz chess program... Nothing quite like getting yelled at in German for making a stupid mistake.

  10. #25
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by jlou
    Using "using std::cout;" has the basically the same problems as "using namespace std;"
    Your example would give an error at compile-time if "using namespace std;" was replaced with "using std::count;", so I'd say it's safer to use.

    Also, I think this thread is great.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #26
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I think he meant that you can still run into the same errors that you can with a normal 'using namespace ...' statement. It is safer in a sense, though, if you are the only one modifying the code, because whenever you do import a particular symbol, it should register in your mind if it conflicts with something else you put in.

  12. #27
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Zach L.
    I think he meant that you can still run into the same errors that you can with a normal 'using namespace ...' statement. .
    Yes, but how?

    Show me some code.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #28
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Quote Originally Posted by Sang-drax
    Yes, but how?

    Show me some code.
    Take the same example and then add the #include <algorithm> Assuming the 'using' behaves the same way (I haven't tried it) when using specific parts of the namespace or entire namespaces, it should still give the 'wrong' output.

    I say this because jlou said even after including the header it still used the wrong function, which makes sense if you think about it... because when a function without a namespace identifier is called, and there's a choice between a 'real' global function and a 'pseudo' global function resulting from the using statement, who wouldn't go for the real function?
    Last edited by Hunter2; 08-04-2004 at 10:57 AM.
    Just Google It. √

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

  14. #29
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The basic problem is that you override something without being aware of it. You can still do that, but it is a bit les likely because you are (hopefully) aware of what you explicitly imported into the namespace. Other programmers working on the same code, however, may not. I'll post a concrete example later.

  15. #30
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Hunter2
    there's a choice between a 'real' global function and a 'pseudo' global function resulting from the using statement, who wouldn't go for the real function?
    Any standard-conforming compiler would issue an error "ambiguous command", or something equivalent. The "real" function would not be called.

    The question is currently whether using-declarations like this one
    Code:
    using N::func;
    can result in the wrong func being called somewhere.

    I haven't been able to find such a case, but we have earlier seen that this can happen when using
    Code:
    using namespace N;
    Last edited by Sang-drax; 08-04-2004 at 11:13 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Page 2 of 3 FirstFirst 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