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.
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.
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
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!
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.
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.
Based on ease of use, I thinkI'm refering to ease of use and standards compilent.
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 01:00 AM.
Another option would be to something along the following:
Or even better (IMO)Code:void foo() { using namespace std; cout<<"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.Code:void foo (std::ostream& out = cout) { using namespace std; out<<"Blarg"<<endl; }
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 03: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
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.
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.
There are no degrees of following the standard. Either you do, or you don't. Every variant does.Originally Posted by Mr.OC
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Opps, yeah typoBut it should be:
std :: ostream &out = std::cout
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'):
http://cboard.cprogramming.com/showt...=namespace+std
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.
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).Originally Posted by Hunter2
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