I have been told that using the whole namespace std is bad practise, due to dragging in the whole standard library...
As with Java I was also told that you should not include partial paths pulling in all classes from a package, eg.
import java.util.*; rather use: import java.util.Scanner;
But in java I have heard many believable sources(Deitel & Deitel and other texts) saying "as the java virtual machine keeps a list of what you use in your program and imports only what it needs to". So, even having something like import java.*, import java.*.*, import java.*.*.* is perfectly fine... to what extent I don't know if this is true or not. But it is an exaggeration of those comments.
I'm wondering if the same is true for C++ as many people use:
#include <iostream>
using namespace std;
but my teacher argues that we should only use:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::only_what_we_need_to_use;
Any one have any reliable sources explaining the benifits downfalls of using the whole namespace?
I don't know about Java, but the problem with using is not that using it makes the executable bigger or anything like that. using is just a command given to the compiler that means "if I give you an identifier that you can't find the global namespace, try looking for it in this namespace". From the point of view of the generated code, there's no drawback to using it.
The problem is that it undermines the very point of namespaces in the first place. What's the point of having classes and functions neatly organized into namespaces if the programmer can just bypass it?
[rant]
IMO, the keyword using should be removed from the language altogether, and it was a mistake to ever include it in the first place. It gets abused and misused too easily and too often, and it doesn't even need to exist. It's just a tool to make pre-standard code compile with fewer changes and syntactic sugar to avoid a few keystrokes. I mean seriously, is it that hard to type std::cout? Those five saved keystrokes aren't gonna save you from carpal tunnel syndrome.
[/rant]
And that's not to mention cin, endl, and anything else from the std namespace, other namespaces, et cetera.
In larger programs it's the more economical choice. While I do understand not using it when just dinking around at home (wow, maybe 10, 15 keystrokes saved), 5000 keystrokes is a lot.
Ah yea that makes sense, so realistically this is one of those comments that only old school people would make that are used to having to code the most "286" friendly code they can...
Otherwise, on my amd quadcore 2.1ghz nvidia board 4gb ram, won't make diddly squat difference.
Being that my teacher, learned to program using punch-cards(that's NOT an exaggeration) it seems like one of those typical comments I should generally ignore.
And it's the same for java or even more redundant, java allows you to import classes, to use functions(methods) from those classes, and from what I can tell if you use the * wildcard or not, it makes no difference, because the virtual machine(JVM) does the exact same amount of work regardless.
So you're not actually importing any/every class, in any scenario (wildcard or not), the JVM simply reads the code first, figures out what methods it needs from the packages given, and compiles accordingly.
One million saved keystrokes aren't worth the troubles using can bring.
Yea I didn't quite read between the lines enough in your first post...
So I asked this question anyway at risk of getting my ears blown off. And the response was that because I or anyone else in the class do not know what is in the namespace, we should only use the specific using std::cout, cin, endl, etc, not using namespace std;
The reason being that we WOULD and WILL run into trouble later on !!! because we do not know everything inside of the std libraries, hence only use std::cout...
It is better to use usingnamespace std; that to name classes explicitly as you have don in using std::cout;.
The rule is to never use using in a header file. In comercial projects, the advice is to never use using at all, but if you must, confine it to the lowest scope you can get away with.
This is a quote from bazzy on another topic which kind of sums up what my professor has said.
if you are using namespace std you will have the entire std namespace in the global scope.
By using just some elements you only have those elements of the namespace in you global scope.
If you are #including only standard headers you won't have problems but if you #include a header containing declarations of something which has the same name of an element from the namespace std and you are using the entire namespace you could get ambiguity errors.