Jump to content
  • Advertisement
Sign in to follow this  
ender_341

(VC++.net03) using namespace std

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:

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


Nope - it's purely a naming convention.

Jim.

Share this post


Link to post
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::vector

struct 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!