2

Can the keyword using in C++ be used with something except of keyword namespace? If no -- so why can't we simply write "namespace ..."? If yes -- may you give some examples of its usage in "non-namespace" context, please?

Thanks.

vortexxx192
  • 929
  • 1
  • 9
  • 24
  • http://stackoverflow.com/questions/1896830/why-should-i-use-the-using-keyword-to-access-my-base-class-method – Retired Ninja Sep 08 '13 at 20:10
  • I have answered you, but note that the first link provided by [google search "using keyword C++"](https://www.google.es/search?q=using+keyword+C%2B%2B&oq=using+keyword+C%2B%2B&aqs=chrome..69i57j0l3j69i60.3481j0&sourceid=chrome&ie=UTF-8) is the reference page of cppreference.com about it. – Manu343726 Sep 08 '13 at 20:20

3 Answers3

13

There are multiple different uses for the keyword using:

  1. You can declare that all names within a namespace don't need the namespace prefix with a using directive, e.g., using namespace std.
  2. You can declare that a specific member of a namespace doesn't need a namespace prefix with a using declaration, e.g., using std::vector.
  3. You can declare that member functions of a base class shall not be hidden in a derived class, e.g., using B::f; where B is a base class and f is a possibly overloaded member of this base class.
  4. You can declare that a base class's constructors shall be inherited, e.g., using B::B;.
  5. You can bind template arguments and give a name to template or use it to alias types, e.g., template <typename T> using singleton = std::pair<int, T>;.
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
5

Yes, you can using a single name. For instance

using std::swap;

does a "using" only to swap from namespace std, while

using namespace std;

pulls in every name in std.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
3

Yes. Its used to declare namespace members. For example:

namespace homebrew
{
   template<typename T>
   struct vector {};
}

int main()
{
    {
        using namespace std;
        vector<T> v; //Refers to std::vector
    }
    {
        using homebrew::vector;
        vector<T> v; //Refers to homebrew::vector
    }
}

Also is used to do the same with class members:

#include <iostream>
struct B {
    virtual void f(int) { std::cout << "B::f\n"; }
    void g(char)        { std::cout << "B::g\n"; }
    void h(int)         { std::cout << "B::h\n"; }
 protected:
    int m; // B::m is protected
    typedef int value_type;
};

struct D : B {
    using B::m; // D::m is public
    using B::value_type; // D::value_type is public

    using B::f;
    void f(int) { std::cout << "D::f\n"; } // D::f(int) overrides B::f(int)
    using B::g;
    void g(int) { std::cout << "D::g\n"; } // both g(int) and g(char) are visible
                                           // as members of D
    using B::h;
    void h(int) { std::cout << "D::h\n"; } // D::h(int) hides B::h(int)
};

Finally, its used since C++11 to define type aliases, template aliases included:

using vector_of_bool = std::vector<bool>;

template<typename T>
using vector_t = std::vector<T>;

The examples were extracted from the cppreference.com documentation about using keyword.

Manu343726
  • 13,969
  • 4
  • 40
  • 75