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