tags:

views:

223

answers:

5

Heya,

I'm well aware of using namespaces however, every now and then I'm stumbling upon a using, which uses a specific class. For instance :

#include <string>
using namespace std;
(...)

However - every now and then, I'm seeing :

using std::string;

How should I interpret the "using" in this case ?

Cheers

+21  A: 

using std::string simply imports std::string into the current scope (aka, you can just use 'string' rather than 'std::string') without importing everything from ::std into the current scope.


edit: clarification after comment.

Grant Limberg
The purpose of doing this is to not pollute your current namespace with EVERYTHING from another namespace. The std namespace, for instance, holds a LOT of stuff. If you don't happen to need most of it, it's kind of silly to do 'using std' and bring it all in.
Michael Kohne
A general guideline is to never use the "using ..." in library headers as you pollute the namespace for the users of the library. Keep in mind: there is no way to undo a "using".
jdehaan
+3  A: 

In this case it allows you to bind to a specific type within a namespace without qualification. As opposed to the first case which allows you to bind to any type.

JaredPar
+3  A: 

You will be able to use string class without putting std:: before it. However if you want to use something else for example a vector then you need to use std::vector

Ponting
+12  A: 

using namespace foo allows you to access all names in the namespace foo without qualification. using foo::bar allows you to use bar without qualification, but not any other names in foo.

sepp2k
+1  A: 

too make things more complicated it's possible to do that:

class Base {
protected:
    void f();
};
class Fun: public Base {
public:
    using Base::f;
};

and now you have nice public method.

teZeriusz