Using "using" and namespace pollution from header files

U

Urs Thuermann

I'd like to have some hints on how to best make use (or not make use)
of "using" in header files. Say I have a header file like this:

#include <list>
#include <vector>

class Foo {
public:
std::list<int> foo();
private:
std::vector<int> bar();
};

I could add

using std::list;
using std::vector;

so I don't need the std:: prefix in the (maybe numerous) uses of list
and vector. However, this causes the names to be visible for all
users of that header file, so I probably wouldn't do this for names
only needed in the private: section, like std::vector in the above
example.

For names needed in the public: section one could argue that the user
of the header file will probably also need std::list e.g. to define a
variable to store the return value from Foo::foo(), so the using
directive in the header problably doesn't hurt.

I still think it is better to avoid "using" at all in header files and
let the user decide what he wants in his namespace.

What is current or wide-spread best-practice?

urs
 
J

Juha Nieminen

Urs Thuermann said:
What is current or wide-spread best-practice?

The most common coding guideline is that the 'using' keyword should
never be used in a header file.

(Ok, the 'using' keyword has another role besides dealing with namespaces.
There are certain situations where you have to tell in a derived class that
a function in the base class should be visible in the derived class, but
that has nothing to do with namespaces. The guideline is only about bringing
names from namespaces to other namespaces.)

Personally I follow the rule "never use 'using'" (except in those rare
cases where its role has nothing to do with namespaces, of course). I find
that the code becomes more readable and easier to understand with the
namespace prefixes than without them. For some strange reason most people
seem to think the opposite. I don't understand that.
 
R

RaZiel

The most common coding guideline is that the 'using' keyword should
never be used in a header file.

(Ok, the 'using' keyword has another role besides dealing with namespaces.
There are certain situations where you have to tell in a derived class that
a function in the base class should be visible in the derived class, but
that has nothing to do with namespaces. The guideline is only about bringing
names from namespaces to other namespaces.)

Personally I follow the rule "never use 'using'" (except in those rare
cases where its role has nothing to do with namespaces, of course). I find
that the code becomes more readable and easier to understand with the
namespace prefixes than without them. For some strange reason most people
seem to think the opposite. I don't understand that.

I agree with your last paragraph, but only when the length of the
namespace prefixes is within a reasonable length.
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top