About using namespaces

T

tron.thomas

If I remember correctly, in one of his books, Scott Meyers suggests
putting code into a custom namespace rather, and here I might be
paraphrasing, "polluting the global namespace" with type definitions
and such.

Assuming this is a good idea, I'm wondering about what happens when so
many people do the following:

using namespace std;

It seems like such a statement will bring a lot of stuff into the
global namespace, and a lot of it won't be used at all. Most people
only make use of a small portion what contained in the std namespace.

I'm wondering if it isn't better to only declare what someone wants to
use such as:

using std::vector;
using std::cout;

What kind of difference can it make to use either approach for
promoting items for a specific namespace for use in global scope?
 
D

Daniel T.

If I remember correctly, in one of his books, Scott Meyers suggests
putting code into a custom namespace rather, and here I might be
paraphrasing, "polluting the global namespace" with type definitions
and such.

Assuming this is a good idea, I'm wondering about what happens when so
many people do the following:

using namespace std;

It seems like such a statement will bring a lot of stuff into the
global namespace, and a lot of it won't be used at all. Most people
only make use of a small portion what contained in the std namespace.

I'm wondering if it isn't better to only declare what someone wants to
use such as:

using std::vector;
using std::cout;

What kind of difference can it make to use either approach for
promoting items for a specific namespace for use in global scope?

If "using namespace /X/" is contained within an individual translation
unit (a .cpp file) then it is a rather local "pollution" that can be
easily changed (assuming you aren't writing .cpp files that are
thousands of lines long.)

However putting using declarations or definitions in header files have a
much wider effect.
 
V

Victor Bazarov

If I remember correctly, in one of his books, Scott Meyers suggests
putting code into a custom namespace rather, and here I might be
paraphrasing, "polluting the global namespace" with type definitions
and such.

Assuming this is a good idea, I'm wondering about what happens when so
many people do the following:

using namespace std;

It seems like such a statement will bring a lot of stuff into the
global namespace, and a lot of it won't be used at all. Most people
only make use of a small portion what contained in the std namespace.

I'm wondering if it isn't better to only declare what someone wants to
use such as:

using std::vector;
using std::cout;

What kind of difference can it make to use either approach for
promoting items for a specific namespace for use in global scope?

I believe there is a FAQ on that. See 27.5.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

#include and namespaces 20
Best practice for 'using namespaces' 6
Namespaces 2
Indentation styles for namespaces? 3
What about namespaces ? 52
Not using namespaces 4
namespaces 5
Namespaces? 2

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top