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
Printable View
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
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? :D
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...
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.
It may not be bad, from what I see.Quote:
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?
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.
>>>>>>>>>>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)
I agree. The risk is small, but is is there.Quote:
Originally Posted by jlou
The more specific syntax is OK to use.Code:using namespace std; //Should perhaps be avoided
using std::cout; //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.
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! ;)
Hmm... I kind of like the Fritz chess program... Nothing quite like getting yelled at in German for making a stupid mistake.Quote:
Originally Posted by Thantos
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.Quote:
Originally Posted by jlou
Also, I think this thread is great.
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.
Yes, but how?Quote:
Originally Posted by Zach L.
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.Quote:
Originally Posted by Sang-drax
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? :D
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.
Any standard-conforming compiler would issue an error "ambiguous command", or something equivalent. The "real" function would not be called.Quote:
Originally Posted by Hunter2
The question is currently whether using-declarations like this one
can result in the wrong func being called somewhere.Code:using N::func;
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;