(VC++.net03) using namespace std

This topic is 4727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

is there a time when you might need to use say using: std::cout, etc instead of just: using namespace std; ?

Share on other sites
The reason for namespaces is to do with name clashes.

As your programs get bigger (and bigger), and you link in more and more external code, there's more chance you'll find 2 functions / classes / whatever called the same thing.

At this point the compiler goes 'huh?' and gives you an error about ambiguous names - as it doesn't know what to call - after all, it's got more than one option.

Hence namespaces. They are just like a filing system, like directories on your computer. They add another level of classification to the naming system.

OK - now a more complete example. Let's say you decide you're doing some maths, and you write your own vector class for handling vectors. Then you decide you want to store a whole bunch of vectors, so you do something like this:

#include <vector>using namespace std;struct vector{ float x_, y_;};int main(){// What does this line do? vector<vector> myVectors; return 0;}

This would all be solved by using:

#include <vector>struct vector{ float x_, y_;};int main{ std::vector<vector> myVectors; return 0;}

Now there is no ambiguity about what you mean by a vector.

Hope this helps,
Jim.

Share on other sites
thanks for the quick reply, it helped alot.

Share on other sites
When you use "using namespace std;", i think you only need to specify the namespace when there's an ambiguity (don't quote me on this).

hope that helps !
Matt

Share on other sites
If it's your module (your cpp file), and you know you are going to be using std::vector only, you can also do this...

#include <vector>using std::vector;

... which only brings the vector class into the global namespace, and not the entire contents of std.

Share on other sites
Quote:
 Original post by Anonymous Poster... which only brings the vector class into the global namespace, and not the entire contents of std.

would this lead to performance gains, or any noticable difference?

Share on other sites
Quote:
 would this lead to performance gains, or any noticable difference?

Nope - it's purely a naming convention.

Jim.

Share on other sites
Quote:
 When you use "using namespace std;", i think you only need to specify the namespace when there's an ambiguity (don't quote me on this).

This thinking leads to this code:

#include <vector>using namespace std;// Or : as the AP pointed out, could use : using std::vectorstruct vector{	float x_, y_;};int main(){// What does this line do?	std::vector<::vector> myVectors;	return 0;}

Now we are still using the std namespace, but the nameclash variable vector is clearly disambiguated during usage.

Problem with this might be if you end up with some unexpected function overloading from namespace pollution, when you are relying on an implicit conversion.

Jim.