- typedef
- std::unique_ptr
> - UPtrMapSS;
- using UPtrMapSS =
- std::unique_ptr
>; -
- typedef void (*FP)(int, const std::string&);
- using FP = void (*)(int, const std::string&);
typedef不能,typedef嵌套进模板化的struct才能等效并且使用typedef声明一个使用了模板形参的对象,必须在typedef前面加上typename- // using
- template<typename T>
- using MyAllocList = std::list
>; - MyAllocList
lw; -
- template<typename T>
- class Widget {
- private:
- MyAllocList
list; - };
-
- // typedef
- template<typename T>
- struct MyAllocList {
- typedef std::list
> type; - };
- MyAllocList
::type lw; -
- template<typename T>
- class Widget {
- private:
- typename MyAllocList
::type list; - };
MyAllocList::type是一个依赖类型,必须使用typename修饰符,避免出现歧义- class Wine { … };
-
- template<> //当T是Wine
- class MyAllocList
{ //特化MyAllocList - private:
- enum class WineType
- { White, Red, Rose };
-
- WineType type; //在这个类中,type是一个数据成员!
- };
说明:如果Widget使用Wine实例化,在Widget模板中的MyAllocList将会是一个数据成员,不是一个类型,在Widget模板内,MyAllocList是否表示一个类型取决于T是什么,这就是为什么编译器会坚持要求你在前面加上typename