Search code examples
c++using

Could someone explain the meaning of "using"?


I do not understand below usage of using in C++. What's difference with typedef? Could someone explain it with some example?

template<typename DataType>
class DataWriter
{
using ObjType = std::function<void(DataType)>
// ...
}

Solution

  • There is no difference in your example to a typedef.

    Those are identical:

    typedef int a;
    using a = int;
    

    In general, it is more versatile though, which is the reason it was introduced:

    • It can be templated.

      template<class X> using smart = std::unique_ptr<X>;
      
    • It can be used to import symbols into the current scope.

      struct Derived : Base {
          using Base::Fun;
      };