using代替typedef

定义函数指针类型

using用来定义类型别名是C++11引入的新语法,相比于typedef使用起来更优雅,尤其是函数指针这块,下面是对比:

// 普通函数
typedef void (*FP) (int, const std::string&);
using FP = void (*) (int, const std::string&);

// 成员函数
typedef std::string (Foo::* fooMemFnPtr) (const std::string&);
using fooMemFnPtr = std::string (Foo::*) (const std::string&);

定义模板别名

typedef要定义模板别名:

template <typename T>
struct Vec {
  typedef MyVector<T> type;
};

// usage
Vec<int>::type vec;

using定义模板别名:

template <typename T>
using Vec = MyVector<T>;

// usage
Vec<int> vec;
posted @ 2020-11-19 11:41  HachikoT  阅读(208)  评论(0编辑  收藏  举报