Originally Posted by
Elysia
Meh, it has two different purposes.
1) Import a specific type into the global namespace (ie use std::cout).
Into any namespace. You can do this:
Code:
namespace foo { void x(); }
namespace bar { using foo::x; }
void f() { bar::x(); }
This is quite often useful, e.g. many Boost libraries define most of their symbols in a library-specific namespace and lift a few important names into the boost namespace.
2) Create a namespace alias (ie using l = boost::lambda).
No, you do that using namespace:
Code:
namespace ll = boost::lambda;
namespace fs = boost::filesystem;
3) Create a type alias (ie template<typename T> using Vec = std::vector).
That one is new in C++0x.
There's more.
4) Make names from a base class visible in the derived class despite overloading:
Code:
struct Base {
virtual void f(int);
virtual void f(float);
};
struct Derived : public Base {
virtual void f(int); // Override (int) version.
using Base::f; // But let the compiler still find the (float) version.
};
5) Change access of inherited elements, especially inherited from private bases:
Code:
class Whatever : private std::vector<int> {
public:
typedef std::vector<int>::iterator iterator;
typedef std::vector<int>::const_iterator const_iterator;
using std::vector<int>::begin;
using std::vector<int>::end;
};