用using声明别名而不是typedef
typedef unsigned int uint_t; //给unsigned int取一个别名:uint_t using uint_t = unsigned int; //给unsigned int取一个别名:uint_t typedef std::map<std::string, int> map_int_t; //错误,不能这么用 typedef void (*func_t)(int, int); //定义一个函数指针func_t指向void(int, int)函数类型 using func_t = void (*)(int, int); //定义一个函数指针func_t指向void(int, int)函数类型 template<typename T> using MyAllocList = std::list<T, MyAlloc<T> > //使用MyAllocList<int> 等价于 使用std::list<int, MyAlloc<int> > template<typename T> struct MyAllocList { typedef std::list<T, MyAlloc<T> > type; }; //使用MyAllocList<int>::type 等价于 使用std::list<int, MyAlloc<int> >
typedef的问题就在于不能直接给模板定义别名。
C++11 using关键字
于 2022-01-21 12:16:24 首次发布