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)> // ... } 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)> // ... } 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; }; using appears. He was talking about alias declarations in particular.There is no difference. [dcl.typedef]/2:
A typedef-name can also be introduced by an alias-declaration. The identifier following the
usingkeyword becomes a typedef-name [..]. It has the same semantics as if it were introduced by thetypedefspecifier.
I.e.
using ObjType = std::function<void(DataType)>; Is equivalent to
typedef std::function<void(DataType)> ObjType;